Skip to content

Commit 0bab7f1

Browse files
authored
feat(teams) Add the ability to assign CustomRoles to users in teams (#383)
* add custom role data source * add documentation * change from string to set * removed validation because it is done on BE side * update documentation * replace string with constants * update documentation * fixed typo in constants * use randomText instead of acctest.RandStringFromCharSet * validate errors while setting resource data * fix doc
1 parent adcf4be commit 0bab7f1

File tree

8 files changed

+202
-13
lines changed

8 files changed

+202
-13
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package sysdig
2+
3+
import (
4+
"context"
5+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
"strconv"
8+
"time"
9+
)
10+
11+
func dataSourceSysdigCustomRole() *schema.Resource {
12+
timeout := 5 * time.Minute
13+
14+
return &schema.Resource{
15+
ReadContext: dataSourceSysdigCustomRoleRead,
16+
17+
Timeouts: &schema.ResourceTimeout{
18+
Read: schema.DefaultTimeout(timeout),
19+
},
20+
21+
Schema: map[string]*schema.Schema{
22+
SchemaNameKey: {
23+
Type: schema.TypeString,
24+
Required: true,
25+
},
26+
SchemaDescriptionKey: {
27+
Type: schema.TypeString,
28+
Computed: true,
29+
},
30+
SchemaMonitorPermKey: {
31+
Type: schema.TypeSet,
32+
Computed: true,
33+
Elem: &schema.Schema{
34+
Type: schema.TypeString,
35+
},
36+
},
37+
SchemaSecurePermKey: {
38+
Type: schema.TypeSet,
39+
Computed: true,
40+
Elem: &schema.Schema{
41+
Type: schema.TypeString,
42+
},
43+
},
44+
},
45+
}
46+
}
47+
48+
func dataSourceSysdigCustomRoleRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
49+
client, err := m.(SysdigClients).sysdigCommonClientV2()
50+
if err != nil {
51+
return diag.FromErr(err)
52+
}
53+
54+
name := d.Get(SchemaNameKey).(string)
55+
56+
customRole, err := client.GetCustomRoleByName(ctx, name)
57+
if err != nil {
58+
return diag.FromErr(err)
59+
}
60+
61+
d.SetId(strconv.Itoa(customRole.ID))
62+
err = d.Set(SchemaNameKey, customRole.Name)
63+
if err != nil {
64+
return diag.FromErr(err)
65+
}
66+
67+
err = d.Set(SchemaDescriptionKey, customRole.Description)
68+
if err != nil {
69+
return diag.FromErr(err)
70+
}
71+
72+
err = d.Set(SchemaMonitorPermKey, customRole.MonitorPermissions)
73+
if err != nil {
74+
return diag.FromErr(err)
75+
}
76+
77+
err = d.Set(SchemaSecurePermKey, customRole.SecurePermissions)
78+
if err != nil {
79+
return diag.FromErr(err)
80+
}
81+
82+
return nil
83+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//go:build tf_acc_sysdig_monitor || tf_acc_sysdig_secure
2+
3+
package sysdig_test
4+
5+
import (
6+
"fmt"
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
12+
"github.com/draios/terraform-provider-sysdig/sysdig"
13+
)
14+
15+
func TestAccCustomRoleDateSource(t *testing.T) {
16+
rText := randomText(10)
17+
18+
resource.ParallelTest(t, resource.TestCase{
19+
PreCheck: preCheckAnyEnv(t, SysdigMonitorApiTokenEnv, SysdigSecureApiTokenEnv),
20+
ProviderFactories: map[string]func() (*schema.Provider, error){
21+
"sysdig": func() (*schema.Provider, error) {
22+
return sysdig.Provider(), nil
23+
},
24+
},
25+
Steps: []resource.TestStep{
26+
{
27+
Config: getCustomRole(rText),
28+
Check: resource.ComposeAggregateTestCheckFunc(
29+
resource.TestCheckTypeSetElemAttr("data.sysdig_custom_role.custom", "monitor_permissions.*", "token.view"),
30+
resource.TestCheckTypeSetElemAttr("data.sysdig_custom_role.custom", "monitor_permissions.*", "api-token.read"),
31+
resource.TestCheckResourceAttr("data.sysdig_custom_role.custom", "secure_permissions.#", "0"),
32+
),
33+
},
34+
},
35+
})
36+
}
37+
38+
func getCustomRole(name string) string {
39+
return fmt.Sprintf(`
40+
resource "sysdig_custom_role" "test" {
41+
name = "%s"
42+
description = "test"
43+
44+
permissions {
45+
monitor_permissions = ["token.view", "api-token.read"]
46+
}
47+
}
48+
data "sysdig_custom_role" "custom" {
49+
depends_on = [sysdig_custom_role.test]
50+
name = sysdig_custom_role.test.name
51+
}
52+
`, name)
53+
}

sysdig/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ func Provider() *schema.Provider {
174174
"sysdig_current_user": dataSourceSysdigCurrentUser(),
175175
"sysdig_user": dataSourceSysdigUser(),
176176
"sysdig_secure_connection": dataSourceSysdigSecureConnection(),
177+
"sysdig_custom_role": dataSourceSysdigCustomRole(),
177178

178179
"sysdig_fargate_workload_agent": dataSourceSysdigFargateWorkloadAgent(),
179180
"sysdig_monitor_notification_channel_pagerduty": dataSourceSysdigMonitorNotificationChannelPagerduty(),

sysdig/resource_sysdig_monitor_team.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,9 @@ func resourceSysdigMonitorTeam() *schema.Resource {
8686
Required: true,
8787
},
8888
"role": {
89-
Type: schema.TypeString,
90-
Optional: true,
91-
Default: "ROLE_TEAM_STANDARD",
92-
ValidateFunc: validation.StringInSlice([]string{"ROLE_TEAM_STANDARD", "ROLE_TEAM_EDIT", "ROLE_TEAM_READ", "ROLE_TEAM_MANAGER"}, false),
89+
Type: schema.TypeString,
90+
Optional: true,
91+
Default: "ROLE_TEAM_STANDARD",
9392
},
9493
},
9594
},

