88
99 gapi "github.com/grafana/grafana-api-golang-client"
1010
11+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
1112 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1213 "github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1314)
@@ -34,14 +35,14 @@ func TestAccDashboard_basic(t *testing.T) {
3435 // TODO: Make parallelizable
3536 resource .Test (t , resource.TestCase {
3637 ProviderFactories : testAccProviderFactories ,
37- CheckDestroy : testAccDashboardCheckDestroy (& dashboard ),
38+ CheckDestroy : testAccDashboardCheckDestroy (& dashboard , 0 ),
3839 Steps : []resource.TestStep {
3940 {
4041 // Test resource creation.
4142 Config : testAccExample (t , "resources/grafana_dashboard/_acc_basic.tf" ),
4243 Check : resource .ComposeTestCheckFunc (
4344 testAccDashboardCheckExists ("grafana_dashboard.test" , & dashboard ),
44- resource .TestCheckResourceAttr ("grafana_dashboard.test" , "id" , "basic" ),
45+ resource .TestCheckResourceAttr ("grafana_dashboard.test" , "id" , "0: basic" ), // <org id>:<uid>
4546 resource .TestCheckResourceAttr ("grafana_dashboard.test" , "uid" , "basic" ),
4647 resource .TestCheckResourceAttr ("grafana_dashboard.test" , "url" , strings .TrimRight (os .Getenv ("GRAFANA_URL" ), "/" )+ "/d/basic/terraform-acceptance-test" ),
4748 resource .TestCheckResourceAttr (
@@ -54,7 +55,7 @@ func TestAccDashboard_basic(t *testing.T) {
5455 Config : testAccExample (t , "resources/grafana_dashboard/_acc_basic_update.tf" ),
5556 Check : resource .ComposeTestCheckFunc (
5657 testAccDashboardCheckExists ("grafana_dashboard.test" , & dashboard ),
57- resource .TestCheckResourceAttr ("grafana_dashboard.test" , "id" , "basic" ),
58+ resource .TestCheckResourceAttr ("grafana_dashboard.test" , "id" , "0: basic" ), // <org id>:<uid>
5859 resource .TestCheckResourceAttr ("grafana_dashboard.test" , "uid" , "basic" ),
5960 resource .TestCheckResourceAttr (
6061 "grafana_dashboard.test" , "config_json" , expectedUpdatedTitleConfig ,
@@ -68,7 +69,7 @@ func TestAccDashboard_basic(t *testing.T) {
6869 Config : testAccExample (t , "resources/grafana_dashboard/_acc_basic_update_uid.tf" ),
6970 Check : resource .ComposeTestCheckFunc (
7071 testAccDashboardCheckExists ("grafana_dashboard.test" , & dashboard ),
71- resource .TestCheckResourceAttr ("grafana_dashboard.test" , "id" , "basic-update" ),
72+ resource .TestCheckResourceAttr ("grafana_dashboard.test" , "id" , "0: basic-update" ), // <org id>:<uid>
7273 resource .TestCheckResourceAttr ("grafana_dashboard.test" , "uid" , "basic-update" ),
7374 resource .TestCheckResourceAttr ("grafana_dashboard.test" , "url" , strings .TrimRight (os .Getenv ("GRAFANA_URL" ), "/" )+ "/d/basic-update/updated-title" ),
7475 resource .TestCheckResourceAttr (
@@ -96,7 +97,7 @@ func TestAccDashboard_uid_unset(t *testing.T) {
9697
9798 resource .ParallelTest (t , resource.TestCase {
9899 ProviderFactories : testAccProviderFactories ,
99- CheckDestroy : testAccDashboardCheckDestroy (& dashboard ),
100+ CheckDestroy : testAccDashboardCheckDestroy (& dashboard , 0 ),
100101 Steps : []resource.TestStep {
101102 {
102103 // Create dashboard with no uid set.
@@ -140,7 +141,7 @@ func TestAccDashboard_computed_config(t *testing.T) {
140141
141142 resource .ParallelTest (t , resource.TestCase {
142143 ProviderFactories : testAccProviderFactories ,
143- CheckDestroy : testAccDashboardCheckDestroy (& dashboard ),
144+ CheckDestroy : testAccDashboardCheckDestroy (& dashboard , 0 ),
144145 Steps : []resource.TestStep {
145146 {
146147 // Test resource creation.
@@ -170,7 +171,7 @@ func TestAccDashboard_folder(t *testing.T) {
170171 testAccDashboardCheckExists ("grafana_dashboard.test_folder" , & dashboard ),
171172 testAccFolderCheckExists ("grafana_folder.test_folder" , & folder ),
172173 testAccDashboardCheckExistsInFolder (& dashboard , & folder ),
173- resource .TestCheckResourceAttr ("grafana_dashboard.test_folder" , "id" , "folder" ),
174+ resource .TestCheckResourceAttr ("grafana_dashboard.test_folder" , "id" , "0: folder" ), // <org id>:<uid>
174175 resource .TestCheckResourceAttr ("grafana_dashboard.test_folder" , "uid" , "folder" ),
175176 resource .TestMatchResourceAttr (
176177 "grafana_dashboard.test_folder" , "folder" , idRegexp ,
@@ -181,6 +182,40 @@ func TestAccDashboard_folder(t *testing.T) {
181182 })
182183}
183184
185+ func TestAccDashboard_inOrg (t * testing.T ) {
186+ CheckOSSTestsEnabled (t )
187+
188+ var dashboard gapi.Dashboard
189+ var org gapi.Org
190+
191+ orgName := acctest .RandString (10 )
192+
193+ resource .ParallelTest (t , resource.TestCase {
194+ ProviderFactories : testAccProviderFactories ,
195+ CheckDestroy : testAccDashboardCheckDestroy (& dashboard , org .ID ),
196+ Steps : []resource.TestStep {
197+ {
198+ Config : testAccDashboardInOrganization (orgName ),
199+ Check : resource .ComposeTestCheckFunc (
200+ testAccOrganizationCheckExists ("grafana_organization.test" , & org ),
201+ testAccDashboardCheckExists ("grafana_dashboard.test" , & dashboard ),
202+ resource .TestCheckResourceAttr ("grafana_dashboard.test" , "uid" , "dashboard-" + orgName ),
203+ // Check that the dashboard is not in the default org, from its ID
204+ func (s * terraform.State ) error {
205+ expectedOrgID := org .ID
206+ if org .ID <= 1 {
207+ return fmt .Errorf ("expected org ID higher than 1, got %d" , org .ID )
208+ }
209+ expectedDashboardID := fmt .Sprintf ("%d:dashboard-%s" , expectedOrgID , orgName ) // <org id>:<uid>
210+ checkFunc := resource .TestCheckResourceAttr ("grafana_dashboard.test" , "id" , expectedDashboardID )
211+ return checkFunc (s )
212+ },
213+ ),
214+ },
215+ },
216+ })
217+ }
218+
184219func testAccDashboardCheckExists (rn string , dashboard * gapi.Dashboard ) resource.TestCheckFunc {
185220 return func (s * terraform.State ) error {
186221 rs , ok := s .RootModule ().Resources [rn ]
@@ -190,8 +225,9 @@ func testAccDashboardCheckExists(rn string, dashboard *gapi.Dashboard) resource.
190225 if rs .Primary .ID == "" {
191226 return fmt .Errorf ("resource id not set" )
192227 }
193- client := testAccProvider .Meta ().(* client ).gapi
194- gotDashboard , err := client .DashboardByUID (rs .Primary .ID )
228+
229+ client , _ , dashboardID := clientFromOSSOrgID (testAccProvider .Meta (), rs .Primary .ID )
230+ gotDashboard , err := client .DashboardByUID (dashboardID )
195231 if err != nil {
196232 return fmt .Errorf ("error getting dashboard: %s" , err )
197233 }
@@ -209,9 +245,13 @@ func testAccDashboardCheckExistsInFolder(dashboard *gapi.Dashboard, folder *gapi
209245 }
210246}
211247
212- func testAccDashboardCheckDestroy (dashboard * gapi.Dashboard ) resource.TestCheckFunc {
248+ func testAccDashboardCheckDestroy (dashboard * gapi.Dashboard , orgID int64 ) resource.TestCheckFunc {
213249 return func (s * terraform.State ) error {
214250 client := testAccProvider .Meta ().(* client ).gapi
251+ if orgID != 0 {
252+ client = client .WithOrgID (orgID )
253+ }
254+
215255 _ , err := client .DashboardByUID (dashboard .Model ["uid" ].(string ))
216256 if err == nil {
217257 return fmt .Errorf ("dashboard still exists" )
@@ -294,3 +334,18 @@ func Test_normalizeDashboardConfigJSON(t *testing.T) {
294334 })
295335 }
296336}
337+
338+ func testAccDashboardInOrganization (orgName string ) string {
339+ return fmt .Sprintf (`
340+ resource "grafana_organization" "test" {
341+ name = "%[1]s"
342+ }
343+
344+ resource "grafana_dashboard" "test" {
345+ org_id = grafana_organization.test.id
346+ config_json = jsonencode({
347+ title = "dashboard-%[1]s"
348+ uid = "dashboard-%[1]s"
349+ })
350+ }` , orgName )
351+ }
0 commit comments