Skip to content

Commit 631a98d

Browse files
authored
feat(zone): add Zone resource for Secure (#609)
* add Zone resource for Secure * remove ZoneWrapper | change Scopes schema * fix acceptance test * add documentation * convert `last_updated` to date (string) from timestamp * upgrade actions/cache v2 -> v4 * fix acc test * add Importer to resource schema * refactor: rename method * refactor: rename variables * make `scopes` required in terraform config and add `id` field to `scope` * simplify zone TF schema: remove `scopes` field * add all computed fields to documentation * set scopes properly when updating the state
1 parent aeee262 commit 631a98d

9 files changed

+601
-11
lines changed

sysdig/internal/client/v2/client.go

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ type SecureCommon interface {
6060
PostureControlInterface
6161
PostureAcceptRiskInterface
6262
PostureVulnerabilityAcceptRiskInterface
63+
ZoneInterface
6364
}
6465

6566
type Requester interface {

sysdig/internal/client/v2/client_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ func TestUnmarshal(t *testing.T) {
5757
}
5858

5959
func TestClient_ErrorFromResponse_non_json(t *testing.T) {
60-
6160
givenPayload := "non json body"
6261
expected := "401 Unauthorized"
6362
c := Client{}
@@ -111,7 +110,6 @@ func TestClient_ErrorFromResponse_standard_error_format(t *testing.T) {
111110
}
112111

113112
func TestClient_ErrorFromResponse_standard_error_format_2(t *testing.T) {
114-
115113
givenPayload := `
116114
{
117115
"timestamp" : 1715255725613,

sysdig/internal/client/v2/model.go

+28
Original file line numberDiff line numberDiff line change
@@ -1223,3 +1223,31 @@ type AgentAccessKeyWriteWrapper struct {
12231223
type OrganizationSecure struct {
12241224
cloudauth.CloudOrganization
12251225
}
1226+
1227+
type ZonesWrapper struct {
1228+
Zones []Zone `json:"zones"`
1229+
}
1230+
1231+
type ZoneRequest struct {
1232+
ID int `json:"id,omitempty"`
1233+
Name string `json:"name"`
1234+
Description string `json:"description,omitempty"`
1235+
Scopes []ZoneScope `json:"scopes"`
1236+
}
1237+
1238+
type Zone struct {
1239+
ID int `json:"id"`
1240+
Name string `json:"name"`
1241+
Description string `json:"description,omitempty"`
1242+
Author string `json:"author"`
1243+
LastModifiedBy string `json:"lastModifiedBy,omitempty"`
1244+
LastUpdated int64 `json:"lastUpdated,omitempty"`
1245+
IsSystem bool `json:"isSystem"`
1246+
Scopes []ZoneScope `json:"scopes"`
1247+
}
1248+
1249+
type ZoneScope struct {
1250+
ID int `json:"id,omitempty"`
1251+
TargetType string `json:"targetType"`
1252+
Rules string `json:"rules"`
1253+
}

sysdig/internal/client/v2/posture_zones.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
)
88

99
const (
10-
ZonesPath = "%s/api/cspm/v1/policy/zones"
11-
ZonePath = "%s/api/cspm/v1/policy/zones/%d"
10+
PostureZonesPath = "%s/api/cspm/v1/policy/zones"
11+
PostureZonePath = "%s/api/cspm/v1/policy/zones/%d"
1212
)
1313

1414
type PostureZoneInterface interface {
@@ -28,7 +28,7 @@ func (client *Client) CreateOrUpdatePostureZone(ctx context.Context, r *PostureZ
2828
return nil, "", err
2929
}
3030

31-
response, err := client.requester.Request(ctx, http.MethodPost, client.createZoneURL(), payload)
31+
response, err := client.requester.Request(ctx, http.MethodPost, client.createPostureZoneURL(), payload)
3232
if err != nil {
3333
return nil, "", err
3434
}
@@ -48,7 +48,7 @@ func (client *Client) CreateOrUpdatePostureZone(ctx context.Context, r *PostureZ
4848
}
4949

5050
func (client *Client) GetPostureZone(ctx context.Context, id int) (*PostureZone, error) {
51-
response, err := client.requester.Request(ctx, http.MethodGet, client.getZoneURL(id), nil)
51+
response, err := client.requester.Request(ctx, http.MethodGet, client.getPostureZoneURL(id), nil)
5252
if err != nil {
5353
return nil, err
5454
}
@@ -63,7 +63,7 @@ func (client *Client) GetPostureZone(ctx context.Context, id int) (*PostureZone,
6363
}
6464

6565
func (client *Client) DeletePostureZone(ctx context.Context, id int) error {
66-
response, err := client.requester.Request(ctx, http.MethodDelete, client.getZoneURL(id), nil)
66+
response, err := client.requester.Request(ctx, http.MethodDelete, client.getPostureZoneURL(id), nil)
6767
if err != nil {
6868
return err
6969
}
@@ -76,10 +76,10 @@ func (client *Client) DeletePostureZone(ctx context.Context, id int) error {
7676
return nil
7777
}
7878

79-
func (client *Client) createZoneURL() string {
80-
return fmt.Sprintf(ZonesPath, client.config.url)
79+
func (client *Client) createPostureZoneURL() string {
80+
return fmt.Sprintf(PostureZonesPath, client.config.url)
8181
}
8282

83-
func (client *Client) getZoneURL(id int) string {
84-
return fmt.Sprintf(ZonePath, client.config.url, id)
83+
func (client *Client) getPostureZoneURL(id int) string {
84+
return fmt.Sprintf(PostureZonePath, client.config.url, id)
8585
}

sysdig/internal/client/v2/zones.go

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package v2
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
)
8+
9+
const (
10+
PlatformZonesPath = "%s/platform/v1/zones"
11+
PlatformZonePath = "%s/platform/v1/zones/%d"
12+
)
13+
14+
type ZoneInterface interface {
15+
Base
16+
GetZones(ctx context.Context) ([]Zone, error)
17+
GetZoneById(ctx context.Context, id int) (*Zone, error)
18+
CreateZone(ctx context.Context, zone *ZoneRequest) (*Zone, error)
19+
UpdateZone(ctx context.Context, zone *ZoneRequest) (*Zone, error)
20+
DeleteZone(ctx context.Context, id int) error
21+
}
22+
23+
func (client *Client) GetZones(ctx context.Context) ([]Zone, error) {
24+
response, err := client.requester.Request(ctx, http.MethodGet, client.getZonesURL(), nil)
25+
if err != nil {
26+
return nil, err
27+
}
28+
defer response.Body.Close()
29+
30+
wrapper, err := Unmarshal[ZonesWrapper](response.Body)
31+
if err != nil {
32+
return nil, err
33+
}
34+
35+
return wrapper.Zones, nil
36+
}
37+
38+
func (client *Client) GetZoneById(ctx context.Context, id int) (*Zone, error) {
39+
response, err := client.requester.Request(ctx, http.MethodGet, client.getZoneURL(id), nil)
40+
if err != nil {
41+
return nil, err
42+
}
43+
defer response.Body.Close()
44+
45+
zone, err := Unmarshal[Zone](response.Body)
46+
if err != nil {
47+
return nil, err
48+
}
49+
50+
return &zone, nil
51+
}
52+
53+
func (client *Client) CreateZone(ctx context.Context, zone *ZoneRequest) (*Zone, error) {
54+
payload, err := Marshal(zone)
55+
if err != nil {
56+
return nil, err
57+
}
58+
59+
response, err := client.requester.Request(ctx, http.MethodPost, client.getZonesURL(), payload)
60+
if err != nil {
61+
return nil, err
62+
}
63+
defer response.Body.Close()
64+
65+
if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated {
66+
return nil, client.ErrorFromResponse(response)
67+
}
68+
69+
createdZone, err := Unmarshal[Zone](response.Body)
70+
if err != nil {
71+
return nil, err
72+
}
73+
74+
return &createdZone, nil
75+
}
76+
77+
func (client *Client) UpdateZone(ctx context.Context, zone *ZoneRequest) (*Zone, error) {
78+
payload, err := Marshal(zone)
79+
if err != nil {
80+
return nil, err
81+
}
82+
83+
response, err := client.requester.Request(ctx, http.MethodPut, client.getZoneURL(zone.ID), payload)
84+
if err != nil {
85+
return nil, err
86+
}
87+
defer response.Body.Close()
88+
89+
if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated {
90+
return nil, client.ErrorFromResponse(response)
91+
}
92+
93+
updatedZone, err := Unmarshal[Zone](response.Body)
94+
if err != nil {
95+
return nil, err
96+
}
97+
98+
return &updatedZone, nil
99+
}
100+
101+
func (client *Client) DeleteZone(ctx context.Context, id int) error {
102+
response, err := client.requester.Request(ctx, http.MethodDelete, client.getZoneURL(id), nil)
103+
if err != nil {
104+
return err
105+
}
106+
defer response.Body.Close()
107+
108+
if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNotFound {
109+
return client.ErrorFromResponse(response)
110+
}
111+
112+
return nil
113+
}
114+
115+
func (client *Client) getZonesURL() string {
116+
return fmt.Sprintf(PlatformZonesPath, client.config.url)
117+
}
118+
119+
func (client *Client) getZoneURL(id int) string {
120+
return fmt.Sprintf(PlatformZonePath, client.config.url, id)
121+
}

sysdig/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ func (p *SysdigProvider) Provider() *schema.Provider {
199199
"sysdig_secure_posture_control": resourceSysdigSecurePostureControl(),
200200
"sysdig_secure_posture_accept_risk": resourceSysdigSecureAcceptPostureRisk(),
201201
"sysdig_secure_vulnerability_accept_risk": resourceSysdigSecureVulnerabilityAcceptRisk(),
202+
"sysdig_secure_zone": resourceSysdigSecureZone(),
202203
},
203204
DataSourcesMap: map[string]*schema.Resource{
204205
"sysdig_secure_agentless_scanning_assets": dataSourceSysdigSecureAgentlessScanningAssets(),

0 commit comments

Comments
 (0)