Skip to content

Commit b7cb0ea

Browse files
Fix folder resource read for Grafana 8.5 (#464)
* Bump CI Grafana versions, Add 8.5 * Hackish fix to get folder * Keep same signature
1 parent 0eba6ef commit b7cb0ea

File tree

7 files changed

+42
-21
lines changed

7 files changed

+42
-21
lines changed

.drone/drone.jsonnet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
local grafanaVersions = ['8.4.4', '8.3.7', '8.2.7', '8.1.8', '7.5.15'];
1+
local grafanaVersions = ['8.5.0', '8.4.7', '8.3.7', '8.2.7', '7.5.15'];
22
local images = {
33
go: 'golang:1.18',
44
lint: 'golangci/golangci-lint',

.drone/drone.yml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,14 @@ kind: secret
145145
name: grafana-sm-token
146146
---
147147
kind: pipeline
148-
name: 'oss tests: 8.4.4'
148+
name: 'oss tests: 8.5.0'
149149
platform:
150150
arch: amd64
151151
os: linux
152152
services:
153153
- environment:
154154
GF_DATABASE_URL: sqlite3:///var/lib/grafana/grafana.db?cache=private&mode=rwc&_journal_mode=WAL
155-
image: grafana/grafana:8.4.4
155+
image: grafana/grafana:8.5.0
156156
name: grafana
157157
steps:
158158
- commands:
@@ -162,7 +162,7 @@ steps:
162162
GRAFANA_AUTH: admin:admin
163163
GRAFANA_ORG_ID: 1
164164
GRAFANA_URL: http://grafana:3000
165-
GRAFANA_VERSION: 8.4.4
165+
GRAFANA_VERSION: 8.5.0
166166
image: golang:1.18
167167
name: tests
168168
trigger:
@@ -176,14 +176,14 @@ workspace:
176176
path: /drone/terraform-provider-grafana
177177
---
178178
kind: pipeline
179-
name: 'oss tests: 8.3.7'
179+
name: 'oss tests: 8.4.7'
180180
platform:
181181
arch: amd64
182182
os: linux
183183
services:
184184
- environment:
185185
GF_DATABASE_URL: sqlite3:///var/lib/grafana/grafana.db?cache=private&mode=rwc&_journal_mode=WAL
186-
image: grafana/grafana:8.3.7
186+
image: grafana/grafana:8.4.7
187187
name: grafana
188188
steps:
189189
- commands:
@@ -193,7 +193,7 @@ steps:
193193
GRAFANA_AUTH: admin:admin
194194
GRAFANA_ORG_ID: 1
195195
GRAFANA_URL: http://grafana:3000
196-
GRAFANA_VERSION: 8.3.7
196+
GRAFANA_VERSION: 8.4.7
197197
image: golang:1.18
198198
name: tests
199199
trigger:
@@ -207,14 +207,14 @@ workspace:
207207
path: /drone/terraform-provider-grafana
208208
---
209209
kind: pipeline
210-
name: 'oss tests: 8.2.7'
210+
name: 'oss tests: 8.3.7'
211211
platform:
212212
arch: amd64
213213
os: linux
214214
services:
215215
- environment:
216216
GF_DATABASE_URL: sqlite3:///var/lib/grafana/grafana.db?cache=private&mode=rwc&_journal_mode=WAL
217-
image: grafana/grafana:8.2.7
217+
image: grafana/grafana:8.3.7
218218
name: grafana
219219
steps:
220220
- commands:
@@ -224,7 +224,7 @@ steps:
224224
GRAFANA_AUTH: admin:admin
225225
GRAFANA_ORG_ID: 1
226226
GRAFANA_URL: http://grafana:3000
227-
GRAFANA_VERSION: 8.2.7
227+
GRAFANA_VERSION: 8.3.7
228228
image: golang:1.18
229229
name: tests
230230
trigger:
@@ -238,14 +238,14 @@ workspace:
238238
path: /drone/terraform-provider-grafana
239239
---
240240
kind: pipeline
241-
name: 'oss tests: 8.1.8'
241+
name: 'oss tests: 8.2.7'
242242
platform:
243243
arch: amd64
244244
os: linux
245245
services:
246246
- environment:
247247
GF_DATABASE_URL: sqlite3:///var/lib/grafana/grafana.db?cache=private&mode=rwc&_journal_mode=WAL
248-
image: grafana/grafana:8.1.8
248+
image: grafana/grafana:8.2.7
249249
name: grafana
250250
steps:
251251
- commands:
@@ -255,7 +255,7 @@ steps:
255255
GRAFANA_AUTH: admin:admin
256256
GRAFANA_ORG_ID: 1
257257
GRAFANA_URL: http://grafana:3000
258-
GRAFANA_VERSION: 8.1.8
258+
GRAFANA_VERSION: 8.2.7
259259
image: golang:1.18
260260
name: tests
261261
trigger:
@@ -300,6 +300,6 @@ workspace:
300300
path: /drone/terraform-provider-grafana
301301
---
302302
kind: signature
303-
hmac: fb944ed34626806208f6ad39935f7d01bf1de4e6ee696f779c2b8b8f4233189b
303+
hmac: 6ff6d080f8016b9d41ac6f560d82c5bb0ea1a267e30adbf66fd2e99b1be6b5b0
304304

305305
...

grafana/data_source_folder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ func findFolderWithTitle(client *gapi.Client, title string) (*gapi.Folder, error
5151

5252
for _, f := range folders {
5353
if f.Title == title {
54-
// Query the folder by ID, that API has additional information
55-
return client.Folder(f.ID)
54+
// Query the folder by UID, that API has additional information
55+
return client.FolderByUID(f.UID)
5656
}
5757
}
5858

grafana/resource_dashboard_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ func testAccDashboardFolderCheckDestroy(dashboard *gapi.Dashboard, folder *gapi.
226226
if err == nil {
227227
return fmt.Errorf("dashboard still exists")
228228
}
229-
folder, err = client.Folder(folder.ID)
229+
folder, err = getFolderById(client, folder.ID)
230230
if err == nil {
231231
return fmt.Errorf("the following folder still exists: %s", folder.Title)
232232
}

grafana/resource_folder.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package grafana
33
import (
44
"context"
55
"encoding/json"
6+
"errors"
67
"log"
78
"strconv"
89
"strings"
@@ -99,7 +100,7 @@ func ReadFolder(ctx context.Context, d *schema.ResourceData, meta interface{}) d
99100
return diag.FromErr(err)
100101
}
101102

102-
folder, err := client.Folder(id)
103+
folder, err := getFolderById(client, id)
103104
if err != nil {
104105
if strings.HasPrefix(err.Error(), "status: 404") {
105106
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 {
161162

162163
return string(ret)
163164
}
165+
166+
// Hackish way to get the folder by ID.
167+
// TODO: Revert to using the specific folder ID GET endpoint once it's fixed
168+
// Broken in 8.5.0
169+
func getFolderById(client *gapi.Client, id int64) (*gapi.Folder, error) {
170+
folders, err := client.Folders()
171+
if err != nil {
172+
return nil, err
173+
}
174+
175+
for _, folder := range folders {
176+
if folder.ID == id {
177+
// Need to use another API call, because the "list" call doesn't have all the info
178+
return client.FolderByUID(folder.UID)
179+
}
180+
}
181+
182+
// Replicating the error that would usually be returned by the API call on a missing folder
183+
return nil, errors.New(`status: 404, body: {"message":"folder not found","status":"not-found"}`)
184+
}

grafana/resource_folder_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func testAccFolderCheckExists(rn string, folder *gapi.Folder) resource.TestCheck
107107
if id == 0 {
108108
return fmt.Errorf("got a folder id of 0")
109109
}
110-
gotFolder, err := client.Folder(id)
110+
gotFolder, err := getFolderById(client, id)
111111
if err != nil {
112112
return fmt.Errorf("error getting folder: %s", err)
113113
}
@@ -121,7 +121,7 @@ func testAccFolderCheckExists(rn string, folder *gapi.Folder) resource.TestCheck
121121
func testAccFolderCheckDestroy(folder *gapi.Folder) resource.TestCheckFunc {
122122
return func(s *terraform.State) error {
123123
client := testAccProvider.Meta().(*client).gapi
124-
_, err := client.Folder(folder.ID)
124+
_, err := getFolderById(client, folder.ID)
125125
if err == nil {
126126
return fmt.Errorf("folder still exists")
127127
}

grafana/resource_library_panel_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func testAccLibraryPanelFolderCheckDestroy(panel *gapi.LibraryPanel, folder *gap
167167
if err == nil {
168168
return fmt.Errorf("panel still exists")
169169
}
170-
folder, err = client.Folder(folder.ID)
170+
folder, err = getFolderById(client, folder.ID)
171171
if err == nil {
172172
return fmt.Errorf("the following folder still exists: %s", folder.Title)
173173
}

0 commit comments

Comments
 (0)