Skip to content

Commit 8409f6b

Browse files
authored
fix: introduced partial state setting (#668)
1 parent 531950f commit 8409f6b

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

stackit/internal/services/argus/instance/resource.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
1212
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
1313
"github.com/hashicorp/terraform-plugin-framework/attr"
14+
"github.com/hashicorp/terraform-plugin-framework/diag"
1415
"github.com/hashicorp/terraform-plugin-framework/path"
1516
"github.com/hashicorp/terraform-plugin-framework/resource"
1617
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
@@ -19,6 +20,7 @@ import (
1920
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
2021
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
2122
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
23+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
2224
"github.com/hashicorp/terraform-plugin-framework/types"
2325
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
2426
"github.com/hashicorp/terraform-plugin-log/tflog"
@@ -875,7 +877,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
875877
}
876878

877879
// Set state to fully populated data
878-
diags = resp.State.Set(ctx, model)
880+
diags = setACL(ctx, resp.State, &model)
879881
resp.Diagnostics.Append(diags...)
880882
if resp.Diagnostics.HasError() {
881883
return
@@ -917,7 +919,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
917919
}
918920

919921
// Set state to fully populated data
920-
diags = resp.State.Set(ctx, model)
922+
diags = setMetricsRetentions(ctx, resp.State, &model)
921923
resp.Diagnostics.Append(diags...)
922924
if resp.Diagnostics.HasError() {
923925
return
@@ -961,7 +963,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
961963
}
962964

963965
// Set state to fully populated data
964-
diags = resp.State.Set(ctx, model)
966+
diags = setAlertConfig(ctx, resp.State, &model)
965967
resp.Diagnostics.Append(diags...)
966968
if resp.Diagnostics.HasError() {
967969
return
@@ -1143,8 +1145,7 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
11431145
}
11441146

11451147
// Set state to ACL populated data
1146-
diags = resp.State.Set(ctx, model)
1147-
resp.Diagnostics.Append(diags...)
1148+
resp.Diagnostics.Append(setACL(ctx, resp.State, &model)...)
11481149
if resp.Diagnostics.HasError() {
11491150
return
11501151
}
@@ -1184,7 +1185,7 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
11841185
return
11851186
}
11861187
// Set state to fully populated data
1187-
diags = resp.State.Set(ctx, model)
1188+
diags = setMetricsRetentions(ctx, resp.State, &model)
11881189
resp.Diagnostics.Append(diags...)
11891190
if resp.Diagnostics.HasError() {
11901191
return
@@ -1228,7 +1229,7 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
12281229
}
12291230

12301231
// Set state to fully populated data
1231-
diags = resp.State.Set(ctx, model)
1232+
diags = setAlertConfig(ctx, resp.State, &model)
12321233
resp.Diagnostics.Append(diags...)
12331234
if resp.Diagnostics.HasError() {
12341235
return
@@ -2185,3 +2186,18 @@ func (r *instanceResource) loadPlanId(ctx context.Context, model *Model) error {
21852186
}
21862187
return nil
21872188
}
2189+
2190+
func setACL(ctx context.Context, state tfsdk.State, model *Model) diag.Diagnostics {
2191+
return state.SetAttribute(ctx, path.Root("acl"), model.ACL)
2192+
}
2193+
2194+
func setMetricsRetentions(ctx context.Context, state tfsdk.State, model *Model) (diags diag.Diagnostics) {
2195+
diags = append(diags, state.SetAttribute(ctx, path.Root("metrics_retention_days"), model.MetricsRetentionDays)...)
2196+
diags = append(diags, state.SetAttribute(ctx, path.Root("metrics_retention_days_5m_downsampling"), model.MetricsRetentionDays5mDownsampling)...)
2197+
diags = append(diags, state.SetAttribute(ctx, path.Root("metrics_retention_days_1h_downsampling"), model.MetricsRetentionDays1hDownsampling)...)
2198+
return diags
2199+
}
2200+
2201+
func setAlertConfig(ctx context.Context, state tfsdk.State, model *Model) diag.Diagnostics {
2202+
return state.SetAttribute(ctx, path.Root("alert_config"), model.AlertConfig)
2203+
}

