@@ -77,6 +77,13 @@ func ResourceCockpitAlertManagerCreate(ctx context.Context, d *schema.ResourceDa
7777 }
7878
7979 projectID := d .Get ("project_id" ).(string )
80+ if projectID == "" {
81+ projectID , err = getDefaultProjectID (ctx , meta )
82+ if err != nil {
83+ return diag .FromErr (err )
84+ }
85+ _ = d .Set ("project_id" , projectID )
86+ }
8087 contactPoints := d .Get ("contact_points" ).([]any )
8188
8289 _ , err = api .EnableAlertManager (& cockpit.RegionalAPIEnableAlertManagerRequest {
@@ -100,8 +107,16 @@ func ResourceCockpitAlertManagerCreate(ctx context.Context, d *schema.ResourceDa
100107 return diag .FromErr (err )
101108 }
102109
103- // Note: Waiting for alerts to be enabled will be handled by SDK waiters when available
104- // For now, we continue without waiting as the Read function handles enabling/enabled states
110+ // Wait for alerts to be enabled
111+ _ , err = api .WaitForPreconfiguredAlerts (& cockpit.WaitForPreconfiguredAlertsRequest {
112+ Region : region ,
113+ ProjectID : projectID ,
114+ PreconfiguredRules : alertIDs ,
115+ TargetStatus : cockpit .AlertStatusEnabled ,
116+ }, scw .WithContext (ctx ))
117+ if err != nil {
118+ return diag .FromErr (err )
119+ }
105120 }
106121 }
107122
@@ -175,51 +190,47 @@ func ResourceCockpitAlertManagerRead(ctx context.Context, d *schema.ResourceData
175190 IsPreconfigured : scw .BoolPtr (true ),
176191 }, scw .WithContext (ctx ), scw .WithAllPages ())
177192 if err != nil {
178- // If we can't read alerts (e.g., permission denied), just set empty arrays
179- // This allows the resource to work even without cockpit read permissions
180- _ = d .Set ("preconfigured_alert_ids" , userRequestedIDs )
181- _ = d .Set ("default_preconfigured_alert_ids" , defaultEnabledIDs )
182- } else {
183- // Build a map of alert statuses
184- alertStatusMap := make (map [string ]cockpit.AlertStatus )
185- for _ , alert := range alerts .Alerts {
186- if alert .PreconfiguredData != nil && alert .PreconfiguredData .PreconfiguredRuleID != "" {
187- alertStatusMap [alert .PreconfiguredData .PreconfiguredRuleID ] = alert .RuleStatus
188- }
193+ return diag .FromErr (err )
194+ }
195+ // Build a map of alert statuses
196+ alertStatusMap := make (map [string ]cockpit.AlertStatus )
197+ for _ , alert := range alerts .Alerts {
198+ if alert .PreconfiguredData != nil && alert .PreconfiguredData .PreconfiguredRuleID != "" {
199+ alertStatusMap [alert .PreconfiguredData .PreconfiguredRuleID ] = alert .RuleStatus
189200 }
201+ }
190202
191- if v , ok := d .GetOk ("preconfigured_alert_ids" ); ok {
192- requestedIDs := expandStringSet (v .(* schema.Set ))
193- requestedMap := make (map [string ]bool )
194- for _ , id := range requestedIDs {
195- requestedMap [id ] = true
196- }
203+ if v , ok := d .GetOk ("preconfigured_alert_ids" ); ok {
204+ requestedIDs := expandStringSet (v .(* schema.Set ))
205+ requestedMap := make (map [string ]bool )
206+ for _ , id := range requestedIDs {
207+ requestedMap [id ] = true
208+ }
197209
198- // Check all enabled/enabling alerts
199- for ruleID , status := range alertStatusMap {
200- if status == cockpit .AlertStatusEnabled || status == cockpit .AlertStatusEnabling {
201- if requestedMap [ruleID ] {
202- // This alert was explicitly requested by the user
203- userRequestedIDs = append (userRequestedIDs , ruleID )
204- } else {
205- // This alert was enabled automatically by the API
206- defaultEnabledIDs = append (defaultEnabledIDs , ruleID )
207- }
208- }
209- }
210- } else {
211- // No alerts explicitly requested, all enabled alerts are API defaults
212- for ruleID , status := range alertStatusMap {
213- if status == cockpit .AlertStatusEnabled || status == cockpit .AlertStatusEnabling {
210+ // Check all enabled/enabling alerts
211+ for ruleID , status := range alertStatusMap {
212+ if status == cockpit .AlertStatusEnabled || status == cockpit .AlertStatusEnabling {
213+ if requestedMap [ruleID ] {
214+ // This alert was explicitly requested by the user
215+ userRequestedIDs = append (userRequestedIDs , ruleID )
216+ } else {
217+ // This alert was enabled automatically by the API
214218 defaultEnabledIDs = append (defaultEnabledIDs , ruleID )
215219 }
216220 }
217221 }
218-
219- _ = d .Set ("preconfigured_alert_ids" , userRequestedIDs )
220- _ = d .Set ("default_preconfigured_alert_ids" , defaultEnabledIDs )
222+ } else {
223+ // No alerts explicitly requested, all enabled alerts are API defaults
224+ for ruleID , status := range alertStatusMap {
225+ if status == cockpit .AlertStatusEnabled || status == cockpit .AlertStatusEnabling {
226+ defaultEnabledIDs = append (defaultEnabledIDs , ruleID )
227+ }
228+ }
221229 }
222230
231+ _ = d .Set ("preconfigured_alert_ids" , userRequestedIDs )
232+ _ = d .Set ("default_preconfigured_alert_ids" , defaultEnabledIDs )
233+
223234 contactPoints , err := api .ListContactPoints (& cockpit.RegionalAPIListContactPointsRequest {
224235 Region : region ,
225236 ProjectID : projectID ,
@@ -273,7 +284,16 @@ func ResourceCockpitAlertManagerUpdate(ctx context.Context, d *schema.ResourceDa
273284 return diag .FromErr (err )
274285 }
275286
276- // Note: Waiting for alerts to be disabled will be handled by SDK waiters when available
287+ // Wait for alerts to be disabled
288+ _ , err = api .WaitForPreconfiguredAlerts (& cockpit.WaitForPreconfiguredAlertsRequest {
289+ Region : region ,
290+ ProjectID : projectID ,
291+ PreconfiguredRules : toDisable ,
292+ TargetStatus : cockpit .AlertStatusDisabled ,
293+ }, scw .WithContext (ctx ))
294+ if err != nil {
295+ return diag .FromErr (err )
296+ }
277297 }
278298
279299 // IDs to enable: in new but not in old
@@ -288,7 +308,16 @@ func ResourceCockpitAlertManagerUpdate(ctx context.Context, d *schema.ResourceDa
288308 return diag .FromErr (err )
289309 }
290310
291- // Note: Waiting for alerts to be enabled will be handled by SDK waiters when available
311+ // Wait for alerts to be enabled
312+ _ , err = api .WaitForPreconfiguredAlerts (& cockpit.WaitForPreconfiguredAlertsRequest {
313+ Region : region ,
314+ ProjectID : projectID ,
315+ PreconfiguredRules : toEnable ,
316+ TargetStatus : cockpit .AlertStatusEnabled ,
317+ }, scw .WithContext (ctx ))
318+ if err != nil {
319+ return diag .FromErr (err )
320+ }
292321 }
293322 }
294323
0 commit comments