diff --git a/docs/resources/dashboard.md b/docs/resources/dashboard.md index e34422bd5..d76dfcfa8 100644 --- a/docs/resources/dashboard.md +++ b/docs/resources/dashboard.md @@ -17,8 +17,17 @@ Manages Grafana dashboards. ## Example Usage ```terraform -resource "grafana_dashboard" "metrics" { - config_json = file("grafana-dashboard.json") +resource "grafana_folder" "test" { + title = "My Folder" + uid = "my-folder-uid" +} + +resource "grafana_dashboard" "test" { + folder = grafana_folder.test.uid + config_json = jsonencode({ + "title" : "My Dashboard", + "uid" : "my-dashboard-uid" + }) } ``` diff --git a/examples/resources/grafana_dashboard/_acc_folder.tf b/examples/resources/grafana_dashboard/_acc_folder.tf deleted file mode 100644 index 5d9ba9751..000000000 --- a/examples/resources/grafana_dashboard/_acc_folder.tf +++ /dev/null @@ -1,15 +0,0 @@ -resource "grafana_folder" "test_folder" { - title = "Terraform Folder Folder ID Test" - uid = "folder-dashboard-id-test" -} - -resource "grafana_dashboard" "test_folder" { - folder = grafana_folder.test_folder.id - config_json = jsonencode({ - "title" : "Terraform Folder Test Dashboard With ID", - "id" : 123, - "version" : "434", - "uid" : "folder-dashboard-test-ref-with-id" - }) -} - diff --git a/examples/resources/grafana_dashboard/_acc_folder_uid_ref.tf b/examples/resources/grafana_dashboard/_acc_folder_uid_ref.tf deleted file mode 100644 index 2fa7981b4..000000000 --- a/examples/resources/grafana_dashboard/_acc_folder_uid_ref.tf +++ /dev/null @@ -1,14 +0,0 @@ -resource "grafana_folder" "test_folder" { - title = "Terraform Folder Folder UID Test" - uid = "folder-dashboard-uid-test" -} - -resource "grafana_dashboard" "test_folder" { - folder = grafana_folder.test_folder.uid - config_json = jsonencode({ - "title" : "Terraform Folder Test Dashboard With UID", - "id" : 1234, - "version" : "4345", - "uid" : "folder-dashboard-test-ref-with-uid" - }) -} diff --git a/examples/resources/grafana_dashboard/resource.tf b/examples/resources/grafana_dashboard/resource.tf index 8771a6029..3477f7cfa 100644 --- a/examples/resources/grafana_dashboard/resource.tf +++ b/examples/resources/grafana_dashboard/resource.tf @@ -1,3 +1,12 @@ -resource "grafana_dashboard" "metrics" { - config_json = file("grafana-dashboard.json") +resource "grafana_folder" "test" { + title = "My Folder" + uid = "my-folder-uid" +} + +resource "grafana_dashboard" "test" { + folder = grafana_folder.test.uid + config_json = jsonencode({ + "title" : "My Dashboard", + "uid" : "my-dashboard-uid" + }) } diff --git a/internal/resources/grafana/resource_dashboard.go b/internal/resources/grafana/resource_dashboard.go index d75cf9091..75915831d 100644 --- a/internal/resources/grafana/resource_dashboard.go +++ b/internal/resources/grafana/resource_dashboard.go @@ -65,7 +65,6 @@ Manages Grafana dashboards. "folder": { Type: schema.TypeString, Optional: true, - ForceNew: true, Description: "The id or UID of the folder to save the dashboard in.", DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { _, old = SplitOrgResourceID(old) diff --git a/internal/resources/grafana/resource_dashboard_test.go b/internal/resources/grafana/resource_dashboard_test.go index 41f4f0383..9d6f2cf1d 100644 --- a/internal/resources/grafana/resource_dashboard_test.go +++ b/internal/resources/grafana/resource_dashboard_test.go @@ -163,6 +163,8 @@ func TestAccDashboard_computed_config(t *testing.T) { func TestAccDashboard_folder(t *testing.T) { testutils.CheckOSSTestsEnabled(t) + uid := acctest.RandString(10) + var dashboard gapi.Dashboard var folder goapi.Folder @@ -171,13 +173,25 @@ func TestAccDashboard_folder(t *testing.T) { CheckDestroy: testAccDashboardFolderCheckDestroy(&dashboard, &folder), Steps: []resource.TestStep{ { - Config: testutils.TestAccExample(t, "resources/grafana_dashboard/_acc_folder.tf"), + Config: testAccDashboardFolder(uid, "grafana_folder.test_folder1.id"), + Check: resource.ComposeTestCheckFunc( + testAccDashboardCheckExists("grafana_dashboard.test_folder", &dashboard), + folderCheckExists.exists("grafana_folder.test_folder1", &folder), + testAccDashboardCheckExistsInFolder(&dashboard, &folder), + resource.TestCheckResourceAttr("grafana_dashboard.test_folder", "id", "1:"+uid), // : + resource.TestCheckResourceAttr("grafana_dashboard.test_folder", "uid", uid), + resource.TestMatchResourceAttr("grafana_dashboard.test_folder", "folder", common.IDRegexp), + ), + }, + // Update folder + { + Config: testAccDashboardFolder(uid, "grafana_folder.test_folder2.id"), Check: resource.ComposeTestCheckFunc( testAccDashboardCheckExists("grafana_dashboard.test_folder", &dashboard), - folderCheckExists.exists("grafana_folder.test_folder", &folder), + folderCheckExists.exists("grafana_folder.test_folder2", &folder), testAccDashboardCheckExistsInFolder(&dashboard, &folder), - resource.TestCheckResourceAttr("grafana_dashboard.test_folder", "id", "1:folder-dashboard-test-ref-with-id"), // : - resource.TestCheckResourceAttr("grafana_dashboard.test_folder", "uid", "folder-dashboard-test-ref-with-id"), + resource.TestCheckResourceAttr("grafana_dashboard.test_folder", "id", "1:"+uid), // : + resource.TestCheckResourceAttr("grafana_dashboard.test_folder", "uid", uid), resource.TestMatchResourceAttr("grafana_dashboard.test_folder", "folder", common.IDRegexp), ), }, @@ -188,6 +202,8 @@ func TestAccDashboard_folder(t *testing.T) { func TestAccDashboard_folder_uid(t *testing.T) { testutils.CheckOSSTestsEnabled(t, ">=8.0.0") // UID in folders were added in v8 + uid := acctest.RandString(10) + var dashboard gapi.Dashboard var folder goapi.Folder @@ -196,14 +212,26 @@ func TestAccDashboard_folder_uid(t *testing.T) { CheckDestroy: testAccDashboardFolderCheckDestroy(&dashboard, &folder), Steps: []resource.TestStep{ { - Config: testutils.TestAccExample(t, "resources/grafana_dashboard/_acc_folder_uid_ref.tf"), + Config: testAccDashboardFolder(uid, "grafana_folder.test_folder1.uid"), + Check: resource.ComposeTestCheckFunc( + folderCheckExists.exists("grafana_folder.test_folder1", &folder), + testAccDashboardCheckExists("grafana_dashboard.test_folder", &dashboard), + testAccDashboardCheckExistsInFolder(&dashboard, &folder), + resource.TestCheckResourceAttr("grafana_dashboard.test_folder", "id", "1:"+uid), // : + resource.TestCheckResourceAttr("grafana_dashboard.test_folder", "uid", uid), + resource.TestCheckResourceAttr("grafana_dashboard.test_folder", "folder", uid+"-1"), + ), + }, + // Update folder + { + Config: testAccDashboardFolder(uid, "grafana_folder.test_folder2.uid"), Check: resource.ComposeTestCheckFunc( - folderCheckExists.exists("grafana_folder.test_folder", &folder), + folderCheckExists.exists("grafana_folder.test_folder2", &folder), testAccDashboardCheckExists("grafana_dashboard.test_folder", &dashboard), testAccDashboardCheckExistsInFolder(&dashboard, &folder), - resource.TestCheckResourceAttr("grafana_dashboard.test_folder", "id", "1:folder-dashboard-test-ref-with-uid"), // : - resource.TestCheckResourceAttr("grafana_dashboard.test_folder", "uid", "folder-dashboard-test-ref-with-uid"), - resource.TestCheckResourceAttr("grafana_dashboard.test_folder", "folder", "folder-dashboard-uid-test"), + resource.TestCheckResourceAttr("grafana_dashboard.test_folder", "id", "1:"+uid), // : + resource.TestCheckResourceAttr("grafana_dashboard.test_folder", "uid", uid), + resource.TestCheckResourceAttr("grafana_dashboard.test_folder", "folder", uid+"-2"), ), }, { @@ -397,6 +425,27 @@ func Test_NormalizeDashboardConfigJSON(t *testing.T) { } } +func testAccDashboardFolder(uid string, folderRef string) string { + return fmt.Sprintf(` +resource "grafana_folder" "test_folder1" { + title = "%[1]s-1" + uid = "%[1]s-1" +} + +resource "grafana_folder" "test_folder2" { + title = "%[1]s-2" + uid = "%[1]s-2" +} + +resource "grafana_dashboard" "test_folder" { + folder = %[2]s + config_json = jsonencode({ + "title" : "%[1]s", + "uid" : "%[1]s" + }) +}`, uid, folderRef) +} + func testAccDashboardInOrganization(orgName string) string { return fmt.Sprintf(` resource "grafana_organization" "test" {