@@ -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
164167func 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
253254func 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
395390func 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-
492461func 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 }
0 commit comments