Skip to content

Commit f3c8b22

Browse files
Dashboard with org id: Additional validation (#804)
In the tests deploying a dashboard in an specific org, additionally validate that the given dashboard is NOT in the default org This is a sanity check to make sure the OrgID logic does not get broken in the future If the dashboard exists when passing the orgID but does not when using the default org, it means that the org attribute works as expected
1 parent 988272a commit f3c8b22

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

provider/resource_dashboard_test.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,20 @@ func testAccDashboardCheckExists(rn string, dashboard *gapi.Dashboard) resource.
226226
if rs.Primary.ID == "" {
227227
return fmt.Errorf("resource id not set")
228228
}
229+
orgID, dashboardUID := splitOSSOrgID(rs.Primary.ID)
229230

230-
client, _, dashboardID := clientFromOSSOrgID(testAccProvider.Meta(), rs.Primary.ID)
231-
gotDashboard, err := client.DashboardByUID(dashboardID)
231+
client := testAccProvider.Meta().(*common.Client).GrafanaAPI
232+
// If the org ID is set, check that the dashboard doesn't exist in the default org
233+
if orgID > 0 {
234+
dashboard, err := client.DashboardByUID(dashboardUID)
235+
if err == nil || dashboard != nil {
236+
return fmt.Errorf("dashboard %s exists in the default org", dashboardUID)
237+
}
238+
client = client.WithOrgID(orgID)
239+
}
240+
241+
// Check that the dashboard is in the expected org
242+
gotDashboard, err := client.DashboardByUID(dashboardUID)
232243
if err != nil {
233244
return fmt.Errorf("error getting dashboard: %s", err)
234245
}
@@ -248,15 +259,22 @@ func testAccDashboardCheckExistsInFolder(dashboard *gapi.Dashboard, folder *gapi
248259

249260
func testAccDashboardCheckDestroy(dashboard *gapi.Dashboard, orgID int64) resource.TestCheckFunc {
250261
return func(s *terraform.State) error {
262+
// Check that the dashboard was deleted from the default organization
251263
client := testAccProvider.Meta().(*common.Client).GrafanaAPI
264+
dashboard, err := client.DashboardByUID(dashboard.Model["uid"].(string))
265+
if dashboard != nil || err == nil {
266+
return fmt.Errorf("dashboard still exists")
267+
}
268+
269+
// If the dashboard was created in an organization, check that it was deleted from that organization
252270
if orgID != 0 {
253271
client = client.WithOrgID(orgID)
272+
dashboard, err = client.DashboardByUID(dashboard.Model["uid"].(string))
273+
if dashboard != nil || err == nil {
274+
return fmt.Errorf("dashboard still exists")
275+
}
254276
}
255277

256-
_, err := client.DashboardByUID(dashboard.Model["uid"].(string))
257-
if err == nil {
258-
return fmt.Errorf("dashboard still exists")
259-
}
260278
return nil
261279
}
262280
}

0 commit comments

Comments
 (0)