/
resource_newrelic_one_dashboard_raw.go
280 lines (241 loc) · 7.91 KB
/
resource_newrelic_one_dashboard_raw.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
package newrelic
import (
"context"
"log"
"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/structure"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/newrelic/newrelic-client-go/pkg/common"
"github.com/newrelic/newrelic-client-go/pkg/errors"
)
func resourceNewRelicOneDashboardRaw() *schema.Resource {
return &schema.Resource{
CreateContext: resourceNewRelicOneDashboardRawCreate,
ReadContext: resourceNewRelicOneDashboardRawRead,
UpdateContext: resourceNewRelicOneDashboardRawUpdate,
DeleteContext: resourceNewRelicOneDashboardRawDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
// Required
"name": {
Type: schema.TypeString,
Required: true,
Description: "The dashboard's name.",
},
"page": {
Type: schema.TypeList,
Required: true,
Description: "",
Elem: dashboardRawPageSchemaElem(),
},
// Optional
"account_id": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "The New Relic account ID where you want to create the dashboard.",
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "The dashboard's description.",
},
"permissions": {
Type: schema.TypeString,
Optional: true,
Default: "public_read_only",
ValidateFunc: validation.StringInSlice([]string{"private", "public_read_only", "public_read_write"}, false),
Description: "Determines who can see or edit the dashboard. Valid values are private, public_read_only, public_read_write. Defaults to public_read_only.",
},
// Computed
"guid": {
Type: schema.TypeString,
Computed: true,
Description: "The unique entity identifier of the dashboard in New Relic.",
},
"permalink": {
Type: schema.TypeString,
Computed: true,
Description: "The URL of the dashboard.",
},
},
}
}
// dashboardPageSchemaElem returns the schema for a New Relic dashboard Page
func dashboardRawPageSchemaElem() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"description": {
Type: schema.TypeString,
Optional: true,
Description: "The dashboard page's description.",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "The dashboard page's name.",
},
"guid": {
Type: schema.TypeString,
Computed: true,
Description: "The unique entity identifier of the dashboard page in New Relic.",
},
// All the widget types below
"widget": {
Type: schema.TypeList,
Optional: true,
Description: "A generic widget configuration. Visualization id is required.",
Elem: dashboardRawWidgetSchemaElem(),
},
},
}
}
func dashboardRawWidgetSchemaElem() *schema.Resource {
s := dashboardRawWidgetSchemaBase()
delete(s, "nrql_query") // No queries for Raw
// Possibly call it VisualizationId
s["visualization_id"] = &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The visualization ID of the widget.",
}
// TODO: raw_configuration
s["configuration"] = &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The configuration of the widget.",
DiffSuppressFunc: structure.SuppressJsonDiff,
}
// Expose linked_entity_guids
s["linked_entity_guids"] = &schema.Schema{
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Description: "(Optional) Related entity GUIDs. Currently only supports Dashboard entity GUIDs.",
DiffSuppressFunc: structure.SuppressJsonDiff,
}
return &schema.Resource{
Schema: s,
}
}
func dashboardRawWidgetSchemaBase() map[string]*schema.Schema {
return map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The ID of the widget.",
},
"title": {
Type: schema.TypeString,
Required: true,
Description: "A title for the widget.",
},
"column": {
Type: schema.TypeInt,
Required: true,
},
"height": {
Type: schema.TypeInt,
Optional: true,
Default: 3,
ValidateFunc: validation.IntAtLeast(1),
},
"row": {
Type: schema.TypeInt,
Required: true,
},
"width": {
Type: schema.TypeInt,
Optional: true,
Default: 4,
ValidateFunc: validation.IntBetween(1, 12),
},
}
}
func resourceNewRelicOneDashboardRawCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConfig := meta.(*ProviderConfig)
if !providerConfig.hasNerdGraphCredentials() {
return diag.Errorf("err: NerdGraph support not present, but required for Create")
}
client := providerConfig.NewClient
accountID := selectAccountID(providerConfig, d)
defaultInfo := map[string]interface{}{
"account_id": accountID,
}
dashboard, err := expandDashboardRawInput(d, defaultInfo)
if err != nil {
return diag.FromErr(err)
}
log.Printf("[INFO] Creating New Relic One dashboard: %s", dashboard.Name)
created, err := client.Dashboards.DashboardCreateWithContext(ctx, accountID, *dashboard)
if err != nil {
return diag.FromErr(err)
}
guid := created.EntityResult.GUID
if guid == "" {
var errMessages string
for _, e := range created.Errors {
errMessages += "[" + string(e.Type) + ": " + e.Description + "]"
}
return diag.Errorf("err: newrelic_one_dashboard Create failed: %s", errMessages)
}
d.SetId(string(guid))
return resourceNewRelicOneDashboardRawRead(ctx, d, meta)
}
// resourceNewRelicOneDashboardRawRead NerdGraph => Terraform reader
func resourceNewRelicOneDashboardRawRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConfig := meta.(*ProviderConfig)
if !providerConfig.hasNerdGraphCredentials() {
return diag.Errorf("err: NerdGraph support not present, but required for Read")
}
client := providerConfig.NewClient
log.Printf("[INFO] Reading New Relic One dashboard %s", d.Id())
dashboard, err := client.Dashboards.GetDashboardEntityWithContext(ctx, common.EntityGUID(d.Id()))
if err != nil {
if _, ok := err.(*errors.NotFound); ok {
d.SetId("")
return nil
}
return diag.FromErr(err)
}
return diag.FromErr(flattenDashboardRawEntity(dashboard, d))
}
func resourceNewRelicOneDashboardRawUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConfig := meta.(*ProviderConfig)
if !providerConfig.hasNerdGraphCredentials() {
return diag.Errorf("err: NerdGraph support not present, but required for Update")
}
client := providerConfig.NewClient
accountID := selectAccountID(providerConfig, d)
defaultInfo := map[string]interface{}{
"account_id": accountID,
}
dashboard, err := expandDashboardRawInput(d, defaultInfo)
if err != nil {
return diag.FromErr(err)
}
log.Printf("[INFO] Updating New Relic One dashboard '%s' (%s)", dashboard.Name, d.Id())
result, err := client.Dashboards.DashboardUpdateWithContext(ctx, *dashboard, common.EntityGUID(d.Id()))
if err != nil {
return diag.FromErr(err)
}
// We have to use the Update Result, not a re-read of the entity as the changes take
// some amount of time to be re-indexed
return diag.FromErr(flattenDashboardRawUpdateResult(result, d))
}
func resourceNewRelicOneDashboardRawDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*ProviderConfig).NewClient
log.Printf("[INFO] Deleting New Relic One dashboard %v", d.Id())
if _, err := client.Dashboards.DashboardDeleteWithContext(ctx, common.EntityGUID(d.Id())); err != nil {
if _, ok := err.(*errors.NotFound); ok {
return nil
}
return diag.FromErr(err)
}
return nil
}