Skip to content

Commit 07e1968

Browse files
committed
feat: fix alert_manager to use proper ID parsing helpers
1 parent 2c3a10a commit 07e1968

File tree

3 files changed

+34
-58
lines changed

3 files changed

+34
-58
lines changed

internal/services/cockpit/alert_manager.go

Lines changed: 20 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ func ResourceCockpitAlertManagerCreate(ctx context.Context, d *schema.ResourceDa
8383
_ = d.Set("project_id", projectID)
8484
}
8585

86-
contactPoints := d.Get("contact_points").([]any)
86+
contactPoints, _ := d.Get("contact_points").([]any)
87+
if contactPoints == nil {
88+
contactPoints = []any{}
89+
}
8790

8891
_, err = api.EnableAlertManager(&cockpit.RegionalAPIEnableAlertManagerRequest{
8992
Region: region,
@@ -105,7 +108,7 @@ func ResourceCockpitAlertManagerCreate(ctx context.Context, d *schema.ResourceDa
105108

106109
// Handle preconfigured alerts
107110
if v, ok := d.GetOk("preconfigured_alert_ids"); ok {
108-
alertIDs := expandStringSet(v.(*schema.Set))
111+
alertIDs := types.ExpandStrings(v.(*schema.Set).List())
109112
if len(alertIDs) > 0 {
110113
_, err = api.EnableAlertRules(&cockpit.RegionalAPIEnableAlertRulesRequest{
111114
Region: region,
@@ -162,13 +165,7 @@ func ResourceCockpitAlertManagerCreate(ctx context.Context, d *schema.ResourceDa
162165
}
163166

164167
func ResourceCockpitAlertManagerRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
165-
api, region, err := cockpitAPIWithRegion(d, meta)
166-
if err != nil {
167-
return diag.FromErr(err)
168-
}
169-
170-
// Parse the ID to get projectID
171-
_, projectID, err := ResourceCockpitAlertManagerParseID(d.Id())
168+
api, region, projectID, err := NewAPIWithRegionAndProjectID(meta, d.Id())
172169
if err != nil {
173170
return diag.FromErr(err)
174171
}
@@ -178,6 +175,10 @@ func ResourceCockpitAlertManagerRead(ctx context.Context, d *schema.ResourceData
178175
ProjectID: projectID,
179176
}, scw.WithContext(ctx))
180177
if err != nil {
178+
if httperrors.Is404(err) {
179+
d.SetId("")
180+
return nil
181+
}
181182
return diag.FromErr(err)
182183
}
183184

@@ -208,7 +209,7 @@ func ResourceCockpitAlertManagerRead(ctx context.Context, d *schema.ResourceData
208209
}
209210

210211
if v, ok := d.GetOk("preconfigured_alert_ids"); ok {
211-
requestedIDs := expandStringSet(v.(*schema.Set))
212+
requestedIDs := types.ExpandStrings(v.(*schema.Set).List())
212213
requestedMap := make(map[string]bool)
213214

214215
for _, id := range requestedIDs {
@@ -251,13 +252,7 @@ func ResourceCockpitAlertManagerRead(ctx context.Context, d *schema.ResourceData
251252
}
252253

253254
func ResourceCockpitAlertManagerUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
254-
api, region, err := cockpitAPIWithRegion(d, meta)
255-
if err != nil {
256-
return diag.FromErr(err)
257-
}
258-
259-
// Parse the ID to get projectID
260-
_, projectID, err := ResourceCockpitAlertManagerParseID(d.Id())
255+
api, region, projectID, err := NewAPIWithRegionAndProjectID(meta, d.Id())
261256
if err != nil {
262257
return diag.FromErr(err)
263258
}
@@ -268,7 +263,7 @@ func ResourceCockpitAlertManagerUpdate(ctx context.Context, d *schema.ResourceDa
268263
newSet := newIDs.(*schema.Set)
269264

270265
// IDs to disable: in old but not in new
271-
toDisable := expandStringSet(oldSet.Difference(newSet))
266+
toDisable := types.ExpandStrings(oldSet.Difference(newSet).List())
272267
if len(toDisable) > 0 {
273268
_, err = api.DisableAlertRules(&cockpit.RegionalAPIDisableAlertRulesRequest{
274269
Region: region,
@@ -292,7 +287,7 @@ func ResourceCockpitAlertManagerUpdate(ctx context.Context, d *schema.ResourceDa
292287
}
293288

294289
// IDs to enable: in new but not in old
295-
toEnable := expandStringSet(newSet.Difference(oldSet))
290+
toEnable := types.ExpandStrings(newSet.Difference(oldSet).List())
296291
if len(toEnable) > 0 {
297292
_, err = api.EnableAlertRules(&cockpit.RegionalAPIEnableAlertRulesRequest{
298293
Region: region,
@@ -393,20 +388,14 @@ func ResourceCockpitAlertManagerUpdate(ctx context.Context, d *schema.ResourceDa
393388
}
394389

395390
func ResourceCockpitAlertManagerDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
396-
api, region, err := cockpitAPIWithRegion(d, meta)
397-
if err != nil {
398-
return diag.FromErr(err)
399-
}
400-
401-
// Parse the ID to get projectID
402-
_, projectID, err := ResourceCockpitAlertManagerParseID(d.Id())
391+
api, region, projectID, err := NewAPIWithRegionAndProjectID(meta, d.Id())
403392
if err != nil {
404393
return diag.FromErr(err)
405394
}
406395

407396
// Disable all preconfigured alerts if any are enabled
408397
if v, ok := d.GetOk("preconfigured_alert_ids"); ok {
409-
alertIDs := expandStringSet(v.(*schema.Set))
398+
alertIDs := types.ExpandStrings(v.(*schema.Set).List())
410399
if len(alertIDs) > 0 {
411400
_, err = api.DisableAlertRules(&cockpit.RegionalAPIDisableAlertRulesRequest{
412401
Region: region,
@@ -469,26 +458,6 @@ func ResourceCockpitAlertManagerID(region scw.Region, projectID string) (resourc
469458
return fmt.Sprintf("%s/%s/1", region, projectID)
470459
}
471460

472-
// ResourceCockpitAlertManagerParseID extracts region and project ID from the resource identifier.
473-
// The resource identifier format is "Region/ProjectID/1"
474-
func ResourceCockpitAlertManagerParseID(resourceID string) (region scw.Region, projectID string, err error) {
475-
parts := strings.Split(resourceID, "/")
476-
if len(parts) != 3 {
477-
return "", "", fmt.Errorf("invalid alert manager ID format: %s", resourceID)
478-
}
479-
480-
return scw.Region(parts[0]), parts[1], nil
481-
}
482-
483-
func expandStringSet(set *schema.Set) []string {
484-
result := make([]string, set.Len())
485-
for i, v := range set.List() {
486-
result[i] = v.(string)
487-
}
488-
489-
return result
490-
}
491-
492461
func shouldEnableLegacyManagedAlerts(d *schema.ResourceData) bool {
493462
if !d.Get("enable_managed_alerts").(bool) {
494463
return false
@@ -510,23 +479,17 @@ func diffSuppressPreconfiguredAlertIDs(k, _, _ string, d *schema.ResourceData) b
510479
var oldList, newList []string
511480

512481
if oldSetTyped, ok := oldSet.(*schema.Set); ok {
513-
oldList = expandStringSet(oldSetTyped)
482+
oldList = types.ExpandStrings(oldSetTyped.List())
514483
} else if oldListAny, ok := oldSet.([]any); ok {
515-
oldList = make([]string, len(oldListAny))
516-
for i, v := range oldListAny {
517-
oldList[i] = v.(string)
518-
}
484+
oldList = types.ExpandStrings(oldListAny)
519485
} else {
520486
return false
521487
}
522488

523489
if newSetTyped, ok := newSet.(*schema.Set); ok {
524-
newList = expandStringSet(newSetTyped)
490+
newList = types.ExpandStrings(newSetTyped.List())
525491
} else if newListAny, ok := newSet.([]any); ok {
526-
newList = make([]string, len(newListAny))
527-
for i, v := range newListAny {
528-
newList[i] = v.(string)
529-
}
492+
newList = types.ExpandStrings(newListAny)
530493
} else {
531494
return false
532495
}

internal/services/cockpit/alert_manager_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ func testAccCheckPreconfiguredAlertsCount(tt *acctest.TestTools, resourceName st
454454

455455
userRequestedIDs := make(map[string]bool)
456456

457-
for i := 0; i < actualCount; i++ {
457+
for i := range actualCount {
458458
alertID := rs.Primary.Attributes[fmt.Sprintf("preconfigured_alert_ids.%d", i)]
459459
if alertID != "" {
460460
userRequestedIDs[alertID] = true

internal/services/cockpit/helpers_cockpit.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@ func NewAPIWithRegionAndID(m any, id string) (*cockpit.RegionalAPI, scw.Region,
5555
return api, region, id, nil
5656
}
5757

58+
// NewAPIWithRegionAndProjectID returns a new cockpit API with region and project ID extracted from composite ID.
59+
// The ID format is "region/projectID/1" (used by alert_manager resource).
60+
func NewAPIWithRegionAndProjectID(m any, id string) (*cockpit.RegionalAPI, scw.Region, string, error) {
61+
api := cockpit.NewRegionalAPI(meta.ExtractScwClient(m))
62+
63+
parts := strings.Split(id, "/")
64+
if len(parts) != 3 {
65+
return nil, "", "", fmt.Errorf("invalid alert manager ID format: %s, expected region/projectID/1", id)
66+
}
67+
68+
return api, scw.Region(parts[0]), parts[1], nil
69+
}
70+
5871
// NewAPIGrafanaUserID returns a new cockpit API with the Grafana user ID and the project ID.
5972
func NewAPIGrafanaUserID(m any, id string) (*cockpit.GlobalAPI, string, uint32, error) {
6073
projectID, resourceIDString, err := parseCockpitID(id)

0 commit comments

Comments
 (0)