Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dashboard: Support folder UID as reference #900

Merged
merged 2 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/resources/dashboard.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ resource "grafana_dashboard" "metrics" {

### Optional

- `folder` (String) The id of the folder to save the dashboard in. This attribute is a string to reflect the type of the folder's id.
- `folder` (String) The id or UID of the folder to save the dashboard in.
- `message` (String) Set a commit message for the version history.
- `org_id` (String) The Organization ID. If not set, the Org ID defined in the provider block will be used.
- `overwrite` (Boolean) Set to true if you want to overwrite existing dashboard with newer version, same dashboard title in folder or same dashboard uid.
Expand Down
20 changes: 10 additions & 10 deletions examples/resources/grafana_dashboard/_acc_folder.tf
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
resource "grafana_folder" "test_folder" {
title = "Terraform 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 = <<EOD
{
"title": "Terraform Folder Test Dashboard",
"id": 12,
"version": "43",
"uid": "folder"
}
EOD
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"
})
}

14 changes: 14 additions & 0 deletions examples/resources/grafana_dashboard/_acc_folder_uid_ref.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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"
})
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.18
require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/grafana/amixr-api-go-client v0.0.7
github.com/grafana/grafana-api-golang-client v0.19.1
github.com/grafana/grafana-api-golang-client v0.20.1
github.com/grafana/machine-learning-go-client v0.5.0
github.com/grafana/synthetic-monitoring-agent v0.14.4
github.com/grafana/synthetic-monitoring-api-go-client v0.7.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grafana/amixr-api-go-client v0.0.7 h1:U6W6yKxMMybI+Qz4zl+Vih48o6CczLaU/vjk2m7omvU=
github.com/grafana/amixr-api-go-client v0.0.7/go.mod h1:N6x26XUrM5zGtK5zL5vNJnAn2JFMxLFPPLTw/6pDkFE=
github.com/grafana/grafana-api-golang-client v0.19.1 h1:eKekLtjX+dSYcxJIsMpz0HkTk2QxUkXf1pyLtZCuxNk=
github.com/grafana/grafana-api-golang-client v0.19.1/go.mod h1:24W29gPe9yl0/3A9X624TPkAOR8DpHno490cPwnkv8E=
github.com/grafana/grafana-api-golang-client v0.20.1 h1:xadfMY9PDcWd2ppU/AgYvMeBeWqioed7cetPhBYoNSk=
github.com/grafana/grafana-api-golang-client v0.20.1/go.mod h1:24W29gPe9yl0/3A9X624TPkAOR8DpHno490cPwnkv8E=
github.com/grafana/machine-learning-go-client v0.5.0 h1:Q1K+MPSy8vfMm2jsk3WQ7O77cGr2fM5hxwtPSoPc5NU=
github.com/grafana/machine-learning-go-client v0.5.0/go.mod h1:QFfZz8NkqVF8++skjkKQXJEZfpCYd8S0yTWJUpsLLTA=
github.com/grafana/synthetic-monitoring-agent v0.14.4 h1:amLwPpBvWnqoYHg4Dn2IBdkDy9szcRLr7yCJHMXNhG8=
Expand Down
34 changes: 16 additions & 18 deletions internal/resources/grafana/resource_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

