Skip to content

Commit bded7e1

Browse files
authored
Bugfix: MetricsRetentionDays not set correctly to model (#681)
Add missing set functions to Read() as well. State need to be passed as pointer in order to be set correctly. Signed-off-by: Alexander Dahmen <alexander.dahmen@inovex.de>
1 parent 468dc24 commit bded7e1

2 files changed

Lines changed: 62 additions & 26 deletions

File tree

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

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
877877
}
878878

879879
// Set state to fully populated data
880-
diags = setACL(ctx, resp.State, &model)
880+
diags = setACL(ctx, &resp.State, &model)
881881
resp.Diagnostics.Append(diags...)
882882
if resp.Diagnostics.HasError() {
883883
return
@@ -919,7 +919,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
919919
}
920920

921921
// Set state to fully populated data
922-
diags = setMetricsRetentions(ctx, resp.State, &model)
922+
diags = setMetricsRetentions(ctx, &resp.State, &model)
923923
resp.Diagnostics.Append(diags...)
924924
if resp.Diagnostics.HasError() {
925925
return
@@ -963,7 +963,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
963963
}
964964

965965
// Set state to fully populated data
966-
diags = setAlertConfig(ctx, resp.State, &model)
966+
diags = setAlertConfig(ctx, &resp.State, &model)
967967
resp.Diagnostics.Append(diags...)
968968
if resp.Diagnostics.HasError() {
969969
return
@@ -1025,20 +1025,41 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
10251025
return
10261026
}
10271027

1028+
// Set state to fully populated data
1029+
diags = resp.State.Set(ctx, model)
1030+
resp.Diagnostics.Append(diags...)
1031+
if resp.Diagnostics.HasError() {
1032+
return
1033+
}
1034+
10281035
// Map response body to schema
10291036
err = mapACLField(aclListResp, &model)
10301037
if err != nil {
10311038
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Processing API response for the ACL: %v", err))
10321039
return
10331040
}
10341041

1042+
// Set state to fully populated data
1043+
diags = setACL(ctx, &resp.State, &model)
1044+
resp.Diagnostics.Append(diags...)
1045+
if resp.Diagnostics.HasError() {
1046+
return
1047+
}
1048+
10351049
// Map response body to schema
10361050
err = mapMetricsRetentionField(metricsRetentionResp, &model)
10371051
if err != nil {
10381052
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Processing API response for the metrics retention: %v", err))
10391053
return
10401054
}
10411055

1056+
// Set state to fully populated data
1057+
diags = setMetricsRetentions(ctx, &resp.State, &model)
1058+
resp.Diagnostics.Append(diags...)
1059+
if resp.Diagnostics.HasError() {
1060+
return
1061+
}
1062+
10421063
// Map response body to schema
10431064
err = mapAlertConfigField(ctx, alertConfigResp, &model)
10441065
if err != nil {
@@ -1047,7 +1068,7 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
10471068
}
10481069

10491070
// Set state to fully populated data
1050-
diags = resp.State.Set(ctx, model)
1071+
diags = setAlertConfig(ctx, &resp.State, &model)
10511072
resp.Diagnostics.Append(diags...)
10521073
if resp.Diagnostics.HasError() {
10531074
return
@@ -1145,7 +1166,7 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
11451166
}
11461167

11471168
// Set state to ACL populated data
1148-
resp.Diagnostics.Append(setACL(ctx, resp.State, &model)...)
1169+
resp.Diagnostics.Append(setACL(ctx, &resp.State, &model)...)
11491170
if resp.Diagnostics.HasError() {
11501171
return
11511172
}
@@ -1185,7 +1206,7 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
11851206
return
11861207
}
11871208
// Set state to fully populated data
1188-
diags = setMetricsRetentions(ctx, resp.State, &model)
1209+
diags = setMetricsRetentions(ctx, &resp.State, &model)
11891210
resp.Diagnostics.Append(diags...)
11901211
if resp.Diagnostics.HasError() {
11911212
return
@@ -1229,7 +1250,7 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
12291250
}
12301251

