Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion internal/services/cockpit/alert_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ func ResourceCockpitAlertManagerCreate(ctx context.Context, d *schema.ResourceDa
ProjectID: projectID,
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
// If alert manager is already enabled, ignore the 409 error
if !httperrors.Is409(err) {
return diag.FromErr(err)
}
}

if shouldEnableLegacyManagedAlerts(d) {
Expand Down Expand Up @@ -238,6 +241,10 @@ func ResourceCockpitAlertManagerRead(ctx context.Context, d *schema.ResourceData
ProjectID: projectID,
}, scw.WithContext(ctx))
if err != nil {
if httperrors.Is403(err) {
return nil
}

return diag.FromErr(err)
}

Expand Down
57 changes: 49 additions & 8 deletions internal/services/cockpit/alert_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,12 @@ func TestAccCockpitAlertManager_IDHandling(t *testing.T) {
Steps: []resource.TestStep{
{
Config: `
resource "scaleway_account_project" "project" {
name = "tf_tests_cockpit_alert_manager_id"
}

resource "scaleway_cockpit_alert_manager" "main" {
project_id = scaleway_account_project.project.id
contact_points {
email = "test@example.com"
}
Expand All @@ -187,7 +192,12 @@ func TestAccCockpitAlertManager_IDHandling(t *testing.T) {
},
{
Config: `
resource "scaleway_account_project" "project" {
name = "tf_tests_cockpit_alert_manager_id"
}

resource "scaleway_cockpit_alert_manager" "main" {
project_id = scaleway_account_project.project.id
contact_points {
email = "updated@example.com"
}
Expand Down Expand Up @@ -219,7 +229,12 @@ func testAccCockpitAlertManagerConfigWithContacts(contactPoints []map[string]str
contactsConfig += contactsConfigSb230.String()

return fmt.Sprintf(`
resource "scaleway_account_project" "project" {
name = "tf_tests_cockpit_alert_manager"
}

resource "scaleway_cockpit_alert_manager" "alert_manager" {
project_id = scaleway_account_project.project.id
%s
}
`, contactsConfig)
Expand Down Expand Up @@ -254,28 +269,34 @@ func testAccCheckCockpitContactPointExists(tt *acctest.TestTools, resourceName s

func testAccCockpitAlertManagerAndContactsDestroy(tt *acctest.TestTools) resource.TestCheckFunc {
return func(state *terraform.State) error {
cockpitAPI := cockpit.NewRegionalAPI(meta.ExtractScwClient(tt.Meta))
region := scw.RegionFrPar

for _, rs := range state.RootModule().Resources {
if rs.Type != "scaleway_cockpit_alert_manager" {
continue
}

api := cockpit.NewRegionalAPI(meta.ExtractScwClient(tt.Meta))
projectID := rs.Primary.Attributes["project_id"]
region := scw.RegionFrPar
alertManager, err := api.GetAlertManager(&cockpit.RegionalAPIGetAlertManagerRequest{
if projectID == "" {
continue
}

alertManager, err := cockpitAPI.GetAlertManager(&cockpit.RegionalAPIGetAlertManagerRequest{
Region: region,
ProjectID: projectID,
})

if !httperrors.Is404(err) && !httperrors.Is403(err) {
return err
if httperrors.Is404(err) || httperrors.Is403(err) {
// Project deleted or no permissions, consider it cleaned up
return nil
}

if alertManager == nil {
return nil
if err != nil {
return err
}

if alertManager.AlertManagerEnabled {
if alertManager != nil && alertManager.AlertManagerEnabled {
return errors.New("cockpit alert manager (" + rs.Primary.ID + ") is still enabled")
}
}
Expand Down Expand Up @@ -341,7 +362,12 @@ func TestAccCockpitAlertManager_WithPreconfiguredAlerts(t *testing.T) {
Steps: []resource.TestStep{
{
Config: `
resource "scaleway_account_project" "project" {
name = "tf_tests_cockpit_alert_manager_preconfigured"
}

resource "scaleway_cockpit_alert_manager" "main" {
project_id = scaleway_account_project.project.id
# Enable 2 specific preconfigured alerts (stable IDs)
preconfigured_alert_ids = [
"6c6843af-1815-46df-9e52-6feafcf31fd7", # PostgreSQL Too Many Connections
Expand Down Expand Up @@ -377,7 +403,12 @@ func TestAccCockpitAlertManager_UpdatePreconfiguredAlerts(t *testing.T) {
Steps: []resource.TestStep{
{
Config: `
resource "scaleway_account_project" "project" {
name = "tf_tests_cockpit_alert_manager_update_preconfigured"
}

resource "scaleway_cockpit_alert_manager" "main" {
project_id = scaleway_account_project.project.id
# Enable a specific PostgreSQL alert (stable ID)
preconfigured_alert_ids = [
"6c6843af-1815-46df-9e52-6feafcf31fd7" # PostgreSQL Too Many Connections
Expand All @@ -397,7 +428,12 @@ func TestAccCockpitAlertManager_UpdatePreconfiguredAlerts(t *testing.T) {
},
{
Config: `
resource "scaleway_account_project" "project" {
name = "tf_tests_cockpit_alert_manager_update_preconfigured"
}

resource "scaleway_cockpit_alert_manager" "main" {
project_id = scaleway_account_project.project.id
# Enable 2 specific alerts (stable IDs)
preconfigured_alert_ids = [
"6c6843af-1815-46df-9e52-6feafcf31fd7", # PostgreSQL Too Many Connections
Expand All @@ -418,7 +454,12 @@ func TestAccCockpitAlertManager_UpdatePreconfiguredAlerts(t *testing.T) {
},
{
Config: `
resource "scaleway_account_project" "project" {
name = "tf_tests_cockpit_alert_manager_update_preconfigured"
}

resource "scaleway_cockpit_alert_manager" "main" {
project_id = scaleway_account_project.project.id
# Disable all
preconfigured_alert_ids = []

Expand Down
Loading
Loading