-
Notifications
You must be signed in to change notification settings - Fork 219
/
resource_library_panel.go
305 lines (273 loc) · 9.48 KB
/
resource_library_panel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package grafana
import (
"context"
"encoding/json"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/grafana/grafana-openapi-client-go/models"
"github.com/grafana/terraform-provider-grafana/v2/internal/common"
)
func resourceLibraryPanel() *common.Resource {
schema := &schema.Resource{
Description: `
Manages Grafana library panels.
* [Official documentation](https://grafana.com/docs/grafana/latest/dashboards/build-dashboards/manage-library-panels/)
* [HTTP API](https://grafana.com/docs/grafana/latest/developers/http_api/library_element/)
`,
CreateContext: createLibraryPanel,
ReadContext: readLibraryPanel,
UpdateContext: updateLibraryPanel,
DeleteContext: deleteLibraryPanel,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"org_id": orgIDAttribute(),
"uid": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "The unique identifier (UID) of a library panel uniquely identifies library panels between multiple Grafana installs. " +
"It’s automatically generated unless you specify it during library panel creation." +
"The UID provides consistent URLs for accessing library panels and when syncing library panels between multiple Grafana installs.",
},
"panel_id": {
Type: schema.TypeInt,
Computed: true,
Description: "The numeric ID of the library panel computed by Grafana.",
},
"folder_id": {
Type: schema.TypeString,
Optional: true,
Description: "Deprecated. Use `folder_uid` instead",
Deprecated: "Use `folder_uid` instead",
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if d.Get("folder_uid") != "" && new == "" {
return true
}
_, old = SplitOrgResourceID(old)
_, new = SplitOrgResourceID(new)
return old == "0" && new == "" || old == "" && new == "0" || old == new
},
ConflictsWith: []string{"folder_uid"},
},
"folder_uid": {
Type: schema.TypeString,
Optional: true,
Description: "Unique ID (UID) of the folder containing the library panel.",
ConflictsWith: []string{"folder_id"},
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if d.Get("folder_id") != "" && new == "" {
return true
}
_, old = SplitOrgResourceID(old)
_, new = SplitOrgResourceID(new)
return old == new
},
ValidateFunc: folderUIDValidation,
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "Name of the library panel.",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "Description of the library panel.",
},
"type": {
Type: schema.TypeString,
Computed: true,
Description: "Type of the library panel (eg. text).",
},
"model_json": {
Type: schema.TypeString,
Required: true,
StateFunc: normalizeLibraryPanelModelJSON,
ValidateFunc: validateLibraryPanelModelJSON,
Description: "The JSON model for the library panel.",
},
"version": {
Type: schema.TypeInt,
Computed: true,
Description: "Version of the library panel.",
},
"folder_name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of the folder containing the library panel.",
},
"created": {
Type: schema.TypeString,
Computed: true,
Description: "Timestamp when the library panel was created.",
},
"updated": {
Type: schema.TypeString,
Computed: true,
Description: "Timestamp when the library panel was last modified.",
},
"dashboard_ids": {
Type: schema.TypeList,
Computed: true,
Description: "Numerical IDs of Grafana dashboards containing the library panel.",
Elem: &schema.Schema{Type: schema.TypeInt},
},
},
}
return common.NewLegacySDKResource(
"grafana_library_panel",
orgResourceIDString("uid"),
schema,
)
}
func createLibraryPanel(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, _ := OAPIClientFromNewOrgResource(meta, d)
panel := makeLibraryPanel(d)
resp, err := client.LibraryElements.CreateLibraryElement(&panel)
if err != nil {
return diag.FromErr(err)
}
createdPanel := resp.Payload.Result
d.SetId(MakeOrgResourceID(createdPanel.OrgID, createdPanel.UID))
return readLibraryPanel(ctx, d, meta)
}
func readLibraryPanel(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, orgID, uid := OAPIClientFromExistingOrgResource(meta, d.Id())
resp, err := client.LibraryElements.GetLibraryElementByUID(uid)
if err, shouldReturn := common.CheckReadError("library panel", d, err); shouldReturn {
return err
}
panel := resp.Payload.Result
modelJSONBytes, err := json.Marshal(panel.Model)
if err != nil {
return diag.FromErr(err)
}
remotePanelJSON, err := unmarshalLibraryPanelModelJSON(string(modelJSONBytes))
if err != nil {
return diag.FromErr(err)
}
modelJSON := normalizeLibraryPanelModelJSON(remotePanelJSON)
d.SetId(MakeOrgResourceID(orgID, uid))
d.Set("uid", panel.UID)
d.Set("panel_id", panel.ID)
d.Set("org_id", strconv.FormatInt(panel.OrgID, 10))
// nolint:staticcheck
d.Set("folder_id", MakeOrgResourceID(orgID, panel.FolderID))
d.Set("folder_uid", panel.Meta.FolderUID)
d.Set("description", panel.Description)
d.Set("type", panel.Type)
d.Set("name", panel.Name)
d.Set("model_json", modelJSON)
d.Set("version", panel.Version)
d.Set("folder_name", panel.Meta.FolderName)
d.Set("created", panel.Meta.Created.String())
d.Set("updated", panel.Meta.Updated.String())
connResp, err := client.LibraryElements.GetLibraryElementConnections(uid)
if err != nil {
return diag.FromErr(err)
}
connections := connResp.Payload.Result
dashboardIds := make([]int64, 0, len(connections))
for _, connection := range connections {
dashboardIds = append(dashboardIds, connection.ConnectionID)
}
d.Set("dashboard_ids", dashboardIds)
return nil
}
func updateLibraryPanel(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, _, uid := OAPIClientFromExistingOrgResource(meta, d.Id())
modelJSON := d.Get("model_json").(string)
panelJSON, _ := unmarshalLibraryPanelModelJSON(modelJSON)
body := models.PatchLibraryElementCommand{
Name: d.Get("name").(string),
Model: panelJSON,
Kind: 1,
Version: int64(d.Get("version").(int)),
}
if folderUID, ok := d.GetOk("folder_uid"); ok {
_, body.FolderUID = SplitOrgResourceID(folderUID.(string))
} else if folderIDStr, ok := d.GetOk("folder_id"); ok {
_, folderIDStr = SplitOrgResourceID(folderIDStr.(string))
folderID, _ := strconv.ParseInt(folderIDStr.(string), 10, 64)
// nolint:staticcheck
body.FolderID = folderID
}
resp, err := client.LibraryElements.UpdateLibraryElement(uid, &body)
if err != nil {
return diag.FromErr(err)
}
updatedPanel := resp.Payload.Result
d.SetId(MakeOrgResourceID(updatedPanel.OrgID, updatedPanel.UID))
return readLibraryPanel(ctx, d, meta)
}
func deleteLibraryPanel(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, _, uid := OAPIClientFromExistingOrgResource(meta, d.Id())
_, err := client.LibraryElements.DeleteLibraryElementByUID(uid)
diag, _ := common.CheckReadError("library panel", d, err)
return diag
}
func makeLibraryPanel(d *schema.ResourceData) models.CreateLibraryElementCommand {
modelJSON := d.Get("model_json").(string)
panelJSON, _ := unmarshalLibraryPanelModelJSON(modelJSON)
panel := models.CreateLibraryElementCommand{
UID: d.Get("uid").(string),
Name: d.Get("name").(string),
Model: panelJSON,
Kind: 1,
}
if folderUID, ok := d.GetOk("folder_uid"); ok {
_, panel.FolderUID = SplitOrgResourceID(folderUID.(string))
} else if folderIDStr, ok := d.GetOk("folder_id"); ok {
_, folderIDStr = SplitOrgResourceID(folderIDStr.(string))
folderID, _ := strconv.ParseInt(folderIDStr.(string), 10, 64)
// nolint:staticcheck
panel.FolderID = folderID
}
return panel
}
// unmarshalLibraryPanelModelJSON is a convenience func for unmarshalling
// `model_json` field.
func unmarshalLibraryPanelModelJSON(modelJSON string) (map[string]interface{}, error) {
unmarshalledJSON := map[string]interface{}{}
err := json.Unmarshal([]byte(modelJSON), &unmarshalledJSON)
if err != nil {
return nil, err
}
return unmarshalledJSON, nil
}
// validateLibraryPanelModelJSON is the ValidateFunc for `model_json`. It
// ensures its value is valid JSON.
func validateLibraryPanelModelJSON(model interface{}, k string) ([]string, []error) {
modelJSON := model.(string)
if _, err := unmarshalLibraryPanelModelJSON(modelJSON); err != nil {
return nil, []error{err}
}
return nil, nil
}
// normalizeLibraryPanelModelJSON is the StateFunc for the `model_json` field.
func normalizeLibraryPanelModelJSON(config interface{}) string {
var modelJSON map[string]interface{}
switch c := config.(type) {
case map[string]interface{}:
modelJSON = c
case string:
var err error
modelJSON, err = unmarshalLibraryPanelModelJSON(c)
if err != nil {
return c
}
}
// replace nil with empty string in JSON
// API will always return model JSON with description and type
if _, ok := modelJSON["description"]; !ok {
modelJSON["description"] = ""
}
if _, ok := modelJSON["type"]; !ok {
modelJSON["type"] = ""
}
j, _ := json.Marshal(modelJSON)
return string(j)
}