gapi "github.com/grafana/grafana-api-golang-client"
"github.com/grafana/terraform-provider-grafana/internal/common"
Expand Down Expand Up @@ -71,11 +70,10 @@ Manages Grafana dashboards.
"so that previous versions of your dashboard are not lost.",
},
"folder": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "The id of the folder to save the dashboard in. This attribute is a string to reflect the type of the folder's id.",
ValidateFunc: validation.StringMatch(common.IDRegexp, "must be a valid folder id"),
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 {
return old == "0" && new == "" || old == "" && new == "0"
},
Expand Down Expand Up @@ -221,10 +219,13 @@ func ReadDashboard(ctx context.Context, d *schema.ResourceData, meta interface{}
d.Set("dashboard_id", int64(dashboard.Model["id"].(float64)))
d.Set("version", int64(dashboard.Model["version"].(float64)))
d.Set("url", strings.TrimRight(gapiURL, "/")+dashboard.Meta.URL)
if dashboard.FolderID > 0 {

// If the folder was originally set to a numeric ID, we read the folder ID
// Othwerwise, we read the folder UID
if common.IDRegexp.MatchString(d.Get("folder").(string)) && dashboard.Meta.Folder > 0 {
d.Set("folder", strconv.FormatInt(dashboard.FolderID, 10))
} else {
d.Set("folder", "")
d.Set("folder", dashboard.Meta.FolderUID)
}

configJSONBytes, err := json.Marshal(dashboard.Model)
Expand Down Expand Up @@ -288,20 +289,17 @@ func DeleteDashboard(ctx context.Context, d *schema.ResourceData, meta interface
}

func makeDashboard(d *schema.ResourceData) (gapi.Dashboard, error) {
var parsedFolder int64 = 0
var err error
if folderStr := d.Get("folder").(string); folderStr != "" {
parsedFolder, err = strconv.ParseInt(d.Get("folder").(string), 10, 64)
if err != nil {
return gapi.Dashboard{}, fmt.Errorf("error parsing folder: %s", err)
}
}

dashboard := gapi.Dashboard{
FolderID: parsedFolder,
Overwrite: d.Get("overwrite").(bool),
Message: d.Get("message").(string),
}

if folderInt, err := strconv.ParseInt(d.Get("folder").(string), 10, 64); err == nil {
dashboard.FolderID = folderInt
} else {
dashboard.FolderUID = d.Get("folder").(string)
}

configJSON := d.Get("config_json").(string)
dashboardJSON, err := UnmarshalDashboardConfigJSON(configJSON)
if err != nil {
Expand Down
39 changes: 34 additions & 5 deletions internal/resources/grafana/resource_dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,46 @@ func TestAccDashboard_folder(t *testing.T) {
testAccDashboardCheckExists("grafana_dashboard.test_folder", &dashboard),
testAccFolderCheckExists("grafana_folder.test_folder", &folder),
testAccDashboardCheckExistsInFolder(&dashboard, &folder),
resource.TestCheckResourceAttr("grafana_dashboard.test_folder", "id", "0:folder"), // <org id>:<uid>
resource.TestCheckResourceAttr("grafana_dashboard.test_folder", "uid", "folder"),
resource.TestMatchResourceAttr(
"grafana_dashboard.test_folder", "folder", common.IDRegexp,
),
resource.TestCheckResourceAttr("grafana_dashboard.test_folder", "id", "0:folder-dashboard-test-ref-with-id"), // <org id>:<uid>
resource.TestCheckResourceAttr("grafana_dashboard.test_folder", "uid", "folder-dashboard-test-ref-with-id"),
resource.TestMatchResourceAttr("grafana_dashboard.test_folder", "folder", common.IDRegexp),
),
},
},
})
}

func TestAccDashboard_folder_uid(t *testing.T) {
testutils.CheckOSSTestsEnabled(t)
testutils.CheckOSSTestsSemver(t, ">=8.0.0") // UID in folders were added in v8

var dashboard gapi.Dashboard
var folder gapi.Folder

resource.ParallelTest(t, resource.TestCase{
ProviderFactories: testutils.ProviderFactories,
CheckDestroy: testAccDashboardFolderCheckDestroy(&dashboard, &folder),
Steps: []resource.TestStep{
{
Config: testutils.TestAccExample(t, "resources/grafana_dashboard/_acc_folder_uid_ref.tf"),
Check: resource.ComposeTestCheckFunc(
testAccFolderCheckExists("grafana_folder.test_folder", &folder),
testAccDashboardCheckExists("grafana_dashboard.test_folder", &dashboard),
testAccDashboardCheckExistsInFolder(&dashboard, &folder),
resource.TestCheckResourceAttr("grafana_dashboard.test_folder", "id", "0: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"),
),
},
{
ImportState: true,
ResourceName: "grafana_dashboard.test_folder",
ImportStateVerify: true,
},
},
})
}

func TestAccDashboard_inOrg(t *testing.T) {
testutils.CheckOSSTestsEnabled(t)

Expand Down