Skip to content

Commit edc0967

Browse files
authored
feat(teams): enable on IBM secure (#361)
1 parent af3f215 commit edc0967

File tree

8 files changed

+124
-51
lines changed

8 files changed

+124
-51
lines changed

buildinfo/ibm_secure.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//go:build tf_acc_ibm_secure
2+
3+
package buildinfo
4+
5+
func init() {
6+
IBMSecure = true
7+
}

buildinfo/info.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ var (
55
SysdigMonitor bool
66
SysdigSecure bool
77
IBMMonitor bool
8+
IBMSecure bool
89
)

sysdig/resource_sysdig_monitor_team.go

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -194,21 +194,12 @@ func resourceSysdigMonitorTeamRead(ctx context.Context, d *schema.ResourceData,
194194
_ = d.Set("entrypoint", entrypointToSet(t.EntryPoint))
195195

196196
if clients.GetClientType() == IBMMonitor {
197-
resourceSysdigMonitorTeamReadIBM(d, &t)
197+
resourceSysdigTeamReadIBM(d, &t)
198198
}
199199

200200
return nil
201201
}
202202

203-
func resourceSysdigMonitorTeamReadIBM(d *schema.ResourceData, t *v2.Team) {
204-
var ibmPlatformMetrics *string
205-
if t.NamespaceFilters != nil {
206-
ibmPlatformMetrics = t.NamespaceFilters.IBMPlatformMetrics
207-
}
208-
_ = d.Set("enable_ibm_platform_metrics", t.CanUseBeaconMetrics)
209-
_ = d.Set("ibm_platform_metrics", ibmPlatformMetrics)
210-
}
211-
212203
func userMonitorRolesToSet(userRoles []v2.UserRoles) (res []map[string]interface{}) {
213204
for _, role := range userRoles {
214205
if role.Admin { // Admins are added by default, so skip them
@@ -273,18 +264,6 @@ func resourceSysdigMonitorTeamDelete(ctx context.Context, d *schema.ResourceData
273264
return nil
274265
}
275266

276-
func updateNamespaceFilters(filters *v2.NamespaceFilters, update v2.NamespaceFilters) *v2.NamespaceFilters {
277-
if filters == nil {
278-
filters = &v2.NamespaceFilters{}
279-
}
280-
281-
if update.IBMPlatformMetrics != nil {
282-
filters.IBMPlatformMetrics = update.IBMPlatformMetrics
283-
}
284-
285-
return filters
286-
}
287-
288267
func teamFromResourceData(d *schema.ResourceData, clientType ClientType) v2.Team {
289268
canUseSysdigCapture := d.Get("can_use_sysdig_capture").(bool)
290269
canUseCustomEvents := d.Get("can_see_infrastructure_events").(bool)
@@ -325,15 +304,3 @@ func teamFromResourceData(d *schema.ResourceData, clientType ClientType) v2.Team
325304

326305
return t
327306
}
328-
329-
func teamFromResourceDataIBM(d *schema.ResourceData, t *v2.Team) {
330-
canUseBeaconMetrics := d.Get("enable_ibm_platform_metrics").(bool)
331-
t.CanUseBeaconMetrics = &canUseBeaconMetrics
332-
333-
if v, ok := d.GetOk("ibm_platform_metrics"); ok {
334-
metrics := v.(string)
335-
t.NamespaceFilters = updateNamespaceFilters(t.NamespaceFilters, v2.NamespaceFilters{
336-
IBMPlatformMetrics: &metrics,
337-
})
338-
}
339-
}

sysdig/resource_sysdig_secure_team.go

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ func resourceSysdigSecureTeam() *schema.Resource {
5353
Type: schema.TypeString,
5454
Optional: true,
5555
},
56+
"enable_ibm_platform_metrics": {
57+
Type: schema.TypeBool,
58+
Optional: true,
59+
},
60+
"ibm_platform_metrics": {
61+
Type: schema.TypeString,
62+
Optional: true,
63+
},
5664
"use_sysdig_capture": {
5765
Type: schema.TypeBool,
5866
Optional: true,
@@ -90,13 +98,32 @@ func resourceSysdigSecureTeam() *schema.Resource {
9098
}
9199
}
92100

101+
func getSecureTeamClient(c SysdigClients) (v2.TeamInterface, error) {
102+
var client v2.TeamInterface
103+
var err error
104+
switch c.GetClientType() {
105+
case IBMSecure:
106+
client, err = c.ibmSecureClient()
107+
if err != nil {
108+
return nil, err
109+
}
110+
default:
111+
client, err = c.sysdigSecureClientV2()
112+
if err != nil {
113+
return nil, err
114+
}
115+
}
116+
return client, nil
117+
}
118+
93119
func resourceSysdigSecureTeamCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
94-
client, err := meta.(SysdigClients).sysdigSecureClientV2()
120+
clients := meta.(SysdigClients)
121+
client, err := getSecureTeamClient(clients)
95122
if err != nil {
96123
return diag.FromErr(err)
97124
}
98125

99-
team := secureTeamFromResourceData(d)
126+
team := secureTeamFromResourceData(d, clients.GetClientType())
100127
team.Products = []string{"SDS"}
101128

102129
team, err = client.CreateTeam(ctx, team)
@@ -106,13 +133,15 @@ func resourceSysdigSecureTeamCreate(ctx context.Context, d *schema.ResourceData,
106133

107134
d.SetId(strconv.Itoa(team.ID))
108135
_ = d.Set("version", team.Version)
136+
resourceSysdigSecureTeamRead(ctx, d, meta)
109137

110138
return nil
111139
}
112140

113141
// Retrieves the information of a resource form the file and loads it in Terraform
114142
func resourceSysdigSecureTeamRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
115-
client, err := meta.(SysdigClients).sysdigSecureClientV2()
143+
clients := meta.(SysdigClients)
144+
client, err := getSecureTeamClient(clients)
116145
if err != nil {
117146
return diag.FromErr(err)
118147
}
@@ -135,6 +164,10 @@ func resourceSysdigSecureTeamRead(ctx context.Context, d *schema.ResourceData, m
135164
_ = d.Set("default_team", t.DefaultTeam)
136165
_ = d.Set("user_roles", userSecureRolesToSet(t.UserRoles))
137166

167+
if clients.GetClientType() == IBMSecure {
168+
resourceSysdigTeamReadIBM(d, &t)
169+
}
170+
138171
return nil
139172
}
140173

@@ -153,12 +186,13 @@ func userSecureRolesToSet(userRoles []v2.UserRoles) (res []map[string]interface{
153186
}
154187

155188
func resourceSysdigSecureTeamUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
156-
client, err := meta.(SysdigClients).sysdigSecureClientV2()
189+
clients := meta.(SysdigClients)
190+
client, err := getSecureTeamClient(clients)
157191
if err != nil {
158192
return diag.FromErr(err)
159193
}
160194

161-
t := secureTeamFromResourceData(d)
195+
t := secureTeamFromResourceData(d, clients.GetClientType())
162196
t.Products = []string{"SDS"}
163197

164198
t.Version = d.Get("version").(int)
@@ -169,11 +203,12 @@ func resourceSysdigSecureTeamUpdate(ctx context.Context, d *schema.ResourceData,
169203
return diag.FromErr(err)
170204
}
171205

206+
resourceSysdigSecureTeamRead(ctx, d, meta)
172207
return nil
173208
}
174209

175210
func resourceSysdigSecureTeamDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
176-
client, err := meta.(SysdigClients).sysdigSecureClientV2()
211+
client, err := getSecureTeamClient(meta.(SysdigClients))
177212
if err != nil {
178213
return diag.FromErr(err)
179214
}
@@ -187,15 +222,17 @@ func resourceSysdigSecureTeamDelete(ctx context.Context, d *schema.ResourceData,
187222
return nil
188223
}
189224

190-
func secureTeamFromResourceData(d *schema.ResourceData) v2.Team {
225+
func secureTeamFromResourceData(d *schema.ResourceData, clientType ClientType) v2.Team {
191226
canUseSysdigCapture := d.Get("use_sysdig_capture").(bool)
227+
canUseAwsMetrics := new(bool)
192228
t := v2.Team{
193229
Theme: d.Get("theme").(string),
194230
Name: d.Get("name").(string),
195231
Description: d.Get("description").(string),
196232
Show: d.Get("scope_by").(string),
197233
Filter: d.Get("filter").(string),
198234
CanUseSysdigCapture: &canUseSysdigCapture,
235+
CanUseAwsMetrics: canUseAwsMetrics,
199236
DefaultTeam: d.Get("default_team").(bool),
200237
}
201238

@@ -209,5 +246,9 @@ func secureTeamFromResourceData(d *schema.ResourceData) v2.Team {
209246
}
210247
t.UserRoles = userRoles
211248

249+
if clientType == IBMSecure {
250+
teamFromResourceDataIBM(d, &t)
251+
}
252+
212253
return t
213254
}

sysdig/resource_sysdig_secure_team_test.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
//go:build tf_acc_sysdig_secure || tf_acc_sysdig_common
1+
//go:build tf_acc_sysdig_secure || tf_acc_sysdig_common || tf_acc_ibm_secure || tf_acc_ibm_common
22

33
package sysdig_test
44

55
import (
66
"fmt"
7-
"os"
7+
"github.com/draios/terraform-provider-sysdig/buildinfo"
88
"testing"
99

1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
@@ -18,11 +18,7 @@ func TestAccSecureTeam(t *testing.T) {
1818
rText := func() string { return acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) }
1919

2020
resource.ParallelTest(t, resource.TestCase{
21-
PreCheck: func() {
22-
if v := os.Getenv("SYSDIG_SECURE_API_TOKEN"); v == "" {
23-
t.Fatal("SYSDIG_SECURE_API_TOKEN must be set for acceptance tests")
24-
}
25-
},
21+
PreCheck: preCheckAnyEnv(t, SysdigSecureApiTokenEnv, SysdigIBMSecureAPIKeyEnv),
2622
ProviderFactories: map[string]func() (*schema.Provider, error){
2723
"sysdig": func() (*schema.Provider, error) {
2824
return sysdig.Provider(), nil
@@ -35,6 +31,12 @@ func TestAccSecureTeam(t *testing.T) {
3531
{
3632
Config: secureTeamMinimumConfiguration(rText()),
3733
},
34+
{
35+
Config: secureTeamWithPlatformMetricsIBM(rText()),
36+
SkipFunc: func() (bool, error) {
37+
return !buildinfo.IBMSecure, nil
38+
},
39+
},
3840
{
3941
ResourceName: "sysdig_secure_team.sample",
4042
ImportState: true,
@@ -61,3 +63,12 @@ resource "sysdig_secure_team" "sample" {
6163
name = "sample-%s"
6264
}`, name)
6365
}
66+
67+
func secureTeamWithPlatformMetricsIBM(name string) string {
68+
return fmt.Sprintf(`
69+
resource "sysdig_secure_team" "sample" {
70+
name = "sample-%s"
71+
enable_ibm_platform_metrics = true
72+
ibm_platform_metrics = "foo in (\"0\") and bar in (\"3\")"
73+
}`, name)
74+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package sysdig
2+
3+
import (
4+
v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2"
5+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
6+
)
7+
8+
func resourceSysdigTeamReadIBM(d *schema.ResourceData, t *v2.Team) {
9+
var ibmPlatformMetrics *string
10+
if t.NamespaceFilters != nil {
11+
ibmPlatformMetrics = t.NamespaceFilters.IBMPlatformMetrics
12+
}
13+
_ = d.Set("enable_ibm_platform_metrics", t.CanUseBeaconMetrics)
14+
_ = d.Set("ibm_platform_metrics", ibmPlatformMetrics)
15+
}
16+
17+
func updateNamespaceFilters(filters *v2.NamespaceFilters, update v2.NamespaceFilters) *v2.NamespaceFilters {
18+
if filters == nil {
19+
filters = &v2.NamespaceFilters{}
20+
}
21+
22+
if update.IBMPlatformMetrics != nil {
23+
filters.IBMPlatformMetrics = update.IBMPlatformMetrics
24+
}
25+
26+
return filters
27+
}
28+
29+
func teamFromResourceDataIBM(d *schema.ResourceData, t *v2.Team) {
30+
canUseBeaconMetrics := d.Get("enable_ibm_platform_metrics").(bool)
31+
t.CanUseBeaconMetrics = &canUseBeaconMetrics
32+
33+
if v, ok := d.GetOk("ibm_platform_metrics"); ok {
34+
metrics := v.(string)
35+
t.NamespaceFilters = updateNamespaceFilters(t.NamespaceFilters, v2.NamespaceFilters{
36+
IBMPlatformMetrics: &metrics,
37+
})
38+
}
39+
}

website/docs/index.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,11 @@ When IBM Workload Protection resources are to be created, this authentication mu
209209
It can also be configured from the `SYSDIG_SECURE_TEAM_NAME` environment variable.<br/><br/>
210210

211211
> **Note**
212-
> Enabling this way of authentication is under active development.
212+
> Enabling resources and data sources on IBM is under active development.
213213
>
214-
> For now, you can manage following resources sources on IBM Cloud Monitoring:
214+
> For now, you can manage following resources:
215215
> - `sysdig_monitor_team`
216+
> - `sysdig_secure_team`
216217
> - `sysdig_monitor_notification_channel_email`
217218
> - `sysdig_secure_notification_channel_email`
218219
> - `sysdig_monitor_notification_channel_opsgenie`

website/docs/r/secure_team.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,16 @@ data "sysdig_current_user" "me" {
7070

7171
No additional attributes are exported.
7272

73+
### IBM Workload protection arguments
74+
75+
* `enable_ibm_platform_metrics` - (Optional) Enable platform metrics on IBM Cloud Monitoring.
76+
77+
* `ibm_platform_metrics` - (Optional) Define platform metrics on IBM Cloud Monitoring.
78+
7379
## Import
7480

7581
Secure Teams can be imported using the ID, e.g.
7682

7783
```
7884
$ terraform import sysdig_secure_team.example 12345
79-
```
85+
```

0 commit comments

Comments
 (0)