Skip to content

Commit 9025bc7

Browse files
committed
remove ZoneWrapper | change Scopes schema
1 parent b03759d commit 9025bc7

File tree

4 files changed

+94
-58
lines changed

4 files changed

+94
-58
lines changed

sysdig/internal/client/v2/model.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,34 +1221,30 @@ type OrganizationSecure struct {
12211221
cloudauth.CloudOrganization
12221222
}
12231223

1224-
type ZoneWrapper struct {
1225-
Zone Zone `json:"zone"`
1226-
}
1227-
12281224
type ZonesWrapper struct {
12291225
Zones []Zone `json:"zones"`
12301226
}
12311227

12321228
type ZoneRequest struct {
12331229
ID int `json:"id,omitempty"`
12341230
Name string `json:"name"`
1235-
Description string `json:"description"`
1236-
Scopes []ZoneScope `json:"zoneScope"`
1231+
Description string `json:"description,omitempty"`
1232+
Scopes []ZoneScope `json:"scopes"`
12371233
}
12381234

12391235
type Zone struct {
12401236
ID int `json:"id"`
12411237
Name string `json:"name"`
1242-
Description string `json:"description"`
1238+
Description string `json:"description,omitempty"`
12431239
Author string `json:"author"`
1244-
LastModifiedBy string `json:"lastModifiedBy"`
1245-
LastUpdated string `json:"lastUpdated"`
1240+
LastModifiedBy string `json:"lastModifiedBy,omitempty"`
1241+
LastUpdated int64 `json:"lastUpdated,omitempty"`
12461242
IsSystem bool `json:"isSystem"`
12471243
Scopes []ZoneScope `json:"scopes"`
12481244
}
12491245

12501246
type ZoneScope struct {
1251-
ID int `json:"id"`
1247+
ID int `json:"id,omitempty"`
12521248
TargetType string `json:"targetType"`
12531249
Rules string `json:"rules"`
12541250
}

sysdig/internal/client/v2/zones.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ func (client *Client) GetZoneById(ctx context.Context, id int) (*Zone, error) {
4242
}
4343
defer response.Body.Close()
4444

45-
wrapper, err := Unmarshal[ZoneWrapper](response.Body)
45+
zone, err := Unmarshal[Zone](response.Body)
4646
if err != nil {
4747
return nil, err
4848
}
4949

50-
return &wrapper.Zone, nil
50+
return &zone, nil
5151
}
5252

5353
func (client *Client) CreateZone(ctx context.Context, zone *ZoneRequest) (*Zone, error) {
@@ -66,12 +66,12 @@ func (client *Client) CreateZone(ctx context.Context, zone *ZoneRequest) (*Zone,
6666
return nil, client.ErrorFromResponse(response)
6767
}
6868

69-
wrapper, err := Unmarshal[ZoneWrapper](response.Body)
69+
createdZone, err := Unmarshal[Zone](response.Body)
7070
if err != nil {
7171
return nil, err
7272
}
7373

74-
return &wrapper.Zone, nil
74+
return &createdZone, nil
7575
}
7676

7777
func (client *Client) UpdateZone(ctx context.Context, zone *ZoneRequest) (*Zone, error) {
@@ -90,12 +90,12 @@ func (client *Client) UpdateZone(ctx context.Context, zone *ZoneRequest) (*Zone,
9090
return nil, client.ErrorFromResponse(response)
9191
}
9292

93-
wrapper, err := Unmarshal[ZoneWrapper](response.Body)
93+
updatedZone, err := Unmarshal[Zone](response.Body)
9494
if err != nil {
9595
return nil, err
9696
}
9797

98-
return &wrapper.Zone, nil
98+
return &updatedZone, nil
9999
}
100100

