Skip to content

Commit ca54ec1

Browse files
Dashboard RBAC roles (#1109)
* WIP of Dashboards RBAC support, uses ForceNew. Looks correct (other than ForceNew not being ideal) * Simplified dashboard resource changes, for use with new API design logic * Remove a computed property left behind from older work * Update docs with restricted_roles (no description until out of beta) * Updated RBAC tests, generated cassette files * Added description for restricted_roles * Double ConflictsWith * Fix datadog_dashboard_json test after merging in prod
1 parent 8a594bb commit ca54ec1

17 files changed

+3445
-7
lines changed

datadog/resource_datadog_dashboard.go

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,26 @@ func resourceDatadogDashboard() *schema.Resource {
7575
Optional: true,
7676
Description: "The description of the dashboard.",
7777
},
78-
"is_read_only": {
79-
Type: schema.TypeBool,
80-
Optional: true,
81-
Default: false,
82-
Description: "Whether this dashboard is read-only.",
83-
},
8478
"url": {
8579
Type: schema.TypeString,
8680
Optional: true,
8781
Computed: true,
8882
Description: "The URL of the dashboard.",
8983
},
84+
"is_read_only": {
85+
Type: schema.TypeBool,
86+
Optional: true,
87+
Default: false,
88+
ConflictsWith: []string{"restricted_roles"},
89+
Description: "Whether this dashboard is read-only.",
90+
},
91+
"restricted_roles": {
92+
Type: schema.TypeSet,
93+
Optional: true,
94+
Elem: &schema.Schema{Type: schema.TypeString},
95+
ConflictsWith: []string{"is_read_only"},
96+
Description: "Role UUIDs corresponding to users authorized to edit the dashboard. **This feature is currently in beta.**",
97+
},
9098
"template_variable": {
9199
Type: schema.TypeList,
92100
Optional: true,
@@ -230,10 +238,16 @@ func updateDashboardState(d *schema.ResourceData, dashboard *datadogV1.Dashboard
230238
if err := d.Set("description", dashboard.GetDescription()); err != nil {
231239
return diag.FromErr(err)
232240
}
241+
if err := d.Set("url", dashboard.GetUrl()); err != nil {
242+
return diag.FromErr(err)
243+
}
244+
245+
// Set RBAC role settings
233246
if err := d.Set("is_read_only", dashboard.GetIsReadOnly()); err != nil {
234247
return diag.FromErr(err)
235248
}
236-
if err := d.Set("url", dashboard.GetUrl()); err != nil {
249+
restrictedRoles := buildTerraformRestrictedRoles(dashboard.RestrictedRoles)
250+
if err := d.Set("restricted_roles", restrictedRoles); err != nil {
237251
return diag.FromErr(err)
238252
}
239253

@@ -315,6 +329,10 @@ func buildDatadogDashboard(d *schema.ResourceData) (*datadogV1.Dashboard, error)
315329
if v, ok := d.GetOk("is_read_only"); ok {
316330
dashboard.SetIsReadOnly(v.(bool))
317331
}
332+
if v, ok := d.GetOk("restricted_roles"); ok && !dashboard.GetIsReadOnly() {
333+
// do not set when 'is_read_only = true' because this takes priority on requests
334+
dashboard.RestrictedRoles = buildDatadogRestrictedRoles(v.(*schema.Set))
335+
}
318336

319337
// Build Widgets
320338
terraformWidgets := d.Get("widget").([]interface{})
@@ -513,6 +531,30 @@ func buildTerraformTemplateVariablePresets(datadogTemplateVariablePresets *[]dat
513531
return &terraformTemplateVariablePresets
514532
}
515533

534+
//
535+
// Restricted Roles helpers
536+
//
537+
538+
func buildDatadogRestrictedRoles(terraformRestrictedRoles *schema.Set) *[]string {
539+
roles := make([]string, 0)
540+
for _, r := range terraformRestrictedRoles.List() {
541+
roles = append(roles, r.(string))
542+
}
543+
return &roles
544+
}
545+
546+
func buildTerraformRestrictedRoles(datadogRestrictedRoles *[]string) *[]string {
547+
if datadogRestrictedRoles == nil {
548+
terraformRestrictedRoles := make([]string, 0)
549+
return &terraformRestrictedRoles
550+
}
551+
terraformRestrictedRoles := make([]string, len(*datadogRestrictedRoles))
552+
for i, roleUUID := range *datadogRestrictedRoles {
553+
terraformRestrictedRoles[i] = roleUUID
554+
}
555+
return &terraformRestrictedRoles
556+
}
557+
516558
//
517559
// Notify List helpers
518560
//
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2021-06-14T14:11:07.039383-04:00

datadog/tests/cassettes/TestAccDatadogDashboardRbac_adminToRbac.yaml

Lines changed: 540 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2021-06-11T16:54:51.041329-04:00
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
---
2+
version: 1
3+
interactions:
4+
- request:
5+
body: |
6+
{"description":"Created using the Datadog provider in Terraform","id":"","is_read_only":true,"layout_type":"ordered","notify_list":[],"template_variable_presets":[],"template_variables":[],"title":"tf-TestAccDatadogDashboardRbac_createAdmin-local-1623444891","widgets":[{"definition":{"content":"note text","has_padding":true,"show_tick":false,"type":"note"}}]}
7+
form: {}
8+
headers:
9+
Accept:
10+
- application/json
11+
Content-Type:
12+
- application/json
13+
Dd-Operation-Id:
14+
- CreateDashboard
15+
User-Agent:
16+
- terraform-provider-datadog/dev (terraform 2.6.1; terraform-cli 0.15.4) datadog-api-client-go/1.0.0-beta.22+dev (go go1.16.3; os darwin; arch amd64)
17+
url: https://api.datadoghq.com/api/v1/dashboard
18+
method: POST
19+
response:
20+
body: '{"notify_list":[],"description":"Created using the Datadog provider in Terraform","author_name":null,"template_variable_presets":[],"template_variables":[],"is_read_only":true,"id":"9cf-bmh-734","title":"tf-TestAccDatadogDashboardRbac_createAdmin-local-1623444891","url":"/dashboard/9cf-bmh-734/tf-testaccdatadogdashboardrbaccreateadmin-local-1623444891","created_at":"2021-06-11T20:55:02.417444+00:00","modified_at":"2021-06-11T20:55:02.417444+00:00","author_handle":"[email protected]","widgets":[{"definition":{"content":"note text","has_padding":true,"show_tick":false,"type":"note"},"id":1539309291479890}],"layout_type":"ordered"}'
21+
headers:
22+
Cache-Control:
23+
- no-cache
24+
Connection:
25+
- keep-alive
26+
Content-Security-Policy:
27+
- frame-ancestors 'self'; report-uri https://api.datadoghq.com/csp-report
28+
Content-Type:
29+
- application/json
30+
Date:
31+
- Fri, 11 Jun 2021 20:55:03 GMT
32+
Pragma:
33+
- no-cache
34+
Strict-Transport-Security:
35+
- max-age=15724800;
36+
Vary:
37+
- Accept-Encoding
38+
X-Content-Type-Options:
39+
- nosniff
40+
X-Dd-Debug:
41+
- 2328yjLSqI4XmR1pVqrPRR/SFcQsbafjEpPmZx7/3PfxUK1nJQQsX+wrMelyVyj+
42+
X-Dd-Version:
43+
- "35.4734225"
44+
X-Frame-Options:
45+
- SAMEORIGIN
46+
status: 200 OK
47+
code: 200
48+
duration: ""
49+
- request:
50+
body: ""
51+
form: {}
52+
headers:
53+
Accept:
54+
- application/json
55+
Dd-Operation-Id:
56+
- GetDashboard
57+
User-Agent:
58+
- terraform-provider-datadog/dev (terraform 2.6.1; terraform-cli 0.15.4) datadog-api-client-go/1.0.0-beta.22+dev (go go1.16.3; os darwin; arch amd64)
59+
url: https://api.datadoghq.com/api/v1/dashboard/9cf-bmh-734
60+
method: GET
61+
response:
62+
body: '{"notify_list":[],"description":"Created using the Datadog provider in Terraform","author_name":null,"template_variable_presets":[],"template_variables":[],"is_read_only":true,"id":"9cf-bmh-734","title":"tf-TestAccDatadogDashboardRbac_createAdmin-local-1623444891","url":"/dashboard/9cf-bmh-734/tf-testaccdatadogdashboardrbaccreateadmin-local-1623444891","created_at":"2021-06-11T20:55:02.417444+00:00","modified_at":"2021-06-11T20:55:02.417444+00:00","author_handle":"[email protected]","widgets":[{"definition":{"content":"note text","has_padding":true,"show_tick":false,"type":"note"},"id":1539309291479890}],"layout_type":"ordered"}'
63+
headers:
64+
Cache-Control:
65+
- no-cache
66+
Connection:
67+
- keep-alive
68+
Content-Security-Policy:
69+
- frame-ancestors 'self'; report-uri https://api.datadoghq.com/csp-report
70+
Content-Type:
71+
- application/json
72+
Date:
73+
- Fri, 11 Jun 2021 20:55:04 GMT
74+
Pragma:
75+
- no-cache
76+
Strict-Transport-Security:
77+
- max-age=15724800;
78+
Vary:
79+
- Accept-Encoding
80+
X-Content-Type-Options:
81+
- nosniff
82+
X-Dd-Debug:
83+
- EFjE6I+AUQmTiNqZcuE1nqoFeAjWD0Xtzy3edDrinkwlU/Wzr/2Dbl5kWk3qLVaQ
84+
X-Dd-Version:
85+
- "35.4734225"
86+
X-Frame-Options:
87+
- SAMEORIGIN
88+
status: 200 OK
89+
code: 200
90+
duration: ""
91+
- request:
92+
body: ""
93+
form: {}
94+
headers:
95+
Accept:
96+
- application/json
97+
Dd-Operation-Id:
98+
- GetDashboard
99+
User-Agent:
100+
- terraform-provider-datadog/dev (terraform 2.6.1; terraform-cli 0.15.4) datadog-api-client-go/1.0.0-beta.22+dev (go go1.16.3; os darwin; arch amd64)
101+
url: https://api.datadoghq.com/api/v1/dashboard/9cf-bmh-734
102+
method: GET
103+
response:
104+
body: '{"notify_list":[],"description":"Created using the Datadog provider in Terraform","author_name":null,"template_variable_presets":[],"template_variables":[],"is_read_only":true,"id":"9cf-bmh-734","title":"tf-TestAccDatadogDashboardRbac_createAdmin-local-1623444891","url":"/dashboard/9cf-bmh-734/tf-testaccdatadogdashboardrbaccreateadmin-local-1623444891","created_at":"2021-06-11T20:55:02.417444+00:00","modified_at":"2021-06-11T20:55:02.417444+00:00","author_handle":"[email protected]","widgets":[{"definition":{"content":"note text","has_padding":true,"show_tick":false,"type":"note"},"id":1539309291479890}],"layout_type":"ordered"}'
105+
headers:
106+
Cache-Control:
107+
- no-cache
108+
Connection:
109+
- keep-alive
110+
Content-Security-Policy:
111+
- frame-ancestors 'self'; report-uri https://api.datadoghq.com/csp-report
112+
Content-Type:
113+
- application/json
114+
Date:
115+
- Fri, 11 Jun 2021 20:55:05 GMT
116+
Pragma:
117+
- no-cache
118+
Strict-Transport-Security:
119+
- max-age=15724800;
120+
Vary:
121+
- Accept-Encoding
122+
X-Content-Type-Options:
123+
- nosniff
124+
X-Dd-Debug:
125+
- EFjE6I+AUQmTiNqZcuE1nqoFeAjWD0Xtzy3edDrinkwlU/Wzr/2Dbl5kWk3qLVaQ
126+
X-Dd-Version:
127+
- "35.4734225"
128+
X-Frame-Options:
129+
- SAMEORIGIN
130+
status: 200 OK
131+
code: 200
132+
duration: ""
133+
- request:
134+
body: ""
135+
form: {}
136+
headers:
137+
Accept:
138+
- application/json
139+
Dd-Operation-Id:
140+
- GetDashboard
141+
User-Agent:
142+
- terraform-provider-datadog/dev (terraform 2.6.1; terraform-cli 0.15.4) datadog-api-client-go/1.0.0-beta.22+dev (go go1.16.3; os darwin; arch amd64)
143+
url: https://api.datadoghq.com/api/v1/dashboard/9cf-bmh-734
144+
method: GET
145+
response:
146+
body: '{"notify_list":[],"description":"Created using the Datadog provider in Terraform","author_name":null,"template_variable_presets":[],"template_variables":[],"is_read_only":true,"id":"9cf-bmh-734","title":"tf-TestAccDatadogDashboardRbac_createAdmin-local-1623444891","url":"/dashboard/9cf-bmh-734/tf-testaccdatadogdashboardrbaccreateadmin-local-1623444891","created_at":"2021-06-11T20:55:02.417444+00:00","modified_at":"2021-06-11T20:55:02.417444+00:00","author_handle":"[email protected]","widgets":[{"definition":{"content":"note text","has_padding":true,"show_tick":false,"type":"note"},"id":1539309291479890}],"layout_type":"ordered"}'
147+
headers:
148+
Cache-Control:
149+
- no-cache
150+
Connection:
151+
- keep-alive
152+
Content-Security-Policy:
153+
- frame-ancestors 'self'; report-uri https://api.datadoghq.com/csp-report
154+
Content-Type:
155+
- application/json
156+
Date:
157+
- Fri, 11 Jun 2021 20:55:09 GMT
158+
Pragma:
159+
- no-cache
160+
Strict-Transport-Security:
161+
- max-age=15724800;
162+
Vary:
163+
- Accept-Encoding
164+
X-Content-Type-Options:
165+
- nosniff
166+
X-Dd-Debug:
167+
- JpIJLwIH2nFlZOC+u71rq7aAOL43MLZN3MUsL+gpYHdZz5QLUOG8Jysf8kVK6tPU
168+
X-Dd-Version:
169+
- "35.4734225"
170+
X-Frame-Options:
171+
- SAMEORIGIN
172+
status: 200 OK
173+
code: 200
174+
duration: ""
175+
- request:
176+
body: ""
177+
form: {}
178+
headers:
179+
Accept:
180+
- application/json
181+
Dd-Operation-Id:
182+
- DeleteDashboard
183+
User-Agent:
184+
- terraform-provider-datadog/dev (terraform 2.6.1; terraform-cli 0.15.4) datadog-api-client-go/1.0.0-beta.22+dev (go go1.16.3; os darwin; arch amd64)
185+
url: https://api.datadoghq.com/api/v1/dashboard/9cf-bmh-734
186+
method: DELETE
187+
response:
188+
body: '{"deleted_dashboard_id":"9cf-bmh-734"}'
189+
headers:
190+
Cache-Control:
191+
- no-cache
192+
Connection:
193+
- keep-alive
194+
Content-Security-Policy:
195+
- frame-ancestors 'self'; report-uri https://api.datadoghq.com/csp-report
196+
Content-Type:
197+
- application/json
198+
Date:
199+
- Fri, 11 Jun 2021 20:55:17 GMT
200+
Pragma:
201+
- no-cache
202+
Strict-Transport-Security:
203+
- max-age=15724800;
204+
Vary:
205+
- Accept-Encoding
206+
X-Content-Type-Options:
207+
- nosniff
208+
X-Dd-Debug:
209+
- L3ULR3HwCWYmEqCWGz2Yob3chcH4pjowBacBXkncP7o+/uPqKt9yGEYf/g1AJPzQ
210+
X-Dd-Version:
211+
- "35.4734225"
212+
X-Frame-Options:
213+
- SAMEORIGIN
214+
status: 200 OK
215+
code: 200
216+
duration: ""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2021-06-11T16:57:48.794947-04:00

0 commit comments

Comments
 (0)