Skip to content

Commit d2ba60b

Browse files
committed
fix(observability): Some plans do not offer to configure alert configs or store metrics.
Signed-off-by: Alexander Dahmen <alexander.dahmen@inovex.de>
1 parent bb09c85 commit d2ba60b

File tree

2 files changed

+268
-223
lines changed

2 files changed

+268
-223
lines changed

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

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -400,18 +400,6 @@ func (d *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques
400400
return
401401
}
402402

403-
metricsRetentionResp, err := d.client.GetMetricsStorageRetention(ctx, instanceId, projectId).Execute()
404-
if err != nil {
405-
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Calling API to get metrics retention: %v", err))
406-
return
407-
}
408-
409-
alertConfigResp, err := d.client.GetAlertConfigs(ctx, instanceId, projectId).Execute()
410-
if err != nil {
411-
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Calling API to get alert config: %v", err))
412-
return
413-
}
414-
415403
// Map response body to schema
416404
err = mapFields(ctx, instanceResp, &model)
417405
if err != nil {
@@ -426,6 +414,12 @@ func (d *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques
426414
return
427415
}
428416

417+
plan, err := loadPlanId(ctx, *d.client, &model)
418+
if err != nil {
419+
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Loading service plan: %v", err))
420+
return
421+
}
422+
429423
err = mapACLField(aclListResp, &model)
430424
if err != nil {
431425
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Processing API response for the ACL: %v", err))
@@ -439,31 +433,48 @@ func (d *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques
439433
return
440434
}
441435

442-
// Map response body to schema
443-
err = mapMetricsRetentionField(metricsRetentionResp, &model)
444-
if err != nil {
445-
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Processing API response for the metrics retention: %v", err))
446-
return
436+
// There are some plans which does not offer storage e.g. like Observability-Metrics-Endpoint-100k-EU01
437+
if plan.GetLogsStorage() != 0 && plan.GetTracesStorage() != 0 {
438+
metricsRetentionResp, err := d.client.GetMetricsStorageRetention(ctx, instanceId, projectId).Execute()
439+
if err != nil {
440+
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Calling API to get metrics retention: %v", err))
441+
return
442+
}
443+
// Map response body to schema
444+
err = mapMetricsRetentionField(metricsRetentionResp, &model)
445+
if err != nil {
446+
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Processing API response for the metrics retention: %v", err))
447+
return
448+
}
449+
// Set state to fully populated data
450+
diags := setMetricsRetentions(ctx, &resp.State, &model)
451+
resp.Diagnostics.Append(diags...)
452+
if resp.Diagnostics.HasError() {
453+
return
454+
}
447455
}
448456

449-
// Set state to fully populated data
450-
diags = setMetricsRetentions(ctx, &resp.State, &model)
451-
resp.Diagnostics.Append(diags...)
452-
if resp.Diagnostics.HasError() {
453-
return
454-
}
457+
// There are plans where no alert matchers and receivers are present e.g. like Observability-Metrics-Endpoint-100k-EU01
458+
if plan.GetAlertMatchers() != 0 && plan.GetAlertReceivers() != 0 {
459+
alertConfigResp, err := d.client.GetAlertConfigs(ctx, instanceId, projectId).Execute()
460+
if err != nil {
461+
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Calling API to get alert config: %v", err))
462+
return
463+
}
464+
// Map response body to schema
465+
err = mapAlertConfigField(ctx, alertConfigResp, &model)
466+
if err != nil {
467+
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Processing API response for the alert config: %v", err))
468+
return
469+
}
455470

456-
err = mapAlertConfigField(ctx, alertConfigResp, &model)
457-
if err != nil {
458-
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Processing API response for the alert config: %v", err))
459-
return
471+
// Set state to fully populated data
472+
diags = setAlertConfig(ctx, &resp.State, &model)
473+
resp.Diagnostics.Append(diags...)
474+
if resp.Diagnostics.HasError() {
475+
return
476+
}
460477
}
461478

462-
// Set state to fully populated data
463-
diags = setAlertConfig(ctx, &resp.State, &model)
464-
resp.Diagnostics.Append(diags...)
465-
if resp.Diagnostics.HasError() {
466-
return
467-
}
468479
tflog.Info(ctx, "Observability instance read")
469480
}

0 commit comments

Comments
 (0)