sysdig/resource_sysdig_secure_team.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1312
)
1413

1514
func resourceSysdigSecureTeam() *schema.Resource {
@@ -93,10 +92,9 @@ func resourceSysdigSecureTeam() *schema.Resource {
9392
},
9493

9594
"role": {
96-
Type: schema.TypeString,
97-
Optional: true,
98-
Default: "ROLE_TEAM_STANDARD",
99-
ValidateFunc: validation.StringInSlice([]string{"ROLE_TEAM_STANDARD", "ROLE_TEAM_EDIT", "ROLE_TEAM_READ", "ROLE_TEAM_MANAGER"}, false),
95+
Type: schema.TypeString,
96+
Optional: true,
97+
Default: "ROLE_TEAM_STANDARD",
10098
},
10199
},
102100
},

website/docs/d/custom_role.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
subcategory: "Sysdig Platform"
3+
layout: "sysdig"
4+
page_title: "Sysdig: sysdig_custom_role"
5+
description: |-
6+
Retrieves information about a custom role from the name
7+
---
8+
9+
# Data Source: sysdig_custom_role
10+
11+
Retrieves information about a custom role from the name.
12+
13+
-> **Note:** Sysdig Terraform Provider is under rapid development at this point. If you experience any issue or discrepancy while using it, please make sure you have the latest version. If the issue persists, or you have a Feature Request to support an additional set of resources, please open a [new issue](https://github.com/sysdiglabs/terraform-provider-sysdig/issues/new) in the GitHub repository.
14+
15+
## Example Usage
16+
17+
```terraform
18+
data "sysdig_custom_role" "custom_role" {
19+
name = "CustomRoleName"
20+
}
21+
```
22+
23+
## Attributes Reference
24+
25+
In addition to all arguments above, the following attributes are exported:
26+
27+
* `id` - The custom role's ID.
28+
29+
* `name` - The custom role's name.
30+
31+
* `description` - The custom role's description.
32+
33+
* `monitor_permissions` - The custom role's monitor permissions.
34+
35+
* `secure_permissions` - The custom role's secure permissions.

website/docs/r/monitor_team.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,19 @@ resource "sysdig_monitor_team" "devops" {
3131
3232
role = "ROLE_TEAM_STANDARD"
3333
}
34+
35+
user_roles {
36+
37+
role = data.sysdig_custom_role.custom_role.id
38+
}
3439
}
3540
3641
data "sysdig_current_user" "me" {
3742
}
43+
44+
data "sysdig_custom_role" "custom_role" {
45+
name = "CustomRoleName"
46+
}
3847
```
3948

4049
## Argument Reference
@@ -78,8 +87,9 @@ data "sysdig_current_user" "me" {
7887
* `email` - (Required) The email of the user in the group.
7988

8089
* `role` - (Optional) The role for the user in this group.
81-
Valid roles are: ROLE_TEAM_STANDARD, ROLE_TEAM_EDIT, ROLE_TEAM_READ, ROLE_TEAM_MANAGER.
82-
Default: ROLE_TEAM_STANDARD.
90+
Valid roles are: ROLE_TEAM_STANDARD, ROLE_TEAM_EDIT, ROLE_TEAM_READ, ROLE_TEAM_MANAGER or CustomRole ID.<br/>
91+
Default: ROLE_TEAM_STANDARD.<br/>
92+
Note: CustomRole ID can be referenced from `sysdig_custom_role` resource or `sysdig_custom_role` data source
8393

8494
## Attributes Reference
8595

website/docs/r/secure_team.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,19 @@ resource "sysdig_secure_team" "devops" {
2727
2828
role = "ROLE_TEAM_STANDARD"
2929
}
30+
31+
user_roles {
32+
33+
role = data.sysdig_custom_role.custom_role.id
34+
}
3035
}
3136
3237
data "sysdig_current_user" "me" {
3338
}
39+
40+
data "sysdig_custom_role" "custom_role" {
41+
name = "CustomRoleName"
42+
}
3443
```
3544

3645
## Argument Reference
@@ -67,8 +76,9 @@ data "sysdig_current_user" "me" {
6776
* `email` - (Required) The email of the user in the group.
6877

6978
* `role` - (Optional) The role for the user in this group.
70-
Valid roles are: ROLE_TEAM_STANDARD, ROLE_TEAM_EDIT, ROLE_TEAM_READ, ROLE_TEAM_MANAGER.
71-
Default: ROLE_TEAM_STANDARD.
79+
Valid roles are: ROLE_TEAM_STANDARD, ROLE_TEAM_EDIT, ROLE_TEAM_READ, ROLE_TEAM_MANAGER or CustomRole ID.<br/>
80+
Default: ROLE_TEAM_STANDARD.<br/>
81+
Note: CustomRole ID can be referenced from `sysdig_custom_role` resource or `sysdig_custom_role` data source
7282

7383
## Attributes Reference
7484

0 commit comments

Comments
 (0)