12311252
// Set state to fully populated data
1232-
diags = setAlertConfig(ctx, resp.State, &model)
1253+
diags = setAlertConfig(ctx, &resp.State, &model)
12331254
resp.Diagnostics.Append(diags...)
12341255
if resp.Diagnostics.HasError() {
12351256
return
@@ -1335,9 +1356,6 @@ func mapFields(ctx context.Context, r *argus.GetInstanceResponse, model *Model)
13351356
model.GrafanaPublicReadAccess = types.BoolPointerValue(i.GrafanaPublicReadAccess)
13361357
model.GrafanaInitialAdminPassword = types.StringPointerValue(i.GrafanaAdminPassword)
13371358
model.GrafanaInitialAdminUser = types.StringPointerValue(i.GrafanaAdminUser)
1338-
model.MetricsRetentionDays = types.Int64Value(int64(*i.MetricsRetentionTimeRaw))
1339-
model.MetricsRetentionDays5mDownsampling = types.Int64Value(int64(*i.MetricsRetentionTime5m))
1340-
model.MetricsRetentionDays1hDownsampling = types.Int64Value(int64(*i.MetricsRetentionTime1h))
13411359
model.MetricsURL = types.StringPointerValue(i.MetricsUrl)
13421360
model.MetricsPushURL = types.StringPointerValue(i.PushMetricsUrl)
13431361
model.TargetsURL = types.StringPointerValue(i.TargetsUrl)
@@ -2187,17 +2205,17 @@ func (r *instanceResource) loadPlanId(ctx context.Context, model *Model) error {
21872205
return nil
21882206
}
21892207

2190-
func setACL(ctx context.Context, state tfsdk.State, model *Model) diag.Diagnostics {
2208+
func setACL(ctx context.Context, state *tfsdk.State, model *Model) diag.Diagnostics {
21912209
return state.SetAttribute(ctx, path.Root("acl"), model.ACL)
21922210
}
21932211

2194-
func setMetricsRetentions(ctx context.Context, state tfsdk.State, model *Model) (diags diag.Diagnostics) {
2212+
func setMetricsRetentions(ctx context.Context, state *tfsdk.State, model *Model) (diags diag.Diagnostics) {
21952213
diags = append(diags, state.SetAttribute(ctx, path.Root("metrics_retention_days"), model.MetricsRetentionDays)...)
21962214
diags = append(diags, state.SetAttribute(ctx, path.Root("metrics_retention_days_5m_downsampling"), model.MetricsRetentionDays5mDownsampling)...)
21972215
diags = append(diags, state.SetAttribute(ctx, path.Root("metrics_retention_days_1h_downsampling"), model.MetricsRetentionDays1hDownsampling)...)
21982216
return diags
21992217
}
22002218

2201-
func setAlertConfig(ctx context.Context, state tfsdk.State, model *Model) diag.Diagnostics {
2219+
func setAlertConfig(ctx context.Context, state *tfsdk.State, model *Model) diag.Diagnostics {
22022220
return state.SetAttribute(ctx, path.Root("alert_config"), model.AlertConfig)
22032221
}

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

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
926926
}
927927

928928
// Set state to fully populated data
929-
diags = setACL(ctx, resp.State, &model)
929+
diags = setACL(ctx, &resp.State, &model)
930930
resp.Diagnostics.Append(diags...)
931931
if resp.Diagnostics.HasError() {
932932
return
@@ -968,7 +968,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
968968
}
969969

970970
// Set state to fully populated data
971-
diags = setMetricsRetentions(ctx, resp.State, &model)
971+
diags = setMetricsRetentions(ctx, &resp.State, &model)
972972
resp.Diagnostics.Append(diags...)
973973
if resp.Diagnostics.HasError() {
974974
return
@@ -1012,7 +1012,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
10121012
}
10131013

10141014
// Set state to fully populated data
1015-
diags = setAlertConfig(ctx, resp.State, &model)
1015+
diags = setAlertConfig(ctx, &resp.State, &model)
10161016
resp.Diagnostics.Append(diags...)
10171017
if resp.Diagnostics.HasError() {
10181018
return
@@ -1074,20 +1074,41 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
10741074
return
10751075
}
10761076

1077+
// Set state to fully populated data
1078+
diags = resp.State.Set(ctx, model)
1079+
resp.Diagnostics.Append(diags...)
1080+
if resp.Diagnostics.HasError() {
1081+
return
1082+
}
1083+
10771084
// Map response body to schema
10781085
err = mapACLField(aclListResp, &model)
10791086
if err != nil {
10801087
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Processing API response for the ACL: %v", err))
10811088
return
10821089
}
10831090

1091+
// Set state to fully populated data
1092+
diags = setACL(ctx, &resp.State, &model)
1093+
resp.Diagnostics.Append(diags...)
1094+
if resp.Diagnostics.HasError() {
1095+
return
1096+
}
1097+
10841098
// Map response body to schema
10851099
err = mapMetricsRetentionField(metricsRetentionResp, &model)
10861100
if err != nil {
10871101
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Processing API response for the metrics retention: %v", err))
10881102
return
10891103
}
10901104

1105+
// Set state to fully populated data
1106+
diags = setMetricsRetentions(ctx, &resp.State, &model)
1107+
resp.Diagnostics.Append(diags...)
1108+
if resp.Diagnostics.HasError() {
1109+
return
1110+
}
1111+
10911112
// Map response body to schema
10921113
err = mapAlertConfigField(ctx, alertConfigResp, &model)
10931114
if err != nil {
@@ -1096,7 +1117,7 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
10961117
}
10971118

10981119
// Set state to fully populated data
1099-
diags = resp.State.Set(ctx, model)
1120+
diags = setAlertConfig(ctx, &resp.State, &model)
11001121
resp.Diagnostics.Append(diags...)
11011122
if resp.Diagnostics.HasError() {
11021123
return
@@ -1194,7 +1215,7 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
11941215
}
11951216

11961217
// Set state to ACL populated data
1197-
resp.Diagnostics.Append(setACL(ctx, resp.State, &model)...)
1218+
resp.Diagnostics.Append(setACL(ctx, &resp.State, &model)...)
11981219
if resp.Diagnostics.HasError() {
11991220
return
12001221
}
@@ -1234,7 +1255,7 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
12341255
return
12351256
}
12361257
// Set state to fully populated data
1237-
diags = setMetricsRetentions(ctx, resp.State, &model)
1258+
diags = setMetricsRetentions(ctx, &resp.State, &model)
12381259
resp.Diagnostics.Append(diags...)
12391260
if resp.Diagnostics.HasError() {
12401261
return
@@ -1278,7 +1299,7 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
12781299
}
12791300

12801301
// Set state to fully populated data
1281-
diags = setAlertConfig(ctx, resp.State, &model)
1302+
diags = setAlertConfig(ctx, &resp.State, &model)
12821303
resp.Diagnostics.Append(diags...)
12831304
if resp.Diagnostics.HasError() {
12841305
return
@@ -1384,9 +1405,6 @@ func mapFields(ctx context.Context, r *observability.GetInstanceResponse, model
13841405
model.GrafanaPublicReadAccess = types.BoolPointerValue(i.GrafanaPublicReadAccess)
13851406
model.GrafanaInitialAdminPassword = types.StringPointerValue(i.GrafanaAdminPassword)
13861407
model.GrafanaInitialAdminUser = types.StringPointerValue(i.GrafanaAdminUser)
1387-
model.MetricsRetentionDays = types.Int64Value(int64(*i.MetricsRetentionTimeRaw))
1388-
model.MetricsRetentionDays5mDownsampling = types.Int64Value(int64(*i.MetricsRetentionTime5m))
1389-
model.MetricsRetentionDays1hDownsampling = types.Int64Value(int64(*i.MetricsRetentionTime1h))
13901408
model.MetricsURL = types.StringPointerValue(i.MetricsUrl)
13911409
model.MetricsPushURL = types.StringPointerValue(i.PushMetricsUrl)
13921410
model.TargetsURL = types.StringPointerValue(i.TargetsUrl)
@@ -2236,17 +2254,17 @@ func (r *instanceResource) loadPlanId(ctx context.Context, model *Model) error {
22362254
return nil
22372255
}
22382256

2239-
func setACL(ctx context.Context, state tfsdk.State, model *Model) diag.Diagnostics {
2257+
func setACL(ctx context.Context, state *tfsdk.State, model *Model) diag.Diagnostics {
22402258
return state.SetAttribute(ctx, path.Root("acl"), model.ACL)
22412259
}
22422260

2243-
func setMetricsRetentions(ctx context.Context, state tfsdk.State, model *Model) (diags diag.Diagnostics) {
2261+
func setMetricsRetentions(ctx context.Context, state *tfsdk.State, model *Model) (diags diag.Diagnostics) {
22442262
diags = append(diags, state.SetAttribute(ctx, path.Root("metrics_retention_days"), model.MetricsRetentionDays)...)
22452263
diags = append(diags, state.SetAttribute(ctx, path.Root("metrics_retention_days_5m_downsampling"), model.MetricsRetentionDays5mDownsampling)...)
22462264
diags = append(diags, state.SetAttribute(ctx, path.Root("metrics_retention_days_1h_downsampling"), model.MetricsRetentionDays1hDownsampling)...)
22472265
return diags
22482266
}
22492267

2250-
func setAlertConfig(ctx context.Context, state tfsdk.State, model *Model) diag.Diagnostics {
2268+
func setAlertConfig(ctx context.Context, state *tfsdk.State, model *Model) diag.Diagnostics {
22512269
return state.SetAttribute(ctx, path.Root("alert_config"), model.AlertConfig)
22522270
}

0 commit comments

Comments
 (0)