Skip to content

Commit

Permalink
Fix folder resource read for Grafana 8.5 (#464)
Browse files Browse the repository at this point in the history
* Bump CI Grafana versions, Add 8.5

* Hackish fix to get folder

* Keep same signature
  • Loading branch information
julienduchesne committed Apr 22, 2022
1 parent 0eba6ef commit b7cb0ea
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .drone/drone.jsonnet
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
26 changes: 13 additions & 13 deletions .drone/drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -300,6 +300,6 @@ workspace:
path: /drone/terraform-provider-grafana
---
kind: signature
hmac: fb944ed34626806208f6ad39935f7d01bf1de4e6ee696f779c2b8b8f4233189b
hmac: 6ff6d080f8016b9d41ac6f560d82c5bb0ea1a267e30adbf66fd2e99b1be6b5b0

...
4 changes: 2 additions & 2 deletions grafana/data_source_folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion grafana/resource_dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
23 changes: 22 additions & 1 deletion grafana/resource_folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package grafana
import (
"context"
"encoding/json"
"errors"
"log"
"strconv"
"strings"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"}`)
}
4 changes: 2 additions & 2 deletions grafana/resource_folder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion grafana/resource_library_panel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit b7cb0ea

Please sign in to comment.