Skip to content

Commit

Permalink
Dashboard: Allow folder update (#1110)
Browse files Browse the repository at this point in the history
Closes #1105
Also removed the folder static examples to replace them with code. Too many variables now
  • Loading branch information
julienduchesne authored Nov 1, 2023
1 parent 0d73f2d commit 930d17b
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 43 deletions.
13 changes: 11 additions & 2 deletions docs/resources/dashboard.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
})
}
```

Expand Down
15 changes: 0 additions & 15 deletions examples/resources/grafana_dashboard/_acc_folder.tf

This file was deleted.

14 changes: 0 additions & 14 deletions examples/resources/grafana_dashboard/_acc_folder_uid_ref.tf

This file was deleted.

13 changes: 11 additions & 2 deletions examples/resources/grafana_dashboard/resource.tf
Original file line number Diff line number Diff line change
@@ -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"
})
}
1 change: 0 additions & 1 deletion internal/resources/grafana/resource_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
67 changes: 58 additions & 9 deletions internal/resources/grafana/resource_dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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), // <org id>:<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"), // <org id>:<uid>
resource.TestCheckResourceAttr("grafana_dashboard.test_folder", "uid", "folder-dashboard-test-ref-with-id"),
resource.TestCheckResourceAttr("grafana_dashboard.test_folder", "id", "1:"+uid), // <org id>:<uid>
resource.TestCheckResourceAttr("grafana_dashboard.test_folder", "uid", uid),
resource.TestMatchResourceAttr("grafana_dashboard.test_folder", "folder", common.IDRegexp),
),
},
Expand All @@ -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

Expand All @@ -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), // <org id>:<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"), // <org id>:<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), // <org id>:<uid>
resource.TestCheckResourceAttr("grafana_dashboard.test_folder", "uid", uid),
resource.TestCheckResourceAttr("grafana_dashboard.test_folder", "folder", uid+"-2"),
),
},
{
Expand Down Expand Up @@ -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" {
Expand Down

0 comments on commit 930d17b

Please sign in to comment.