Skip to content

Commit ed86e5e

Browse files
committed
fix(cockpit): fix alert manager ID parsing
1 parent b583ad3 commit ed86e5e

File tree

3 files changed

+1437
-3
lines changed

3 files changed

+1437
-3
lines changed

internal/services/cockpit/alert_manager.go

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"strings"
78

89
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
910
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -123,7 +124,14 @@ func ResourceCockpitAlertManagerRead(ctx context.Context, d *schema.ResourceData
123124
return diag.FromErr(err)
124125
}
125126

126-
projectID := d.Get("project_id").(string)
127+
// Parse the ID to get projectID
128+
parsedRegion, projectID, err := ResourceCockpitAlertManagerParseID(d.Id())
129+
if err != nil {
130+
return diag.FromErr(err)
131+
}
132+
133+
// Use the parsed region instead of the one from the schema
134+
region = parsedRegion
127135

128136
alertManager, err := api.GetAlertManager(&cockpit.RegionalAPIGetAlertManagerRequest{
129137
Region: region,
@@ -136,6 +144,7 @@ func ResourceCockpitAlertManagerRead(ctx context.Context, d *schema.ResourceData
136144
_ = d.Set("enable_managed_alerts", alertManager.ManagedAlertsEnabled)
137145
_ = d.Set("region", alertManager.Region)
138146
_ = d.Set("alert_manager_url", alertManager.AlertManagerURL)
147+
_ = d.Set("project_id", projectID)
139148

140149
contactPoints, err := api.ListContactPoints(&cockpit.RegionalAPIListContactPointsRequest{
141150
Region: region,
@@ -167,7 +176,14 @@ func ResourceCockpitAlertManagerUpdate(ctx context.Context, d *schema.ResourceDa
167176
return diag.FromErr(err)
168177
}
169178

170-
projectID := d.Get("project_id").(string)
179+
// Parse the ID to get projectID
180+
parsedRegion, projectID, err := ResourceCockpitAlertManagerParseID(d.Id())
181+
if err != nil {
182+
return diag.FromErr(err)
183+
}
184+
185+
// Use the parsed region instead of the one from the schema
186+
region = parsedRegion
171187

172188
if d.HasChange("enable_managed_alerts") {
173189
enable := d.Get("enable_managed_alerts").(bool)
@@ -247,7 +263,14 @@ func ResourceCockpitAlertManagerDelete(ctx context.Context, d *schema.ResourceDa
247263
return diag.FromErr(err)
248264
}
249265

250-
projectID := d.Get("project_id").(string)
266+
// Parse the ID to get projectID
267+
parsedRegion, projectID, err := ResourceCockpitAlertManagerParseID(d.Id())
268+
if err != nil {
269+
return diag.FromErr(err)
270+
}
271+
272+
// Use the parsed region instead of the one from the schema
273+
region = parsedRegion
251274

252275
contactPoints, err := api.ListContactPoints(&cockpit.RegionalAPIListContactPointsRequest{
253276
Region: region,
@@ -291,6 +314,19 @@ func ResourceCockpitAlertManagerDelete(ctx context.Context, d *schema.ResourceDa
291314
return nil
292315
}
293316

317+
// ResourceCockpitAlertManagerID builds the resource identifier
318+
// The resource identifier format is "Region/ProjectID/1"
294319
func ResourceCockpitAlertManagerID(region scw.Region, projectID string) (resourceID string) {
295320
return fmt.Sprintf("%s/%s/1", region, projectID)
296321
}
322+
323+
// ResourceCockpitAlertManagerParseID extracts region and project ID from the resource identifier.
324+
// The resource identifier format is "Region/ProjectID/1"
325+
func ResourceCockpitAlertManagerParseID(resourceID string) (region scw.Region, projectID string, err error) {
326+
parts := strings.Split(resourceID, "/")
327+
if len(parts) != 3 {
328+
return "", "", fmt.Errorf("invalid alert manager ID format: %s", resourceID)
329+
}
330+
331+
return scw.Region(parts[0]), parts[1], nil
332+
}

internal/services/cockpit/alert_manager_test.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cockpit_test
22

33
import (
44
"fmt"
5+
"strings"
56
"testing"
67

78
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
@@ -166,6 +167,67 @@ func TestAccCockpitAlertManager_EnableDisable(t *testing.T) {
166167
})
167168
}
168169

170+
func TestAccCockpitAlertManager_IDHandling(t *testing.T) {
171+
tt := acctest.NewTestTools(t)
172+
defer tt.Cleanup()
173+
174+
resource.ParallelTest(t, resource.TestCase{
175+
PreCheck: func() { acctest.PreCheck(t) },
176+
ProviderFactories: tt.ProviderFactories,
177+
CheckDestroy: testAccCockpitAlertManagerAndContactsDestroy(tt),
178+
Steps: []resource.TestStep{
179+
{
180+
Config: `
181+
resource "scaleway_account_project" "project" {
182+
name = "tf_test_cockpit_alert_manager_id"
183+
}
184+
185+
resource "scaleway_cockpit_alert_manager" "main" {
186+
project_id = scaleway_account_project.project.id
187+
enable_managed_alerts = true
188+
189+
contact_points {
190+
191+
}
192+
}
193+
`,
194+
Check: resource.ComposeTestCheckFunc(
195+
resource.TestCheckResourceAttrSet("scaleway_cockpit_alert_manager.main", "id"),
196+
resource.TestCheckResourceAttrSet("scaleway_cockpit_alert_manager.main", "project_id"),
197+
resource.TestCheckResourceAttrSet("scaleway_cockpit_alert_manager.main", "region"),
198+
resource.TestCheckResourceAttr("scaleway_cockpit_alert_manager.main", "enable_managed_alerts", "true"),
199+
resource.TestCheckResourceAttrSet("scaleway_cockpit_alert_manager.main", "alert_manager_url"),
200+
resource.TestCheckResourceAttr("scaleway_cockpit_alert_manager.main", "contact_points.0.email", "[email protected]"),
201+
testAccCheckAlertManagerIDFormat(tt, "scaleway_cockpit_alert_manager.main"),
202+
),
203+
},
204+
{
205+
Config: `
206+
resource "scaleway_account_project" "project" {
207+
name = "tf_test_cockpit_alert_manager_id"
208+
}
209+
210+
resource "scaleway_cockpit_alert_manager" "main" {
211+
project_id = scaleway_account_project.project.id
212+
enable_managed_alerts = true
213+
214+
contact_points {
215+
216+
}
217+
}
218+
`,
219+
Check: resource.ComposeTestCheckFunc(
220+
resource.TestCheckResourceAttrSet("scaleway_cockpit_alert_manager.main", "id"),
221+
resource.TestCheckResourceAttrSet("scaleway_cockpit_alert_manager.main", "project_id"),
222+
resource.TestCheckResourceAttrSet("scaleway_cockpit_alert_manager.main", "region"),
223+
resource.TestCheckResourceAttr("scaleway_cockpit_alert_manager.main", "contact_points.0.email", "[email protected]"),
224+
testAccCheckAlertManagerIDFormat(tt, "scaleway_cockpit_alert_manager.main"),
225+
),
226+
},
227+
},
228+
})
229+
}
230+
169231
func testAccCockpitAlertManagerConfigWithContacts(contactPoints []map[string]string) string {
170232
contactsConfig := ""
171233
for _, contact := range contactPoints {
@@ -284,3 +346,50 @@ func testAccCockpitAlertManagerAndContactsDestroy(tt *acctest.TestTools) resourc
284346
return nil
285347
}
286348
}
349+
350+
// testAccCheckAlertManagerIDFormat verifies the ID format
351+
func testAccCheckAlertManagerIDFormat(tt *acctest.TestTools, resourceName string) resource.TestCheckFunc {
352+
return func(s *terraform.State) error {
353+
rs, ok := s.RootModule().Resources[resourceName]
354+
if !ok {
355+
return fmt.Errorf("alert manager not found: %s", resourceName)
356+
}
357+
358+
id := rs.Primary.ID
359+
if id == "" {
360+
return fmt.Errorf("alert manager ID is empty")
361+
}
362+
363+
parts := strings.Split(id, "/")
364+
if len(parts) != 3 {
365+
return fmt.Errorf("alert manager ID should have 3 parts, got %d: %s", len(parts), id)
366+
}
367+
368+
region := parts[0]
369+
projectID := parts[1]
370+
371+
if region == "" {
372+
return fmt.Errorf("region part of ID is empty")
373+
}
374+
375+
if projectID == "" {
376+
return fmt.Errorf("project ID part of ID is empty")
377+
}
378+
379+
if parts[2] != "1" {
380+
return fmt.Errorf("third part of ID should be '1', got %s", parts[2])
381+
}
382+
383+
expectedProjectID := rs.Primary.Attributes["project_id"]
384+
if expectedProjectID != projectID {
385+
return fmt.Errorf("project_id in attributes (%s) doesn't match project_id in ID (%s)", expectedProjectID, projectID)
386+
}
387+
388+
expectedRegion := rs.Primary.Attributes["region"]
389+
if expectedRegion != region {
390+
return fmt.Errorf("region in attributes (%s) doesn't match region in ID (%s)", expectedRegion, region)
391+
}
392+
393+
return nil
394+
}
395+
}

0 commit comments

Comments
 (0)