Skip to content

Commit 150fd72

Browse files
committed
fix(cockpit): handle permission errors gracefully in alert manager read
1 parent 8953152 commit 150fd72

File tree

1 file changed

+40
-37
lines changed

1 file changed

+40
-37
lines changed

internal/services/cockpit/alert_manager.go

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -165,57 +165,60 @@ func ResourceCockpitAlertManagerRead(ctx context.Context, d *schema.ResourceData
165165
_ = d.Set("project_id", projectID)
166166

167167
// Get enabled preconfigured alerts and separate user-requested vs API-default alerts
168+
// Handle permission errors gracefully to allow the resource to work without cockpit read permissions
169+
var userRequestedIDs []string
170+
var defaultEnabledIDs []string
171+
168172
alerts, err := api.ListAlerts(&cockpit.RegionalAPIListAlertsRequest{
169173
Region: region,
170174
ProjectID: projectID,
171175
IsPreconfigured: scw.BoolPtr(true),
172176
}, scw.WithContext(ctx), scw.WithAllPages())
173177
if err != nil {
174-
return diag.FromErr(err)
175-
}
176-
177-
// Build a map of alert statuses
178-
alertStatusMap := make(map[string]cockpit.AlertStatus)
179-
for _, alert := range alerts.Alerts {
180-
if alert.PreconfiguredData != nil && alert.PreconfiguredData.PreconfiguredRuleID != "" {
181-
alertStatusMap[alert.PreconfiguredData.PreconfiguredRuleID] = alert.RuleStatus
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+
}
182189
}
183-
}
184-
185-
// Separate user-requested alerts from API-default alerts
186-
var userRequestedIDs []string
187-
var defaultEnabledIDs []string
188190

189-
if v, ok := d.GetOk("preconfigured_alert_ids"); ok {
190-
requestedIDs := expandStringSet(v.(*schema.Set))
191-
requestedMap := make(map[string]bool)
192-
for _, id := range requestedIDs {
193-
requestedMap[id] = true
194-
}
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+
}
195197

196-
// Check all enabled/enabling alerts
197-
for ruleID, status := range alertStatusMap {
198-
if status == cockpit.AlertStatusEnabled || status == cockpit.AlertStatusEnabling {
199-
if requestedMap[ruleID] {
200-
// This alert was explicitly requested by the user
201-
userRequestedIDs = append(userRequestedIDs, ruleID)
202-
} else {
203-
// This alert was enabled automatically by the API
204-
defaultEnabledIDs = append(defaultEnabledIDs, ruleID)
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+
}
205208
}
206209
}
207-
}
208-
} else {
209-
// No alerts explicitly requested, all enabled alerts are API defaults
210-
for ruleID, status := range alertStatusMap {
211-
if status == cockpit.AlertStatusEnabled || status == cockpit.AlertStatusEnabling {
212-
defaultEnabledIDs = append(defaultEnabledIDs, ruleID)
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 {
214+
defaultEnabledIDs = append(defaultEnabledIDs, ruleID)
215+
}
213216
}
214217
}
215-
}
216218

217-
_ = d.Set("preconfigured_alert_ids", userRequestedIDs)
218-
_ = d.Set("default_preconfigured_alert_ids", defaultEnabledIDs)
219+
_ = d.Set("preconfigured_alert_ids", userRequestedIDs)
220+
_ = d.Set("default_preconfigured_alert_ids", defaultEnabledIDs)
221+
}
219222

220223
contactPoints, err := api.ListContactPoints(&cockpit.RegionalAPIListContactPointsRequest{
221224
Region: region,

0 commit comments

Comments
 (0)