@@ -2,6 +2,7 @@ package cloud_test
22
33import (
44 "fmt"
5+ "regexp"
56 "strings"
67 "time"
78
@@ -25,29 +26,47 @@ func TestResourceStack_Basic(t *testing.T) {
2526 resourceName := GetRandomStackName (prefix )
2627 stackDescription := "This is a test stack"
2728
29+ firstStepChecks := resource .ComposeTestCheckFunc (
30+ testAccStackCheckExists ("grafana_cloud_stack.test" , & stack ),
31+ resource .TestMatchResourceAttr ("grafana_cloud_stack.test" , "id" , common .IDRegexp ),
32+ resource .TestCheckResourceAttr ("grafana_cloud_stack.test" , "name" , resourceName ),
33+ resource .TestCheckResourceAttr ("grafana_cloud_stack.test" , "slug" , resourceName ),
34+ resource .TestCheckResourceAttr ("grafana_cloud_stack.test" , "status" , "active" ),
35+ resource .TestCheckResourceAttr ("grafana_cloud_stack.test" , "prometheus_remote_endpoint" , "https://prometheus-prod-01-eu-west-0.grafana.net/api/prom" ),
36+ resource .TestCheckResourceAttr ("grafana_cloud_stack.test" , "prometheus_remote_write_endpoint" , "https://prometheus-prod-01-eu-west-0.grafana.net/api/prom/push" ),
37+ resource .TestCheckResourceAttrSet ("grafana_cloud_stack.test" , "prometheus_user_id" ),
38+ resource .TestCheckResourceAttrSet ("grafana_cloud_stack.test" , "alertmanager_user_id" ),
39+ resource .TestCheckResourceAttrSet ("grafana_cloud_stack.test" , "logs_user_id" ),
40+ resource .TestCheckResourceAttrSet ("grafana_cloud_stack.test" , "traces_user_id" ),
41+ resource .TestCheckResourceAttrSet ("grafana_cloud_stack.test" , "graphite_user_id" ),
42+ )
43+
2844 resource .ParallelTest (t , resource.TestCase {
2945 PreCheck : func () {
3046 testAccDeleteExistingStacks (t , prefix )
3147 },
3248 ProviderFactories : testutils .ProviderFactories ,
3349 CheckDestroy : testAccStackCheckDestroy (& stack ),
3450 Steps : []resource.TestStep {
51+ // Create a basic stack
3552 {
3653 Config : testAccStackConfigBasic (resourceName , resourceName ),
37- Check : resource .ComposeTestCheckFunc (
38- testAccStackCheckExists ("grafana_cloud_stack.test" , & stack ),
39- resource .TestMatchResourceAttr ("grafana_cloud_stack.test" , "id" , common .IDRegexp ),
40- resource .TestCheckResourceAttr ("grafana_cloud_stack.test" , "name" , resourceName ),
41- resource .TestCheckResourceAttr ("grafana_cloud_stack.test" , "slug" , resourceName ),
42- resource .TestCheckResourceAttr ("grafana_cloud_stack.test" , "status" , "active" ),
43- resource .TestCheckResourceAttr ("grafana_cloud_stack.test" , "prometheus_remote_endpoint" , "https://prometheus-prod-01-eu-west-0.grafana.net/api/prom" ),
44- resource .TestCheckResourceAttr ("grafana_cloud_stack.test" , "prometheus_remote_write_endpoint" , "https://prometheus-prod-01-eu-west-0.grafana.net/api/prom/push" ),
45- resource .TestCheckResourceAttrSet ("grafana_cloud_stack.test" , "prometheus_user_id" ),
46- resource .TestCheckResourceAttrSet ("grafana_cloud_stack.test" , "alertmanager_user_id" ),
47- resource .TestCheckResourceAttrSet ("grafana_cloud_stack.test" , "logs_user_id" ),
48- resource .TestCheckResourceAttrSet ("grafana_cloud_stack.test" , "traces_user_id" ),
49- resource .TestCheckResourceAttrSet ("grafana_cloud_stack.test" , "graphite_user_id" ),
50- ),
54+ Check : firstStepChecks ,
55+ },
56+ // Check that we can't takeover a stack without importing it
57+ // The retrying logic for creation is very permissive,
58+ // but it shouldn't allow to apply an already existing stack on a new resource
59+ {
60+ Config : testAccStackConfigBasic (resourceName , resourceName ) +
61+ testAccStackConfigBasicWithCustomResourceName (resourceName , resourceName , "test2" ), // new stack with same name/slug
62+ ExpectError : regexp .MustCompile (fmt .Sprintf (".*a stack with the name '%s' already exists.*" , resourceName )),
63+ },
64+ // Test that the stack is correctly recreated if it's tainted and reapplied
65+ // This is a special case because stack deletion is asynchronous
66+ {
67+ Config : testAccStackConfigBasic (resourceName , resourceName ),
68+ Check : firstStepChecks ,
69+ Taint : []string {"grafana_cloud_stack.test" },
5170 },
5271 {
5372 // Delete the stack outside of the test and make sure it is recreated
@@ -65,6 +84,7 @@ func TestResourceStack_Basic(t *testing.T) {
6584 resource .TestCheckResourceAttr ("grafana_cloud_stack.test" , "status" , "active" ),
6685 ),
6786 },
87+ // Update the stack
6888 {
6989 Config : testAccStackConfigUpdate (resourceName + "new" , resourceName , stackDescription ),
7090 Check : resource .ComposeTestCheckFunc (
@@ -150,13 +170,17 @@ func testAccStackCheckDestroy(a *gapi.Stack) resource.TestCheckFunc {
150170}
151171
152172func testAccStackConfigBasic (name string , slug string ) string {
173+ return testAccStackConfigBasicWithCustomResourceName (name , slug , "test" )
174+ }
175+
176+ func testAccStackConfigBasicWithCustomResourceName (name string , slug string , resourceName string ) string {
153177 return fmt .Sprintf (`
154- resource "grafana_cloud_stack" "test " {
178+ resource "grafana_cloud_stack" "%s " {
155179 name = "%s"
156180 slug = "%s"
157181 region_slug = "eu"
158182 }
159- ` , name , slug )
183+ ` , resourceName , name , slug )
160184}
161185
162186func testAccStackConfigUpdate (name string , slug string , description string ) string {
0 commit comments