Skip to content

Commit 32cebf1

Browse files
authored
feat(teams) add ability to set zones (#374)
1 parent 050f2bc commit 32cebf1

File tree

6 files changed

+97
-6
lines changed

6 files changed

+97
-6
lines changed

sysdig/common.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ const (
2121
SchemaIsActiveKey = "is_active"
2222
SchemaPlatformKey = "platform"
2323
SchemaZonesKey = "zones"
24+
SchemaZonesIDsKey = "zone_ids"
25+
SchemaAllZones = "all_zones"
2426
SchemaScopeKey = "scope"
2527
SchemaScopesKey = "scopes"
2628
SchemaTargetTypeKey = "target_type"

sysdig/internal/client/v2/model.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ type Team struct {
2323
Filter string `json:"filter,omitempty"`
2424
NamespaceFilters *NamespaceFilters `json:"namespaceFilters,omitempty"`
2525
DefaultTeam bool `json:"default,omitempty"`
26+
ZoneIDs []int `json:"zoneIds,omitempty"`
27+
AllZones bool `json:"allZones"`
2628
}
2729

2830
type NamespaceFilters struct {

sysdig/resource_sysdig_secure_team.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package sysdig
22

33
import (
44
"context"
5+
"fmt"
56
v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2"
67
"strconv"
78
"time"
@@ -22,7 +23,22 @@ func resourceSysdigSecureTeam() *schema.Resource {
2223
Importer: &schema.ResourceImporter{
2324
StateContext: schema.ImportStatePassthroughContext,
2425
},
26+
CustomizeDiff: func(ctx context.Context, diff *schema.ResourceDiff, i interface{}) error {
27+
plan := diff.GetRawPlan().AsValueMap()
28+
zoneIDsPlan := plan[SchemaZonesIDsKey]
29+
allZonesPlan := plan[SchemaAllZones]
2530

31+
var nonEmptyZoneIDs bool
32+
if !zoneIDsPlan.IsNull() && len(zoneIDsPlan.AsValueSlice()) > 0 {
33+
nonEmptyZoneIDs = true
34+
}
35+
36+
if nonEmptyZoneIDs && allZonesPlan.True() {
37+
return fmt.Errorf("if %s is enabled, %s must be omitted", SchemaAllZones, SchemaZonesIDsKey)
38+
}
39+
40+
return nil
41+
},
2642
Timeouts: &schema.ResourceTimeout{
2743
Create: schema.DefaultTimeout(timeout),
2844
Update: schema.DefaultTimeout(timeout),
@@ -94,6 +110,18 @@ func resourceSysdigSecureTeam() *schema.Resource {
94110
Type: schema.TypeInt,
95111
Computed: true,
96112
},
113+
SchemaZonesIDsKey: {
114+
Optional: true,
115+
Type: schema.TypeList,
116+
Elem: &schema.Schema{
117+
Type: schema.TypeInt,
118+
},
119+
},
120+
SchemaAllZones: {
121+
Optional: true,
122+
Type: schema.TypeBool,
123+
Default: false,
124+
},
97125
},
98126
}
99127
}
@@ -164,6 +192,16 @@ func resourceSysdigSecureTeamRead(ctx context.Context, d *schema.ResourceData, m
164192
_ = d.Set("default_team", t.DefaultTeam)
165193
_ = d.Set("user_roles", userSecureRolesToSet(t.UserRoles))
166194

195+
err = d.Set(SchemaZonesIDsKey, t.ZoneIDs)
196+
if err != nil {
197+
return diag.FromErr(err)
198+
}
199+
200+
err = d.Set(SchemaAllZones, t.AllZones)
201+
if err != nil {
202+
return diag.FromErr(err)
203+
}
204+
167205
if clients.GetClientType() == IBMSecure {
168206
resourceSysdigTeamReadIBM(d, &t)
169207
}
@@ -225,6 +263,7 @@ func resourceSysdigSecureTeamDelete(ctx context.Context, d *schema.ResourceData,
225263
func secureTeamFromResourceData(d *schema.ResourceData, clientType ClientType) v2.Team {
226264
canUseSysdigCapture := d.Get("use_sysdig_capture").(bool)
227265
canUseAwsMetrics := new(bool)
266+
allZones := d.Get(SchemaAllZones).(bool)
228267
t := v2.Team{
229268
Theme: d.Get("theme").(string),
230269
Name: d.Get("name").(string),
@@ -234,6 +273,7 @@ func secureTeamFromResourceData(d *schema.ResourceData, clientType ClientType) v
234273
CanUseSysdigCapture: &canUseSysdigCapture,
235274
CanUseAwsMetrics: canUseAwsMetrics,
236275
DefaultTeam: d.Get("default_team").(bool),
276+
AllZones: allZones,
237277
}
238278

239279
userRoles := make([]v2.UserRoles, 0)
@@ -246,6 +286,12 @@ func secureTeamFromResourceData(d *schema.ResourceData, clientType ClientType) v
246286
}
247287
t.UserRoles = userRoles
248288

289+
zonesData := d.Get("zone_ids").([]interface{})
290+
t.ZoneIDs = make([]int, len(zonesData))
291+
for i, z := range zonesData {
292+
t.ZoneIDs[i] = z.(int)
293+
}
294+
249295
if clientType == IBMSecure {
250296
teamFromResourceDataIBM(d, &t)
251297
}

sysdig/resource_sysdig_secure_team_test.go

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,16 @@ package sysdig_test
55
import (
66
"fmt"
77
"github.com/draios/terraform-provider-sysdig/buildinfo"
8+
"regexp"
89
"testing"
910

10-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1212
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1313

1414
"github.com/draios/terraform-provider-sysdig/sysdig"
1515
)
1616

1717
func TestAccSecureTeam(t *testing.T) {
18-
rText := func() string { return acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) }
19-
2018
resource.ParallelTest(t, resource.TestCase{
2119
PreCheck: preCheckAnyEnv(t, SysdigSecureApiTokenEnv, SysdigIBMSecureAPIKeyEnv),
2220
ProviderFactories: map[string]func() (*schema.Provider, error){
@@ -26,17 +24,29 @@ func TestAccSecureTeam(t *testing.T) {
2624
},
2725
Steps: []resource.TestStep{
2826
{
29-
Config: secureTeamWithName(rText()),
27+
Config: secureTeamWithName(randomText(10)),
3028
},
3129
{
32-
Config: secureTeamMinimumConfiguration(rText()),
30+
Config: secureTeamMinimumConfiguration(randomText(10)),
3331
},
3432
{
35-
Config: secureTeamWithPlatformMetricsIBM(rText()),
33+
Config: secureTeamWithPlatformMetricsIBM(randomText(10)),
3634
SkipFunc: func() (bool, error) {
3735
return !buildinfo.IBMSecure, nil
3836
},
3937
},
38+
{
39+
Config: secureTeamWithPostureZones(randomText(10)),
40+
},
41+
{
42+
Config: secureTeamWithPostureZonesAndAllZones(randomText(10)),
43+
ExpectError: regexp.MustCompile(
44+
fmt.Sprintf("if %s is enabled, %s must be omitted",
45+
sysdig.SchemaAllZones,
46+
sysdig.SchemaZonesIDsKey,
47+
),
48+
),
49+
},
4050
{
4151
ResourceName: "sysdig_secure_team.sample",
4252
ImportState: true,
@@ -72,3 +82,28 @@ resource "sysdig_secure_team" "sample" {
7282
ibm_platform_metrics = "foo in (\"0\") and bar in (\"3\")"
7383
}`, name)
7484
}
85+
86+
func secureTeamWithPostureZones(name string) string {
87+
return fmt.Sprintf(`
88+
resource "sysdig_secure_posture_zone" "z1" {
89+
name = "Zone-%[1]s"
90+
}
91+
92+
resource "sysdig_secure_team" "sample" {
93+
name = "sample-%[1]s"
94+
zone_ids = [sysdig_secure_posture_zone.z1.id]
95+
}`, name)
96+
}
97+
98+
func secureTeamWithPostureZonesAndAllZones(name string) string {
99+
return fmt.Sprintf(`
100+
resource "sysdig_secure_posture_zone" "z1" {
101+
name = "Zone-%[1]s"
102+
}
103+
104+
resource "sysdig_secure_team" "sample" {
105+
name = "sample-%[1]s"
106+
zone_ids = [sysdig_secure_posture_zone.z1.id]
107+
all_zones = true
108+
}`, name)
109+
}

website/docs/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,14 @@ When IBM Workload Protection resources are to be created, this authentication mu
234234
> - `sysdig_monitor_alert_promql`
235235
> - `sysdig_monitor_alert_anomaly`
236236
> - `sysdig_monitor_alert_group_outlier`
237+
> - `sysdig_secure_posture_zone`
237238
>
238239
> And data sources:
239240
> - `sysdig_monitor_notification_channel_pagerduty`
240241
> - `sysdig_monitor_notification_channel_email`
241242
> - `sysdig_current_user`
242243
> - `sysdig_secure_notification_channel`
244+
> - `sysdig_secure_posture_policies`
243245
244246
### Others
245247
* `extra_headers` - (Optional) Defines extra HTTP headers that will be added to the client

website/docs/r/secure_team.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ data "sysdig_current_user" "me" {
5757
Administrators of the account will be automatically added
5858
to every new created team, so they don't need to be added as a
5959
resource in the Terraform manifest.
60+
61+
* `zone_ids` - (Optional) List of zone IDs attached to the team. If `all_zones` is specified this argument needs to be omitted.
62+
63+
* `all_zones` - (Optional) Attach all zones to the team. If this argument is enabled then `zone_ids` needs to be omitted.
6064

6165
### User Role Argument Reference
6266

0 commit comments

Comments
 (0)