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>
This commit is contained in:
Alexander Dahmen 2025-02-19 13:07:54 +01:00 committed by GitHub
parent 468dc24b7a
commit bded7e1114
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 62 additions and 26 deletions

View file

@ -877,7 +877,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
}
// Set state to fully populated data
diags = setACL(ctx, resp.State, &model)
diags = setACL(ctx, &resp.State, &model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
@ -919,7 +919,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
}
// Set state to fully populated data
diags = setMetricsRetentions(ctx, resp.State, &model)
diags = setMetricsRetentions(ctx, &resp.State, &model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
@ -963,7 +963,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
}
// Set state to fully populated data
diags = setAlertConfig(ctx, resp.State, &model)
diags = setAlertConfig(ctx, &resp.State, &model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
@ -1025,6 +1025,13 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
return
}
// Set state to fully populated data
diags = resp.State.Set(ctx, model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Map response body to schema
err = mapACLField(aclListResp, &model)
if err != nil {
@ -1032,6 +1039,13 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
return
}
// Set state to fully populated data
diags = setACL(ctx, &resp.State, &model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Map response body to schema
err = mapMetricsRetentionField(metricsRetentionResp, &model)
if err != nil {
@ -1039,6 +1053,13 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
return
}
// Set state to fully populated data
diags = setMetricsRetentions(ctx, &resp.State, &model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Map response body to schema
err = mapAlertConfigField(ctx, alertConfigResp, &model)
if err != nil {
@ -1047,7 +1068,7 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
}
// Set state to fully populated data
diags = resp.State.Set(ctx, model)
diags = setAlertConfig(ctx, &resp.State, &model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
@ -1145,7 +1166,7 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
}
// Set state to ACL populated data
resp.Diagnostics.Append(setACL(ctx, resp.State, &model)...)
resp.Diagnostics.Append(setACL(ctx, &resp.State, &model)...)
if resp.Diagnostics.HasError() {
return
}
@ -1185,7 +1206,7 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
return
}
// Set state to fully populated data
diags = setMetricsRetentions(ctx, resp.State, &model)
diags = setMetricsRetentions(ctx, &resp.State, &model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
@ -1229,7 +1250,7 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
}
// Set state to fully populated data
diags = setAlertConfig(ctx, resp.State, &model)
diags = setAlertConfig(ctx, &resp.State, &model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
@ -1335,9 +1356,6 @@ func mapFields(ctx context.Context, r *argus.GetInstanceResponse, model *Model)
model.GrafanaPublicReadAccess = types.BoolPointerValue(i.GrafanaPublicReadAccess)
model.GrafanaInitialAdminPassword = types.StringPointerValue(i.GrafanaAdminPassword)
model.GrafanaInitialAdminUser = types.StringPointerValue(i.GrafanaAdminUser)
model.MetricsRetentionDays = types.Int64Value(int64(*i.MetricsRetentionTimeRaw))
model.MetricsRetentionDays5mDownsampling = types.Int64Value(int64(*i.MetricsRetentionTime5m))
model.MetricsRetentionDays1hDownsampling = types.Int64Value(int64(*i.MetricsRetentionTime1h))
model.MetricsURL = types.StringPointerValue(i.MetricsUrl)
model.MetricsPushURL = types.StringPointerValue(i.PushMetricsUrl)
model.TargetsURL = types.StringPointerValue(i.TargetsUrl)
@ -2187,17 +2205,17 @@ func (r *instanceResource) loadPlanId(ctx context.Context, model *Model) error {
return nil
}
func setACL(ctx context.Context, state tfsdk.State, model *Model) diag.Diagnostics {
func setACL(ctx context.Context, state *tfsdk.State, model *Model) diag.Diagnostics {
return state.SetAttribute(ctx, path.Root("acl"), model.ACL)
}
func setMetricsRetentions(ctx context.Context, state tfsdk.State, model *Model) (diags diag.Diagnostics) {
func setMetricsRetentions(ctx context.Context, state *tfsdk.State, model *Model) (diags diag.Diagnostics) {
diags = append(diags, state.SetAttribute(ctx, path.Root("metrics_retention_days"), model.MetricsRetentionDays)...)
diags = append(diags, state.SetAttribute(ctx, path.Root("metrics_retention_days_5m_downsampling"), model.MetricsRetentionDays5mDownsampling)...)
diags = append(diags, state.SetAttribute(ctx, path.Root("metrics_retention_days_1h_downsampling"), model.MetricsRetentionDays1hDownsampling)...)
return diags
}
func setAlertConfig(ctx context.Context, state tfsdk.State, model *Model) diag.Diagnostics {
func setAlertConfig(ctx context.Context, state *tfsdk.State, model *Model) diag.Diagnostics {
return state.SetAttribute(ctx, path.Root("alert_config"), model.AlertConfig)
}

View file

@ -926,7 +926,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
}
// Set state to fully populated data
diags = setACL(ctx, resp.State, &model)
diags = setACL(ctx, &resp.State, &model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
@ -968,7 +968,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
}
// Set state to fully populated data
diags = setMetricsRetentions(ctx, resp.State, &model)
diags = setMetricsRetentions(ctx, &resp.State, &model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
@ -1012,7 +1012,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
}
// Set state to fully populated data
diags = setAlertConfig(ctx, resp.State, &model)
diags = setAlertConfig(ctx, &resp.State, &model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
@ -1074,6 +1074,13 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
return
}
// Set state to fully populated data
diags = resp.State.Set(ctx, model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Map response body to schema
err = mapACLField(aclListResp, &model)
if err != nil {
@ -1081,6 +1088,13 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
return
}
// Set state to fully populated data
diags = setACL(ctx, &resp.State, &model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Map response body to schema
err = mapMetricsRetentionField(metricsRetentionResp, &model)
if err != nil {
@ -1088,6 +1102,13 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
return
}
// Set state to fully populated data
diags = setMetricsRetentions(ctx, &resp.State, &model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Map response body to schema
err = mapAlertConfigField(ctx, alertConfigResp, &model)
if err != nil {
@ -1096,7 +1117,7 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
}
// Set state to fully populated data
diags = resp.State.Set(ctx, model)
diags = setAlertConfig(ctx, &resp.State, &model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
@ -1194,7 +1215,7 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
}
// Set state to ACL populated data
resp.Diagnostics.Append(setACL(ctx, resp.State, &model)...)
resp.Diagnostics.Append(setACL(ctx, &resp.State, &model)...)
if resp.Diagnostics.HasError() {
return
}
@ -1234,7 +1255,7 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
return
}
// Set state to fully populated data
diags = setMetricsRetentions(ctx, resp.State, &model)
diags = setMetricsRetentions(ctx, &resp.State, &model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
@ -1278,7 +1299,7 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
}
// Set state to fully populated data
diags = setAlertConfig(ctx, resp.State, &model)
diags = setAlertConfig(ctx, &resp.State, &model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
@ -1384,9 +1405,6 @@ func mapFields(ctx context.Context, r *observability.GetInstanceResponse, model
model.GrafanaPublicReadAccess = types.BoolPointerValue(i.GrafanaPublicReadAccess)
model.GrafanaInitialAdminPassword = types.StringPointerValue(i.GrafanaAdminPassword)
model.GrafanaInitialAdminUser = types.StringPointerValue(i.GrafanaAdminUser)
model.MetricsRetentionDays = types.Int64Value(int64(*i.MetricsRetentionTimeRaw))
model.MetricsRetentionDays5mDownsampling = types.Int64Value(int64(*i.MetricsRetentionTime5m))
model.MetricsRetentionDays1hDownsampling = types.Int64Value(int64(*i.MetricsRetentionTime1h))
model.MetricsURL = types.StringPointerValue(i.MetricsUrl)
model.MetricsPushURL = types.StringPointerValue(i.PushMetricsUrl)
model.TargetsURL = types.StringPointerValue(i.TargetsUrl)
@ -2236,17 +2254,17 @@ func (r *instanceResource) loadPlanId(ctx context.Context, model *Model) error {
return nil
}
func setACL(ctx context.Context, state tfsdk.State, model *Model) diag.Diagnostics {
func setACL(ctx context.Context, state *tfsdk.State, model *Model) diag.Diagnostics {
return state.SetAttribute(ctx, path.Root("acl"), model.ACL)
}
func setMetricsRetentions(ctx context.Context, state tfsdk.State, model *Model) (diags diag.Diagnostics) {
func setMetricsRetentions(ctx context.Context, state *tfsdk.State, model *Model) (diags diag.Diagnostics) {
diags = append(diags, state.SetAttribute(ctx, path.Root("metrics_retention_days"), model.MetricsRetentionDays)...)
diags = append(diags, state.SetAttribute(ctx, path.Root("metrics_retention_days_5m_downsampling"), model.MetricsRetentionDays5mDownsampling)...)
diags = append(diags, state.SetAttribute(ctx, path.Root("metrics_retention_days_1h_downsampling"), model.MetricsRetentionDays1hDownsampling)...)
return diags
}
func setAlertConfig(ctx context.Context, state tfsdk.State, model *Model) diag.Diagnostics {
func setAlertConfig(ctx context.Context, state *tfsdk.State, model *Model) diag.Diagnostics {
return state.SetAttribute(ctx, path.Root("alert_config"), model.AlertConfig)
}