Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions stackit/internal/services/mongodbflex/instance/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

mongodbflexUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/mongodbflex/utils"
stringplanmodifierCustom "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils/planmodifiers/stringplanmodifier"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
Expand Down Expand Up @@ -240,6 +241,9 @@ func (r *instanceResource) Schema(_ context.Context, _ resource.SchemaRequest, r
"backup_schedule": schema.StringAttribute{
Description: descriptions["backup_schedule"],
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifierCustom.CronNormalizationModifier{},
},
},
"flavor": schema.SingleNestedAttribute{
Required: true,
Expand Down Expand Up @@ -852,10 +856,11 @@ func mapFields(ctx context.Context, resp *mongodbflex.InstanceResponse, model *M
return fmt.Errorf("creating options: %w", core.DiagsToError(diags))
}

simplifiedModelBackupSchedule := utils.SimplifyBackupSchedule(model.BackupSchedule.ValueString())
// If the value returned by the API is different from the one in the model after simplification,
// we update the model so that it causes an error in Terraform
if simplifiedModelBackupSchedule != types.StringPointerValue(instance.BackupSchedule).ValueString() {
// If the API returned "0 0 * * *" but user defined "00 00 * * *" in its config,
// we keep the user's "00 00 * * *" in the state to satisfy Terraform.
backupScheduleApiResp := types.StringPointerValue(instance.BackupSchedule)
if utils.SimplifyCronString(model.BackupSchedule.ValueString()) != utils.SimplifyCronString(backupScheduleApiResp.ValueString()) {
// If the API actually changed it to something else, use the API value
model.BackupSchedule = types.StringPointerValue(instance.BackupSchedule)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -534,23 +534,23 @@ func (r *instanceResource) Schema(_ context.Context, _ resource.SchemaRequest, r
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknownIf(utils.Int64Changed, "metrics_retention_days", "sets `UseStateForUnknown` only if `metrics_retention_days` has not changed"),
int64planmodifier.UseStateForUnknownIf(int64planmodifier.Int64Changed, "metrics_retention_days", "sets `UseStateForUnknown` only if `metrics_retention_days` has not changed"),
},
},
"metrics_retention_days_5m_downsampling": schema.Int64Attribute{
Description: "Specifies for how many days the 5m downsampled metrics are kept. must be less than the value of the general retention. Default is set to `90`.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknownIf(utils.Int64Changed, "metrics_retention_days_5m_downsampling", "sets `UseStateForUnknown` only if `metrics_retention_days_5m_downsampling` has not changed"),
int64planmodifier.UseStateForUnknownIf(int64planmodifier.Int64Changed, "metrics_retention_days_5m_downsampling", "sets `UseStateForUnknown` only if `metrics_retention_days_5m_downsampling` has not changed"),
},
},
"metrics_retention_days_1h_downsampling": schema.Int64Attribute{
Description: "Specifies for how many days the 1h downsampled metrics are kept. must be less than the value of the 5m downsampling retention. Default is set to `90`.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknownIf(utils.Int64Changed, "metrics_retention_days_1h_downsampling", "sets `UseStateForUnknown` only if `metrics_retention_days_1h_downsampling` has not changed"),
int64planmodifier.UseStateForUnknownIf(int64planmodifier.Int64Changed, "metrics_retention_days_1h_downsampling", "sets `UseStateForUnknown` only if `metrics_retention_days_1h_downsampling` has not changed"),
},
},
"metrics_url": schema.StringAttribute{
Expand Down
13 changes: 12 additions & 1 deletion stackit/internal/services/postgresflex/instance/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
stringplanmodifierCustom "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils/planmodifiers/stringplanmodifier"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"

"github.com/hashicorp/terraform-plugin-framework/resource"
Expand Down Expand Up @@ -209,6 +210,9 @@ func (r *instanceResource) Schema(_ context.Context, req resource.SchemaRequest,
"backup_schedule": schema.StringAttribute{
Description: descriptions["backup_schedule"],
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifierCustom.CronNormalizationModifier{},
},
},
"flavor": schema.SingleNestedAttribute{
Required: true,
Expand Down Expand Up @@ -652,11 +656,18 @@ func mapFields(ctx context.Context, resp *postgresflex.InstanceResponse, model *
return fmt.Errorf("creating storage: %w", core.DiagsToError(diags))
}

// If the API returned "0 0 * * *" but user defined "00 00 * * *" in its config,
// we keep the user's "00 00 * * *" in the state to satisfy Terraform.
backupScheduleApiResp := types.StringPointerValue(instance.BackupSchedule)
if utils.SimplifyCronString(model.BackupSchedule.ValueString()) != utils.SimplifyCronString(backupScheduleApiResp.ValueString()) {
// If the API actually changed it to something else, use the API value
model.BackupSchedule = types.StringPointerValue(instance.BackupSchedule)
}

model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), region, instanceId)
model.InstanceId = types.StringValue(instanceId)
model.Name = types.StringPointerValue(instance.Name)
model.ACL = aclList
model.BackupSchedule = types.StringPointerValue(instance.BackupSchedule)
model.Flavor = flavorObject
model.Replicas = types.Int64PointerValue(instance.Replicas)
model.Storage = storageObject
Expand Down
89 changes: 68 additions & 21 deletions stackit/internal/services/postgresflex/instance/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,37 @@ func (c *postgresFlexClientMocked) ListFlavorsExecute(_ context.Context, _, _ st

func TestMapFields(t *testing.T) {
const testRegion = "region"

fixtureModel := func(mods ...func(*Model)) Model {
m := Model{
Id: types.StringValue("pid,region,iid"),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Name: types.StringNull(),
ACL: types.ListNull(types.StringType),
BackupSchedule: types.StringNull(),
Flavor: types.ObjectValueMust(flavorTypes, map[string]attr.Value{
"id": types.StringNull(),
"description": types.StringNull(),
"cpu": types.Int64Null(),
"ram": types.Int64Null(),
}),
Replicas: types.Int64Null(),
Storage: types.ObjectValueMust(storageTypes, map[string]attr.Value{
"class": types.StringNull(),
"size": types.Int64Null(),
}),
Version: types.StringNull(),
Region: types.StringValue(testRegion),
}

for _, mod := range mods {
mod(&m)
}

return m
}

tests := []struct {
description string
state Model
Expand All @@ -49,27 +80,7 @@ func TestMapFields(t *testing.T) {
&flavorModel{},
&storageModel{},
testRegion,
Model{
Id: types.StringValue("pid,region,iid"),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Name: types.StringNull(),
ACL: types.ListNull(types.StringType),
BackupSchedule: types.StringNull(),
Flavor: types.ObjectValueMust(flavorTypes, map[string]attr.Value{
"id": types.StringNull(),
"description": types.StringNull(),
"cpu": types.Int64Null(),
"ram": types.Int64Null(),
}),
Replicas: types.Int64Null(),
Storage: types.ObjectValueMust(storageTypes, map[string]attr.Value{
"class": types.StringNull(),
"size": types.Int64Null(),
}),
Version: types.StringNull(),
Region: types.StringValue(testRegion),
},
fixtureModel(),
true,
},
{
Expand Down Expand Up @@ -262,6 +273,42 @@ func TestMapFields(t *testing.T) {
},
true,
},
{
description: "backup schedule - keep state value when API strips leading zeros",
state: fixtureModel(func(m *Model) {
m.BackupSchedule = types.StringValue("00 00 * * *")
}),
input: &postgresflex.InstanceResponse{
Item: &postgresflex.Instance{
BackupSchedule: utils.Ptr("0 0 * * *"),
},
},
flavor: &flavorModel{},
storage: &storageModel{},
region: testRegion,
expected: fixtureModel(func(m *Model) {
m.BackupSchedule = types.StringValue("00 00 * * *")
}),
isValid: true,
},
{
description: "backup schedule - use updated value from API if cron actually changed",
state: fixtureModel(func(m *Model) {
m.BackupSchedule = types.StringValue("00 01 * * *")
}),
input: &postgresflex.InstanceResponse{
Item: &postgresflex.Instance{
BackupSchedule: utils.Ptr("0 2 * * *"),
},
},
flavor: &flavorModel{},
storage: &storageModel{},
region: testRegion,
expected: fixtureModel(func(m *Model) {
m.BackupSchedule = types.StringValue("0 2 * * *")
}),
isValid: true,
},
{
"nil_response",
Model{
Expand Down
4 changes: 2 additions & 2 deletions stackit/internal/services/ske/cluster/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ func (r *clusterResource) Schema(_ context.Context, _ resource.SchemaRequest, re
Description: "Full Kubernetes version used. For example, if 1.22 was set in `kubernetes_version_min`, this value may result to 1.22.15. " + SKEUpdateDoc,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifierUtils.UseStateForUnknownIf(utils.StringChanged, "kubernetes_version_min", "sets `UseStateForUnknown` only if `kubernetes_min_version` has not changed"),
stringplanmodifierUtils.UseStateForUnknownIf(stringplanmodifierUtils.StringChanged, "kubernetes_version_min", "sets `UseStateForUnknown` only if `kubernetes_min_version` has not changed"),
},
},
"egress_address_ranges": schema.ListAttribute{
Expand Down Expand Up @@ -462,7 +462,7 @@ func (r *clusterResource) Schema(_ context.Context, _ resource.SchemaRequest, re
Description: "Full OS image version used. For example, if 3815.2 was set in `os_version_min`, this value may result to 3815.2.2. " + SKEUpdateDoc,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifierUtils.UseStateForUnknownIf(utils.StringChanged, "os_version_min", "sets `UseStateForUnknown` only if `os_version_min` has not changed"),
stringplanmodifierUtils.UseStateForUnknownIf(stringplanmodifierUtils.StringChanged, "os_version_min", "sets `UseStateForUnknown` only if `os_version_min` has not changed"),
},
},
"volume_type": schema.StringAttribute{
Expand Down
13 changes: 7 additions & 6 deletions stackit/internal/services/sqlserverflex/instance/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

sqlserverflexUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/sqlserverflex/utils"
stringplanmodifierCustom "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils/planmodifiers/stringplanmodifier"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
Expand Down Expand Up @@ -230,9 +231,8 @@ func (r *instanceResource) Schema(_ context.Context, _ resource.SchemaRequest, r
"backup_schedule": schema.StringAttribute{
Description: descriptions["backup_schedule"],
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
stringplanmodifierCustom.CronNormalizationModifier{},
},
},
"flavor": schema.SingleNestedAttribute{
Expand Down Expand Up @@ -784,10 +784,11 @@ func mapFields(ctx context.Context, resp *sqlserverflex.GetInstanceResponse, mod
return fmt.Errorf("creating options: %w", core.DiagsToError(diags))
}

simplifiedModelBackupSchedule := utils.SimplifyBackupSchedule(model.BackupSchedule.ValueString())
// If the value returned by the API is different from the one in the model after simplification,
// we update the model so that it causes an error in Terraform
if simplifiedModelBackupSchedule != types.StringPointerValue(instance.BackupSchedule).ValueString() {
// If the API returned "0 0 * * *" but user defined "00 00 * * *" in its config,
// we keep the user's "00 00 * * *" in the state to satisfy Terraform.
backupScheduleApiResp := types.StringPointerValue(instance.BackupSchedule)
if utils.SimplifyCronString(model.BackupSchedule.ValueString()) != utils.SimplifyCronString(backupScheduleApiResp.ValueString()) {
// If the API actually changed it to something else, use the API value
model.BackupSchedule = types.StringPointerValue(instance.BackupSchedule)
}

Expand Down
51 changes: 0 additions & 51 deletions stackit/internal/utils/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ import (

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils/planmodifiers/int64planmodifier"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils/planmodifiers/stringplanmodifier"
)

type attributeGetter interface {
Expand Down Expand Up @@ -47,51 +44,3 @@ func GetTimeFromStringAttribute(ctx context.Context, attributePath path.Path, so

return diags
}

// Int64Changed sets UseStateForUnkown to true if the attribute's planned value matches the current state
func Int64Changed(ctx context.Context, attributeName string, request planmodifier.Int64Request, response *int64planmodifier.UseStateForUnknownFuncResponse) { // nolint:gocritic // function signature required by Terraform
dependencyPath := request.Path.ParentPath().AtName(attributeName)

var attributePlan types.Int64
diags := request.Plan.GetAttribute(ctx, dependencyPath, &attributePlan)
response.Diagnostics.Append(diags...)
if response.Diagnostics.HasError() {
return
}

var attributeState types.Int64
diags = request.State.GetAttribute(ctx, dependencyPath, &attributeState)
response.Diagnostics.Append(diags...)
if response.Diagnostics.HasError() {
return
}

if attributeState == attributePlan {
response.UseStateForUnknown = true
return
}
}

// StringChanged sets UseStateForUnkown to true if the attribute's planned value matches the current state
func StringChanged(ctx context.Context, attributeName string, request planmodifier.StringRequest, response *stringplanmodifier.UseStateForUnknownFuncResponse) { // nolint:gocritic // function signature required by Terraform
dependencyPath := request.Path.ParentPath().AtName(attributeName)

var attributePlan types.String
diags := request.Plan.GetAttribute(ctx, dependencyPath, &attributePlan)
response.Diagnostics.Append(diags...)
if response.Diagnostics.HasError() {
return
}

var attributeState types.String
diags = request.State.GetAttribute(ctx, dependencyPath, &attributeState)
response.Diagnostics.Append(diags...)
if response.Diagnostics.HasError() {
return
}

if attributeState == attributePlan {
response.UseStateForUnknown = true
return
}
}
24 changes: 24 additions & 0 deletions stackit/internal/utils/cron.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package utils

import (
"regexp"
"strings"
)

// SimplifyCronString removes leading 0s from backup schedule numbers (e.g. "00 00 * * *" becomes "0 0 * * *")
// Needed as some API might do it internally and would otherwise cause inconsistent result in Terraform
func SimplifyCronString(cron string) string {
regex := regexp.MustCompile(`0+\d+`) // Matches series of one or more zeros followed by a series of one or more digits
simplifiedCron := regex.ReplaceAllStringFunc(cron, func(match string) string {
simplified := strings.TrimLeft(match, "0")
if simplified == "" {
simplified = "0"
}
return simplified
})

whiteSpaceRegex := regexp.MustCompile(`\s+`)
simplifiedCron = whiteSpaceRegex.ReplaceAllString(simplifiedCron, " ")

return simplifiedCron
}
Loading
Loading