101101
func (client *Client) DeleteZone(ctx context.Context, id int) error {

sysdig/resource_sysdig_secure_zone.go

Lines changed: 70 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,52 @@ func resourceSysdigZone() *schema.Resource {
1818
DeleteContext: resourceSysdigZoneDelete,
1919

2020
Schema: map[string]*schema.Schema{
21-
"name": {
21+
SchemaNameKey: {
2222
Type: schema.TypeString,
2323
Required: true,
2424
},
25-
"description": {
25+
SchemaDescriptionKey: {
2626
Type: schema.TypeString,
2727
Optional: true,
2828
},
29-
"is_system": {
29+
SchemaIsSystemKey: {
3030
Type: schema.TypeBool,
3131
Computed: true,
3232
},
33-
"author": {
33+
SchemaAuthorKey: {
3434
Type: schema.TypeString,
3535
Computed: true,
3636
},
37-
"last_modified_by": {
37+
SchemaLastModifiedBy: {
3838
Type: schema.TypeString,
3939
Computed: true,
4040
},
41-
"last_updated": {
41+
SchemaLastUpdated: {
4242
Type: schema.TypeString,
4343
Computed: true,
4444
},
45-
"scopes": {
46-
Type: schema.TypeList,
47-
Required: true,
45+
SchemaScopesKey: {
46+
Optional: true,
47+
Type: schema.TypeSet,
48+
MaxItems: 1,
4849
Elem: &schema.Resource{
4950
Schema: map[string]*schema.Schema{
50-
"target_type": {
51-
Type: schema.TypeString,
52-
Required: true,
53-
},
54-
"rules": {
55-
Type: schema.TypeString,
51+
SchemaScopeKey: {
52+
Type: schema.TypeSet,
53+
MinItems: 1,
5654
Required: true,
55+
Elem: &schema.Resource{
56+
Schema: map[string]*schema.Schema{
57+
SchemaTargetTypeKey: {
58+
Type: schema.TypeString,
59+
Required: true,
60+
},
61+
SchemaRulesKey: {
62+
Type: schema.TypeString,
63+
Optional: true,
64+
},
65+
},
66+
},
5767
},
5868
},
5969
},
@@ -86,6 +96,7 @@ func resourceSysdigZoneRead(ctx context.Context, d *schema.ResourceData, m inter
8696
}
8797

8898
id, _ := strconv.Atoi(d.Id())
99+
89100
zone, err := client.GetZoneById(ctx, id)
90101
if err != nil {
91102
d.SetId("")
@@ -94,7 +105,7 @@ func resourceSysdigZoneRead(ctx context.Context, d *schema.ResourceData, m inter
94105

95106
_ = d.Set("name", zone.Name)
96107
_ = d.Set("description", zone.Description)
97-
_ = d.Set("scopes", flattenZoneScopes(zone.Scopes))
108+
_ = d.Set("scopes", fromZoneScopesResponse(zone.Scopes))
98109
_ = d.Set("is_system", zone.IsSystem)
99110
_ = d.Set("author", zone.Author)
100111
_ = d.Set("last_modified_by", zone.LastModifiedBy)
@@ -136,35 +147,64 @@ func resourceSysdigZoneDelete(ctx context.Context, d *schema.ResourceData, m int
136147
}
137148

138149
func zoneRequestFromResourceData(d *schema.ResourceData) *v2.ZoneRequest {
139-
return &v2.ZoneRequest{
140-
ID: d.Get("id").(int),
150+
zoneRequest := &v2.ZoneRequest{
141151
Name: d.Get("name").(string),
142152
Description: d.Get("description").(string),
143-
Scopes: expandZoneScopes(d.Get("scopes").([]interface{})),
153+
Scopes: toZoneScopesRequest(d.Get("scopes").(*schema.Set)),
154+
}
155+
156+
if d.Id() != "" {
157+
id, err := strconv.Atoi(d.Id())
158+
if err == nil {
159+
zoneRequest.ID = id
160+
}
144161
}
162+
163+
return zoneRequest
145164
}
146165

147-
func expandZoneScopes(scopes []interface{}) []v2.ZoneScope {
166+
func toZoneScopesRequest(scopes *schema.Set) []v2.ZoneScope {
148167
var zoneScopes []v2.ZoneScope
149-
for _, scope := range scopes {
150-
scopeMap := scope.(map[string]interface{})
151-
zoneScopes = append(zoneScopes, v2.ZoneScope{
152-
TargetType: scopeMap["target_type"].(string),
153-
Rules: scopeMap["rules"].(string),
154-
})
168+
for _, scopeData := range scopes.List() {
169+
scopeMap := scopeData.(map[string]interface{})
170+
scopeSet := scopeMap[SchemaScopeKey].(*schema.Set)
171+
for _, attr := range scopeSet.List() {
172+
s := attr.(map[string]interface{})
173+
zoneScopes = append(zoneScopes, v2.ZoneScope{
174+
TargetType: s[SchemaTargetTypeKey].(string),
175+
Rules: s[SchemaRulesKey].(string),
176+
})
177+
}
155178
}
156179
return zoneScopes
157180
}
158181

159-
func flattenZoneScopes(scopes []v2.ZoneScope) []interface{} {
182+
func fromZoneScopesResponse(scopes []v2.ZoneScope) []interface{} {
160183
var flattenedScopes []interface{}
161184
for _, scope := range scopes {
162185
flattenedScopes = append(flattenedScopes, map[string]interface{}{
163-
"target_type": scope.TargetType,
164-
"rules": scope.Rules,
186+
SchemaTargetTypeKey: scope.TargetType,
187+
SchemaRulesKey: scope.Rules,
165188
})
166189
}
167-
return flattenedScopes
190+
response := []interface{}{
191+
map[string]interface{}{
192+
SchemaScopeKey: schema.NewSet(schema.HashResource(&schema.Resource{
193+
Schema: map[string]*schema.Schema{
194+
SchemaTargetTypeKey: {
195+
Type: schema.TypeString,
196+
Required: true,
197+
},
198+
SchemaRulesKey: {
199+
Type: schema.TypeString,
200+
Optional: true,
201+
},
202+
},
203+
}), flattenedScopes),
204+
},
205+
}
206+
207+
return response
168208
}
169209

170210
func getZoneClient(clients SysdigClients) (v2.ZoneInterface, error) {

sysdig/resource_sysdig_secure_zone_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ func TestAccSysdigZone_basic(t *testing.T) {
2828
Check: resource.ComposeTestCheckFunc(
2929
resource.TestCheckResourceAttr("sysdig_zone.test", "name", zoneName),
3030
resource.TestCheckResourceAttr("sysdig_zone.test", "description", zoneDescription),
31-
resource.TestCheckResourceAttr("sysdig_zone.test", "scopes.0.target_type", "host"),
32-
resource.TestCheckResourceAttr("sysdig_zone.test", "scopes.0.rules", "host.name = 'example-host'"),
31+
resource.TestCheckResourceAttr("sysdig_zone.test", "scopes.0.target_type", "aws"),
32+
resource.TestCheckResourceAttr("sysdig_zone.test", "scopes.0.rules", "organization in (\"o1\", \"o2\") and account in (\"a1\", \"a2\")"),
3333
),
3434
},
3535
{
@@ -52,12 +52,12 @@ func testAccSysdigZoneConfig(name, description string) string {
5252
resource "sysdig_zone" "test" {
5353
name = "%s"
5454
description = "%s"
55-
scopes = [
56-
{
57-
target_type = "host"
58-
rules = "host.name = 'example-host'"
55+
scopes {
56+
scope {
57+
target_type = "aws"
58+
rules = "organization in (\"o1\", \"o2\") and account in (\"a1\", \"a2\")"
5959
}
60-
]
60+
}
6161
}
6262
`, name, description)
6363
}
@@ -67,12 +67,12 @@ func testAccSysdigZoneConfigUpdatedDescription(name, description string) string
6767
resource "sysdig_zone" "test" {
6868
name = "%s"
6969
description = "%s"
70-
scopes = [
71-
{
72-
target_type = "host"
73-
rules = "host.name = 'example-host'"
70+
scopes {
71+
scope {
72+
target_type = "aws"
73+
rules = "organization in (\"o1\", \"o2\") and account in (\"a1\", \"a2\")"
7474
}
75-
]
75+
}
7676
}
7777
`, name, description)
7878
}

0 commit comments

Comments
 (0)