Skip to content

Commit f9589ad

Browse files
Copilottobio
andcommitted
Add default plan modifier and RequiresReplaceIf for space_id, fix test import ID
Co-authored-by: tobio <[email protected]>
1 parent ab53e8f commit f9589ad

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

internal/kibana/synthetics/acc_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ func TestSyntheticMonitorHTTPResourceWithNamespace(t *testing.T) {
902902
ResourceName: httpMonitorId,
903903
ImportState: true,
904904
ImportStateIdFunc: func(s *terraform.State) (string, error) {
905-
return fmt.Sprintf("%s/%s", "testacc", s.RootModule().Resources[httpMonitorId].Primary.Attributes["id"]), nil
905+
return s.RootModule().Resources[httpMonitorId].Primary.Attributes["id"], nil
906906
},
907907
ImportStateVerify: true,
908908
},

internal/kibana/synthetics/schema.go

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/disaster37/go-kibana-rest/v8/kbapi"
1111
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
1212
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
13+
"github.com/elastic/terraform-provider-elasticstack/internal/utils/planmodifiers"
1314
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
1415
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
1516
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
@@ -149,7 +150,8 @@ func monitorConfigSchema() schema.Schema {
149150
Optional: true,
150151
PlanModifiers: []planmodifier.String{
151152
stringplanmodifier.UseStateForUnknown(),
152-
stringplanmodifier.RequiresReplace(),
153+
planmodifiers.StringUseDefaultIfUnknown("default"),
154+
requiresReplaceIfSpaceIdChanged(),
153155
},
154156
Computed: true,
155157
},
@@ -1071,3 +1073,58 @@ func (v tfStatusConfigV0) toTfStatusConfigV0() *kbapi.SyntheticsStatusConfig {
10711073
Enabled: v.Enabled.ValueBoolPointer(),
10721074
}
10731075
}
1076+
1077+
func requiresReplaceIfSpaceIdChanged() planmodifier.String {
1078+
return stringplanmodifier.RequiresReplaceIf(
1079+
func(ctx context.Context, req planmodifier.StringRequest, resp *stringplanmodifier.RequiresReplaceIfFuncResponse) {
1080+
// Don't require replace if plan value is unknown
1081+
if req.PlanValue.IsUnknown() {
1082+
resp.RequiresReplace = false
1083+
return
1084+
}
1085+
1086+
// Don't require replace if state value is null (creating)
1087+
if req.StateValue.IsNull() {
1088+
resp.RequiresReplace = false
1089+
return
1090+
}
1091+
1092+
// Don't require replace if config value is null (not configured by user)
1093+
if req.ConfigValue.IsNull() {
1094+
resp.RequiresReplace = false
1095+
return
1096+
}
1097+
1098+
stateValue := req.StateValue.ValueString()
1099+
planValue := req.PlanValue.ValueString()
1100+
1101+
// Don't require replace if values are the same
1102+
if stateValue == planValue {
1103+
resp.RequiresReplace = false
1104+
return
1105+
}
1106+
1107+
// Normalize empty and "default" values for comparison
1108+
normalizeValue := func(v string) string {
1109+
if v == "" || v == "default" {
1110+
return "default"
1111+
}
1112+
return v
1113+
}
1114+
1115+
normalizedState := normalizeValue(stateValue)
1116+
normalizedPlan := normalizeValue(planValue)
1117+
1118+
// Don't require replace if the change is between empty/"" and "default"
1119+
if normalizedState == normalizedPlan {
1120+
resp.RequiresReplace = false
1121+
return
1122+
}
1123+
1124+
// Otherwise, require replace
1125+
resp.RequiresReplace = true
1126+
},
1127+
"Requires replace if the space_id changes, except when changing between empty and 'default'",
1128+
"Requires replace if the space_id changes, except when changing between empty and 'default'",
1129+
)
1130+
}

0 commit comments

Comments
 (0)