From b7cb0eaeb0b048de1a380b19a0c40377cf7b0b85 Mon Sep 17 00:00:00 2001 From: Julien Duchesne Date: Fri, 22 Apr 2022 13:04:44 -0400 Subject: [PATCH] Fix folder resource read for Grafana 8.5 (#464) * Bump CI Grafana versions, Add 8.5 * Hackish fix to get folder * Keep same signature --- .drone/drone.jsonnet | 2 +- .drone/drone.yml | 26 +++++++++++++------------- grafana/data_source_folder.go | 4 ++-- grafana/resource_dashboard_test.go | 2 +- grafana/resource_folder.go | 23 ++++++++++++++++++++++- grafana/resource_folder_test.go | 4 ++-- grafana/resource_library_panel_test.go | 2 +- 7 files changed, 42 insertions(+), 21 deletions(-) diff --git a/.drone/drone.jsonnet b/.drone/drone.jsonnet index 4561a4018..acd5c52f6 100644 --- a/.drone/drone.jsonnet +++ b/.drone/drone.jsonnet @@ -1,4 +1,4 @@ -local grafanaVersions = ['8.4.4', '8.3.7', '8.2.7', '8.1.8', '7.5.15']; +local grafanaVersions = ['8.5.0', '8.4.7', '8.3.7', '8.2.7', '7.5.15']; local images = { go: 'golang:1.18', lint: 'golangci/golangci-lint', diff --git a/.drone/drone.yml b/.drone/drone.yml index 7f6c8581b..923c9f1cd 100644 --- a/.drone/drone.yml +++ b/.drone/drone.yml @@ -145,14 +145,14 @@ kind: secret name: grafana-sm-token --- kind: pipeline -name: 'oss tests: 8.4.4' +name: 'oss tests: 8.5.0' platform: arch: amd64 os: linux services: - environment: GF_DATABASE_URL: sqlite3:///var/lib/grafana/grafana.db?cache=private&mode=rwc&_journal_mode=WAL - image: grafana/grafana:8.4.4 + image: grafana/grafana:8.5.0 name: grafana steps: - commands: @@ -162,7 +162,7 @@ steps: GRAFANA_AUTH: admin:admin GRAFANA_ORG_ID: 1 GRAFANA_URL: http://grafana:3000 - GRAFANA_VERSION: 8.4.4 + GRAFANA_VERSION: 8.5.0 image: golang:1.18 name: tests trigger: @@ -176,14 +176,14 @@ workspace: path: /drone/terraform-provider-grafana --- kind: pipeline -name: 'oss tests: 8.3.7' +name: 'oss tests: 8.4.7' platform: arch: amd64 os: linux services: - environment: GF_DATABASE_URL: sqlite3:///var/lib/grafana/grafana.db?cache=private&mode=rwc&_journal_mode=WAL - image: grafana/grafana:8.3.7 + image: grafana/grafana:8.4.7 name: grafana steps: - commands: @@ -193,7 +193,7 @@ steps: GRAFANA_AUTH: admin:admin GRAFANA_ORG_ID: 1 GRAFANA_URL: http://grafana:3000 - GRAFANA_VERSION: 8.3.7 + GRAFANA_VERSION: 8.4.7 image: golang:1.18 name: tests trigger: @@ -207,14 +207,14 @@ workspace: path: /drone/terraform-provider-grafana --- kind: pipeline -name: 'oss tests: 8.2.7' +name: 'oss tests: 8.3.7' platform: arch: amd64 os: linux services: - environment: GF_DATABASE_URL: sqlite3:///var/lib/grafana/grafana.db?cache=private&mode=rwc&_journal_mode=WAL - image: grafana/grafana:8.2.7 + image: grafana/grafana:8.3.7 name: grafana steps: - commands: @@ -224,7 +224,7 @@ steps: GRAFANA_AUTH: admin:admin GRAFANA_ORG_ID: 1 GRAFANA_URL: http://grafana:3000 - GRAFANA_VERSION: 8.2.7 + GRAFANA_VERSION: 8.3.7 image: golang:1.18 name: tests trigger: @@ -238,14 +238,14 @@ workspace: path: /drone/terraform-provider-grafana --- kind: pipeline -name: 'oss tests: 8.1.8' +name: 'oss tests: 8.2.7' platform: arch: amd64 os: linux services: - environment: GF_DATABASE_URL: sqlite3:///var/lib/grafana/grafana.db?cache=private&mode=rwc&_journal_mode=WAL - image: grafana/grafana:8.1.8 + image: grafana/grafana:8.2.7 name: grafana steps: - commands: @@ -255,7 +255,7 @@ steps: GRAFANA_AUTH: admin:admin GRAFANA_ORG_ID: 1 GRAFANA_URL: http://grafana:3000 - GRAFANA_VERSION: 8.1.8 + GRAFANA_VERSION: 8.2.7 image: golang:1.18 name: tests trigger: @@ -300,6 +300,6 @@ workspace: path: /drone/terraform-provider-grafana --- kind: signature -hmac: fb944ed34626806208f6ad39935f7d01bf1de4e6ee696f779c2b8b8f4233189b +hmac: 6ff6d080f8016b9d41ac6f560d82c5bb0ea1a267e30adbf66fd2e99b1be6b5b0 ... diff --git a/grafana/data_source_folder.go b/grafana/data_source_folder.go index 69cc2e012..90dad77ac 100644 --- a/grafana/data_source_folder.go +++ b/grafana/data_source_folder.go @@ -51,8 +51,8 @@ func findFolderWithTitle(client *gapi.Client, title string) (*gapi.Folder, error for _, f := range folders { if f.Title == title { - // Query the folder by ID, that API has additional information - return client.Folder(f.ID) + // Query the folder by UID, that API has additional information + return client.FolderByUID(f.UID) } } diff --git a/grafana/resource_dashboard_test.go b/grafana/resource_dashboard_test.go index 668b7ded2..e044a1336 100644 --- a/grafana/resource_dashboard_test.go +++ b/grafana/resource_dashboard_test.go @@ -226,7 +226,7 @@ func testAccDashboardFolderCheckDestroy(dashboard *gapi.Dashboard, folder *gapi. if err == nil { return fmt.Errorf("dashboard still exists") } - folder, err = client.Folder(folder.ID) + folder, err = getFolderById(client, folder.ID) if err == nil { return fmt.Errorf("the following folder still exists: %s", folder.Title) } diff --git a/grafana/resource_folder.go b/grafana/resource_folder.go index dc4704a02..f76f4e213 100644 --- a/grafana/resource_folder.go +++ b/grafana/resource_folder.go @@ -3,6 +3,7 @@ package grafana import ( "context" "encoding/json" + "errors" "log" "strconv" "strings" @@ -99,7 +100,7 @@ func ReadFolder(ctx context.Context, d *schema.ResourceData, meta interface{}) d return diag.FromErr(err) } - folder, err := client.Folder(id) + folder, err := getFolderById(client, id) if err != nil { if strings.HasPrefix(err.Error(), "status: 404") { log.Printf("[WARN] removing folder %d from state because it no longer exists in grafana", id) @@ -161,3 +162,23 @@ func NormalizeFolderConfigJSON(configI interface{}) string { return string(ret) } + +// Hackish way to get the folder by ID. +// TODO: Revert to using the specific folder ID GET endpoint once it's fixed +// Broken in 8.5.0 +func getFolderById(client *gapi.Client, id int64) (*gapi.Folder, error) { + folders, err := client.Folders() + if err != nil { + return nil, err + } + + for _, folder := range folders { + if folder.ID == id { + // Need to use another API call, because the "list" call doesn't have all the info + return client.FolderByUID(folder.UID) + } + } + + // Replicating the error that would usually be returned by the API call on a missing folder + return nil, errors.New(`status: 404, body: {"message":"folder not found","status":"not-found"}`) +} diff --git a/grafana/resource_folder_test.go b/grafana/resource_folder_test.go index f3e528d4a..f1ab5f9df 100644 --- a/grafana/resource_folder_test.go +++ b/grafana/resource_folder_test.go @@ -107,7 +107,7 @@ func testAccFolderCheckExists(rn string, folder *gapi.Folder) resource.TestCheck if id == 0 { return fmt.Errorf("got a folder id of 0") } - gotFolder, err := client.Folder(id) + gotFolder, err := getFolderById(client, id) if err != nil { return fmt.Errorf("error getting folder: %s", err) } @@ -121,7 +121,7 @@ func testAccFolderCheckExists(rn string, folder *gapi.Folder) resource.TestCheck func testAccFolderCheckDestroy(folder *gapi.Folder) resource.TestCheckFunc { return func(s *terraform.State) error { client := testAccProvider.Meta().(*client).gapi - _, err := client.Folder(folder.ID) + _, err := getFolderById(client, folder.ID) if err == nil { return fmt.Errorf("folder still exists") } diff --git a/grafana/resource_library_panel_test.go b/grafana/resource_library_panel_test.go index a92b0e9cd..16cfc19d4 100644 --- a/grafana/resource_library_panel_test.go +++ b/grafana/resource_library_panel_test.go @@ -167,7 +167,7 @@ func testAccLibraryPanelFolderCheckDestroy(panel *gapi.LibraryPanel, folder *gap if err == nil { return fmt.Errorf("panel still exists") } - folder, err = client.Folder(folder.ID) + folder, err = getFolderById(client, folder.ID) if err == nil { return fmt.Errorf("the following folder still exists: %s", folder.Title) }