Skip to content

Commit f824943

Browse files
rmoeRyan Moetembleking
authored
Add ability to set dashboard sharing options (#146)
* Add ability to set dashboard sharing options Closes: #139 * Add acceptance test and basic docs for dashboard sharing * fix: Solve failing acceptance test * fix: Solve incomplete state being saved Co-authored-by: Ryan Moe <[email protected]> Co-authored-by: Fede Barcelona <[email protected]>
1 parent 0da10fd commit f824943

File tree

4 files changed

+155
-3
lines changed

4 files changed

+155
-3
lines changed

sysdig/internal/client/monitor/model/dashboard.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,16 @@ type TeamSharingOptions struct {
317317
SelectedTeams []interface{} `json:"selectedTeams"`
318318
}
319319

320+
type SharingOptions struct {
321+
Member SharingMember `json:"member"`
322+
Role string `json:"role"`
323+
}
324+
325+
type SharingMember struct {
326+
Type string `json:"type"`
327+
ID int `json:"id"`
328+
}
329+
320330
type ScopeExpressionList struct {
321331
Operand string `json:"operand"`
322332
Operator string `json:"operator"`
@@ -342,7 +352,7 @@ type Dashboard struct {
342352
Description string `json:"description"`
343353
Username string `json:"username"`
344354
Shared bool `json:"shared"`
345-
SharingSettings []interface{} `json:"sharingSettings"`
355+
SharingSettings []*SharingOptions `json:"sharingSettings"`
346356
Public bool `json:"public"`
347357
Favorite bool `json:"favorite"`
348358
CreatedOn int64 `json:"createdOn"`

sysdig/resource_sysdig_monitor_dashboard.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,35 @@ func resourceSysdigMonitorDashboard() *schema.Resource {
5353
ComputedWhen: []string{"public"},
5454
Computed: true,
5555
},
56+
"share": {
57+
Type: schema.TypeSet,
58+
Optional: true,
59+
Elem: &schema.Resource{
60+
Schema: map[string]*schema.Schema{
61+
"member": {
62+
Type: schema.TypeSet,
63+
MaxItems: 1,
64+
Required: true,
65+
Elem: &schema.Resource{
66+
Schema: map[string]*schema.Schema{
67+
"type": {
68+
Type: schema.TypeString,
69+
Required: true,
70+
},
71+
"id": {
72+
Type: schema.TypeInt,
73+
Required: true,
74+
},
75+
},
76+
},
77+
},
78+
"role": {
79+
Type: schema.TypeString,
80+
Required: true,
81+
},
82+
},
83+
},
84+
},
5685
"scope": {
5786
Type: schema.TypeSet,
5887
Optional: true,
@@ -273,9 +302,32 @@ func dashboardFromResourceData(data *schema.ResourceData) (dashboard *model.Dash
273302
dashboard.ScopeExpressionList = scopes
274303

275304
dashboard.AddPanels(panels...)
305+
306+
shares, err := sharingFromResourceData(data)
307+
if err != nil {
308+
return nil, err
309+
}
310+
dashboard.SharingSettings = shares
311+
276312
return dashboard, nil
277313
}
278314

315+
func sharingFromResourceData(data *schema.ResourceData) (sharingSettings []*model.SharingOptions, err error) {
316+
for _, share := range data.Get("share").(*schema.Set).List() {
317+
shareInfo := share.(map[string]interface{})
318+
memberInfo := shareInfo["member"].(*schema.Set).List()[0].(map[string]interface{})
319+
sharingSettings = append(sharingSettings,
320+
&model.SharingOptions{
321+
Member: model.SharingMember{
322+
Type: memberInfo["type"].(string),
323+
ID: memberInfo["id"].(int),
324+
},
325+
Role: shareInfo["role"].(string),
326+
})
327+
}
328+
return
329+
}
330+
279331
func panelsFromResourceData(data *schema.ResourceData) (panels []*model.Panels, err error) {
280332
for _, panelItr := range data.Get("panel").(*schema.Set).List() {
281333
panelInfo := panelItr.(map[string]interface{})
@@ -521,9 +573,30 @@ func dashboardToResourceData(dashboard *model.Dashboard, data *schema.ResourceDa
521573
_ = data.Set("scope", scopes)
522574
_ = data.Set("version", dashboard.Version)
523575

576+
var shares []map[string]interface{}
577+
for _, share := range dashboard.SharingSettings {
578+
dShare, err := shareToResourceData(share)
579+
if err != nil {
580+
return err
581+
}
582+
shares = append(shares, dShare)
583+
}
584+
_ = data.Set("share", shares)
585+
524586
return nil
525587
}
526588

589+
func shareToResourceData(share *model.SharingOptions) (map[string]interface{}, error) {
590+
res := map[string]interface{}{
591+
"role": share.Role,
592+
"member": []map[string]interface{}{{
593+
"type": share.Member.Type,
594+
"id": share.Member.ID,
595+
}},
596+
}
597+
return res, nil
598+
}
599+
527600
func scopeToResourceData(scope *model.ScopeExpressionList) (map[string]interface{}, error) {
528601
res := map[string]interface{}{
529602
"metric": scope.Operand,

sysdig/resource_sysdig_monitor_dashboard_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ func TestAccDashboard(t *testing.T) {
4444
{
4545
Config: multipleUpdatedPanelsDashboard(rText()),
4646
},
47+
{
48+
Config: sharedDashboard(rText()),
49+
Check: resource.ComposeTestCheckFunc(
50+
resource.TestCheckResourceAttr("sysdig_monitor_dashboard.dashboard", "share.#", "1"),
51+
resource.TestCheckTypeSetElemNestedAttrs("sysdig_monitor_dashboard.dashboard", "share.*", map[string]string{
52+
"member.#": "1",
53+
"role": "ROLE_RESOURCE_EDIT",
54+
"member.0.type": "TEAM",
55+
}),
56+
),
57+
},
4758
},
4859
})
4960
}
@@ -225,3 +236,42 @@ resource "sysdig_monitor_dashboard" "dashboard" {
225236
}
226237
`, name, name)
227238
}
239+
240+
func sharedDashboard(name string) string {
241+
return fmt.Sprintf(`
242+
resource "sysdig_monitor_team" "a_team" {
243+
name = "sample-%s"
244+
245+
entrypoint {
246+
type = "Explore"
247+
}
248+
}
249+
250+
resource "sysdig_monitor_dashboard" "dashboard" {
251+
name = "TERRAFORM TEST - METRIC %s"
252+
description = "TERRAFORM TEST - METRIC %s"
253+
254+
panel {
255+
pos_x = 0
256+
pos_y = 0
257+
width = 12 # Maximum size: 24
258+
height = 6
259+
type = "number"
260+
name = "example panel"
261+
description = "description"
262+
263+
query {
264+
promql = "avg(avg_over_time(sysdig_host_cpu_used_percent[$__interval]))"
265+
unit = "percent"
266+
}
267+
}
268+
share {
269+
role = "ROLE_RESOURCE_EDIT"
270+
member {
271+
type = "TEAM"
272+
id = sysdig_monitor_team.a_team.id
273+
}
274+
}
275+
}
276+
`, name, name, name)
277+
}

website/docs/r/monitor_dashboard.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ resource "sysdig_monitor_dashboard" "dashboard" {
9494

9595
* `panel` - (Required) At least 1 panel is required to define a Dashboard.
9696

97+
* `share` - (Optional) Define sharing options for this dashboard.
9798

9899
### scope
99100

@@ -152,7 +153,7 @@ The following arguments are supported:
152153
* `transparent_background` - (Optional) If true, the panel will have a transparent background.
153154
This field is ignored for all panel types except `text`.
154155

155-
### query
156+
### query
156157

157158
To scope a panel built from a PromQL query, you must use a scope variable within the query. The variable will take the value of the referenced scope parameter, and the PromQL panel will change accordingly.
158159
There are two predefined variables available:
@@ -169,6 +170,24 @@ The following arguments are supported:
169170
* `unit` - (Required) The type of metric for this query. Can be one of: `percent`, `data`, `data rate`,
170171
`number`, `number rate`, `time`.
171172

173+
174+
### share
175+
176+
A dashboard can be shared by creating one or more `share` blocks.
177+
178+
The following arguments are supported:
179+
180+
* `role` - (Required) The role to grant to the team.
181+
182+
* `member` - (Required) The team with which to share the dashboard.
183+
184+
Nested scheme for `member`:
185+
186+
* `type`- (Required) Type of member.
187+
188+
* `id` - (Required) ID of member.
189+
190+
172191
## Attributes Reference
173192

174193
In addition to all arguments above, the following attributes are exported:
@@ -196,4 +215,4 @@ Only dashboards that contain supported query types can be imported. Currently su
196215
- Data rate
197216
- Number
198217
- Number rate
199-
- Time
218+
- Time

0 commit comments

Comments
 (0)