@@ -2,6 +2,7 @@ package cockpit_test
22
33import (
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+
169231func 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