Skip to content

Commit 983be59

Browse files
authored
feat(teams): add data sources for monitor and secure teams (#624)
* add `sysdig_monitor_team` and `sysdig_secure_team` data sources * rename helper methods * fix acc test * update schema resources: Read -> ReadContext * update test data * add `data_source_sysdig_monitor_teams` and `data_source_sysdig_secure_teams` * add md files for new data sources * document `theme` and `version` attributes * populate IBM platform metrics * Revert "populate IBM platform metrics" This reverts commit a508c2b. * populate IBM platform metrics * add tests for IBM * remove platform metrics from secure team schema * add platform metrics fields to monitor team docs * fix acc test * leave only `id` and `name` fields in the `secure_teams` and `monitor_teams` schemas * fix test
1 parent 2b02efb commit 983be59

16 files changed

+788
-1
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package sysdig
2+
3+
import (
4+
"context"
5+
"strconv"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
)
10+
11+
func dataSourceSysdigMonitorTeam() *schema.Resource {
12+
return &schema.Resource{
13+
ReadContext: dataSourceSysdigMonitorTeamRead,
14+
Schema: map[string]*schema.Schema{
15+
"id": {
16+
Type: schema.TypeString,
17+
Required: true,
18+
},
19+
"theme": {
20+
Type: schema.TypeString,
21+
Computed: true,
22+
},
23+
"name": {
24+
Type: schema.TypeString,
25+
Computed: true,
26+
},
27+
"description": {
28+
Type: schema.TypeString,
29+
Computed: true,
30+
},
31+
"scope_by": {
32+
Type: schema.TypeString,
33+
Computed: true,
34+
},
35+
"filter": {
36+
Type: schema.TypeString,
37+
Computed: true,
38+
},
39+
"can_use_sysdig_capture": {
40+
Type: schema.TypeBool,
41+
Computed: true,
42+
},
43+
"can_see_infrastructure_events": {
44+
Type: schema.TypeBool,
45+
Computed: true,
46+
},
47+
"can_use_aws_data": {
48+
Type: schema.TypeBool,
49+
Computed: true,
50+
},
51+
"default_team": {
52+
Type: schema.TypeBool,
53+
Computed: true,
54+
},
55+
"enable_ibm_platform_metrics": {
56+
Type: schema.TypeBool,
57+
Computed: true,
58+
},
59+
"ibm_platform_metrics": {
60+
Type: schema.TypeString,
61+
Computed: true,
62+
},
63+
"user_roles": {
64+
Type: schema.TypeSet,
65+
Computed: true,
66+
Elem: &schema.Resource{
67+
Schema: map[string]*schema.Schema{
68+
"email": {
69+
Type: schema.TypeString,
70+
Computed: true,
71+
},
72+
"role": {
73+
Type: schema.TypeString,
74+
Computed: true,
75+
},
76+
},
77+
},
78+
},
79+
"entrypoint": {
80+
Type: schema.TypeList,
81+
Computed: true,
82+
Elem: &schema.Resource{
83+
Schema: map[string]*schema.Schema{
84+
"type": {
85+
Type: schema.TypeString,
86+
Computed: true,
87+
},
88+
"selection": {
89+
Type: schema.TypeString,
90+
Computed: true,
91+
},
92+
},
93+
},
94+
},
95+
"version": {
96+
Type: schema.TypeInt,
97+
Computed: true,
98+
},
99+
},
100+
}
101+
}
102+
103+
func dataSourceSysdigMonitorTeamRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
104+
clients := meta.(SysdigClients)
105+
client, err := getMonitorTeamClient(clients)
106+
if err != nil {
107+
return diag.FromErr(err)
108+
}
109+
110+
id, err := strconv.Atoi(d.Get("id").(string))
111+
if err != nil {
112+
return diag.FromErr(err)
113+
}
114+
115+
team, err := client.GetTeamById(ctx, id)
116+
if err != nil {
117+
return diag.FromErr(err)
118+
}
119+
120+
d.SetId(strconv.Itoa(team.ID))
121+
_ = d.Set("name", team.Name)
122+
_ = d.Set("theme", team.Theme)
123+
_ = d.Set("description", team.Description)
124+
_ = d.Set("scope_by", team.Show)
125+
_ = d.Set("filter", team.Filter)
126+
_ = d.Set("can_use_sysdig_capture", team.CanUseSysdigCapture)
127+
_ = d.Set("can_see_infrastructure_events", team.CanUseCustomEvents)
128+
_ = d.Set("can_use_aws_data", team.CanUseAwsMetrics)
129+
_ = d.Set("default_team", team.DefaultTeam)
130+
_ = d.Set("user_roles", userMonitorRolesToSet(team.UserRoles))
131+
_ = d.Set("entrypoint", entrypointToSet(team.EntryPoint))
132+
_ = d.Set("version", team.Version)
133+
134+
var ibmPlatformMetrics *string
135+
if team.NamespaceFilters != nil {
136+
ibmPlatformMetrics = team.NamespaceFilters.IBMPlatformMetrics
137+
}
138+
_ = d.Set("enable_ibm_platform_metrics", team.CanUseBeaconMetrics)
139+
_ = d.Set("ibm_platform_metrics", ibmPlatformMetrics)
140+
141+
return nil
142+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//go:build tf_acc_ibm_monitor
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 TestAccDataSourceSysdigMonitorTeamIBM(t *testing.T) {
16+
name := fmt.Sprintf("test-monitor-team-%s", randomText(5))
17+
resource.Test(t, resource.TestCase{
18+
PreCheck: preCheckAnyEnv(t, SysdigIBMMonitorAPIKeyEnv),
19+
ProviderFactories: map[string]func() (*schema.Provider, error){
20+
"sysdig": func() (*schema.Provider, error) {
21+
return sysdig.Provider(), nil
22+
},
23+
},
24+
Steps: []resource.TestStep{
25+
{
26+
Config: monitorTeamWithPlatformMetricsAndDatasourceIBM(name),
27+
Check: resource.ComposeTestCheckFunc(
28+
resource.TestCheckResourceAttr("data.sysdig_monitor_team.test", "name", name),
29+
resource.TestCheckResourceAttr("data.sysdig_monitor_team.test", "enable_ibm_platform_metrics", "true"),
30+
resource.TestCheckResourceAttr("data.sysdig_monitor_team.test", "ibm_platform_metrics", "foo in (\"0\") and bar in (\"3\")"),
31+
),
32+
},
33+
},
34+
})
35+
}
36+
37+
func monitorTeamWithPlatformMetricsAndDatasourceIBM(name string) string {
38+
return fmt.Sprintf(`
39+
resource "sysdig_monitor_team" "sample" {
40+
name = "%s"
41+
enable_ibm_platform_metrics = true
42+
ibm_platform_metrics = "foo in (\"0\") and bar in (\"3\")"
43+
44+
entrypoint {
45+
type = "Dashboards"
46+
}
47+
}
48+
49+
data "sysdig_monitor_team" "test" {
50+
id = sysdig_monitor_team.sample.id
51+
}
52+
`, name)
53+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//go:build tf_acc_sysdig_monitor || tf_acc_onprem_monitor
2+
3+
package sysdig_test
4+
5+
import (
6+
"fmt"
7+
"testing"
8+
9+
"github.com/draios/terraform-provider-sysdig/sysdig"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
13+
)
14+
15+
func TestAccDataSourceSysdigMonitorTeam(t *testing.T) {
16+
name := fmt.Sprintf("test-monitor-team-%s", randomText(5))
17+
resource.Test(t, resource.TestCase{
18+
PreCheck: preCheckAnyEnv(t, SysdigMonitorApiTokenEnv),
19+
ProviderFactories: map[string]func() (*schema.Provider, error){
20+
"sysdig": func() (*schema.Provider, error) {
21+
return sysdig.Provider(), nil
22+
},
23+
},
24+
Steps: []resource.TestStep{
25+
{
26+
Config: monitorTeamResourceAndDatasource(name),
27+
Check: resource.ComposeTestCheckFunc(
28+
resource.TestCheckResourceAttr("data.sysdig_monitor_team.test", "name", name),
29+
resource.TestCheckResourceAttr("data.sysdig_monitor_team.test", "description", "A monitor team"),
30+
resource.TestCheckResourceAttr("data.sysdig_monitor_team.test", "scope_by", "host"),
31+
resource.TestCheckResourceAttr("data.sysdig_monitor_team.test", "filter", "container.image.repo = \"sysdig/agent\""),
32+
resource.TestCheckResourceAttr("data.sysdig_monitor_team.test", "can_use_sysdig_capture", "true"),
33+
resource.TestCheckResourceAttr("data.sysdig_monitor_team.test", "can_see_infrastructure_events", "true"),
34+
resource.TestCheckResourceAttr("data.sysdig_monitor_team.test", "can_use_aws_data", "true"),
35+
),
36+
},
37+
},
38+
})
39+
}
40+
41+
func monitorTeamResourceAndDatasource(name string) string {
42+
return fmt.Sprintf(`
43+
resource "sysdig_monitor_team" "sample" {
44+
name = "%s"
45+
description = "A monitor team"
46+
scope_by = "host"
47+
filter = "container.image.repo = \"sysdig/agent\""
48+
can_use_sysdig_capture = true
49+
can_see_infrastructure_events = true
50+
can_use_aws_data = true
51+
52+
entrypoint {
53+
type = "Dashboards"
54+
}
55+
}
56+
57+
data "sysdig_monitor_team" "test" {
58+
id = sysdig_monitor_team.sample.id
59+
}
60+
`, name)
61+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package sysdig
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
)
9+
10+
func dataSourceSysdigMonitorTeams() *schema.Resource {
11+
return &schema.Resource{
12+
ReadContext: dataSourceSysdigMonitorTeamsRead,
13+
Schema: map[string]*schema.Schema{
14+
"teams": {
15+
Type: schema.TypeList,
16+
Computed: true,
17+
Elem: &schema.Resource{
18+
Schema: map[string]*schema.Schema{
19+
"id": {
20+
Type: schema.TypeInt,
21+
Computed: true,
22+
},
23+
"name": {
24+
Type: schema.TypeString,
25+
Computed: true,
26+
},
27+
},
28+
},
29+
},
30+
},
31+
}
32+
}
33+
34+
func dataSourceSysdigMonitorTeamsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
35+
clients := meta.(SysdigClients)
36+
client, err := getMonitorTeamClient(clients)
37+
if err != nil {
38+
return diag.FromErr(err)
39+
}
40+
41+
teams, err := client.ListTeams(ctx)
42+
if err != nil {
43+
return diag.FromErr(err)
44+
}
45+
46+
var result []map[string]interface{}
47+
for _, team := range teams {
48+
result = append(result, map[string]interface{}{
49+
"id": team.ID,
50+
"name": team.Name,
51+
})
52+
}
53+
d.SetId("sysdig_monitor_teams")
54+
if err := d.Set("teams", result); err != nil {
55+
return diag.FromErr(err)
56+
}
57+
58+
return nil
59+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//go:build tf_acc_sysdig_monitor || tf_acc_onprem_monitor || tf_acc_ibm_monitor
2+
3+
package sysdig_test
4+
5+
import (
6+
"fmt"
7+
"github.com/draios/terraform-provider-sysdig/sysdig"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"testing"
10+
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
12+
)
13+
14+
func TestAccDataSourceSysdigMonitorTeams(t *testing.T) {
15+
resource.Test(t, resource.TestCase{
16+
PreCheck: preCheckAnyEnv(t, SysdigMonitorApiTokenEnv, SysdigIBMMonitorAPIKeyEnv),
17+
ProviderFactories: map[string]func() (*schema.Provider, error){
18+
"sysdig": func() (*schema.Provider, error) {
19+
return sysdig.Provider(), nil
20+
},
21+
},
22+
Steps: []resource.TestStep{
23+
{
24+
Config: testAccDataSourceSysdigMonitorTeamsConfig(randomText(10)),
25+
Check: resource.ComposeTestCheckFunc(
26+
resource.TestCheckResourceAttrSet("data.sysdig_monitor_teams.test", "teams.0.id"),
27+
),
28+
},
29+
},
30+
})
31+
}
32+
33+
func testAccDataSourceSysdigMonitorTeamsConfig(name string) string {
34+
return fmt.Sprintf(`
35+
resource "sysdig_monitor_team" "sample" {
36+
name = "%s"
37+
description = "A monitor secure team"
38+
scope_by = "host"
39+
filter = "container.image.repo = \"sysdig/agent\""
40+
can_use_sysdig_capture = true
41+
can_see_infrastructure_events = true
42+
43+
entrypoint {
44+
type = "Dashboards"
45+
}
46+
}
47+
48+
data "sysdig_monitor_teams" "test" {}
49+
`, name)
50+
}

0 commit comments

Comments
 (0)