stackit/internal/services/observability/instance/resource.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
1212
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
1313
"github.com/hashicorp/terraform-plugin-framework/attr"
14+
"github.com/hashicorp/terraform-plugin-framework/diag"
1415
"github.com/hashicorp/terraform-plugin-framework/path"
1516
"github.com/hashicorp/terraform-plugin-framework/resource"
1617
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
@@ -19,6 +20,7 @@ import (
1920
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
2021
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
2122
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
23+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
2224
"github.com/hashicorp/terraform-plugin-framework/types"
2325
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
2426
"github.com/hashicorp/terraform-plugin-log/tflog"
@@ -924,7 +926,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
924926
}
925927

926928
// Set state to fully populated data
927-
diags = resp.State.Set(ctx, model)
929+
diags = setACL(ctx, resp.State, &model)
928930
resp.Diagnostics.Append(diags...)
929931
if resp.Diagnostics.HasError() {
930932
return
@@ -966,7 +968,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
966968
}
967969

968970
// Set state to fully populated data
969-
diags = resp.State.Set(ctx, model)
971+
diags = setMetricsRetentions(ctx, resp.State, &model)
970972
resp.Diagnostics.Append(diags...)
971973
if resp.Diagnostics.HasError() {
972974
return
@@ -1010,7 +1012,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
10101012
}
10111013

10121014
// Set state to fully populated data
1013-
diags = resp.State.Set(ctx, model)
1015+
diags = setAlertConfig(ctx, resp.State, &model)
10141016
resp.Diagnostics.Append(diags...)
10151017
if resp.Diagnostics.HasError() {
10161018
return
@@ -1192,8 +1194,7 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
11921194
}
11931195

11941196
// Set state to ACL populated data
1195-
diags = resp.State.Set(ctx, model)
1196-
resp.Diagnostics.Append(diags...)
1197+
resp.Diagnostics.Append(setACL(ctx, resp.State, &model)...)
11971198
if resp.Diagnostics.HasError() {
11981199
return
11991200
}
@@ -1233,7 +1234,7 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
12331234
return
12341235
}
12351236
// Set state to fully populated data
1236-
diags = resp.State.Set(ctx, model)
1237+
diags = setMetricsRetentions(ctx, resp.State, &model)
12371238
resp.Diagnostics.Append(diags...)
12381239
if resp.Diagnostics.HasError() {
12391240
return
@@ -1277,7 +1278,7 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
12771278
}
12781279

12791280
// Set state to fully populated data
1280-
diags = resp.State.Set(ctx, model)
1281+
diags = setAlertConfig(ctx, resp.State, &model)
12811282
resp.Diagnostics.Append(diags...)
12821283
if resp.Diagnostics.HasError() {
12831284
return
@@ -2234,3 +2235,18 @@ func (r *instanceResource) loadPlanId(ctx context.Context, model *Model) error {
22342235
}
22352236
return nil
22362237
}
2238+
2239+
func setACL(ctx context.Context, state tfsdk.State, model *Model) diag.Diagnostics {
2240+
return state.SetAttribute(ctx, path.Root("acl"), model.ACL)
2241+
}
2242+
2243+
func setMetricsRetentions(ctx context.Context, state tfsdk.State, model *Model) (diags diag.Diagnostics) {
2244+
diags = append(diags, state.SetAttribute(ctx, path.Root("metrics_retention_days"), model.MetricsRetentionDays)...)
2245+
diags = append(diags, state.SetAttribute(ctx, path.Root("metrics_retention_days_5m_downsampling"), model.MetricsRetentionDays5mDownsampling)...)
2246+
diags = append(diags, state.SetAttribute(ctx, path.Root("metrics_retention_days_1h_downsampling"), model.MetricsRetentionDays1hDownsampling)...)
2247+
return diags
2248+
}
2249+
2250+
func setAlertConfig(ctx context.Context, state tfsdk.State, model *Model) diag.Diagnostics {
2251+
return state.SetAttribute(ctx, path.Root("alert_config"), model.AlertConfig)
2252+
}

0 commit comments

Comments
 (0)