Skip to content

Commit f118c34

Browse files
authored
feat(posture_zone): add posture zone resource (#373)
1 parent c9ec66e commit f118c34

File tree

10 files changed

+696
-0
lines changed

10 files changed

+696
-0
lines changed

sysdig/common.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,26 @@ package sysdig
33
const (
44
SchemaIDKey = "id"
55
SchemaPoliciesKey = "policies"
6+
SchemaPolicyIDsKey = "policy_ids"
67
SchemaNameKey = "name"
78
SchemaTypeKey = "type"
89
SchemaKindKey = "kind"
910
SchemaDescriptionKey = "description"
1011
SchemaVersionKey = "version"
1112
SchemaLinkKey = "link"
1213
SchemaAuthorsKey = "authors"
14+
SchemaAuthorKey = "author"
15+
SchemaLastModifiedBy = "last_modified_by"
16+
SchemaLastUpdated = "last_updated"
1317
SchemaPublishedDateKey = "published_date"
1418
SchemaMinKubeVersionKey = "min_kube_version"
1519
SchemaMaxKubeVersionKey = "max_kube_version"
1620
SchemaIsCustomKey = "is_custom"
1721
SchemaIsActiveKey = "is_active"
1822
SchemaPlatformKey = "platform"
1923
SchemaZonesKey = "zones"
24+
SchemaScopeKey = "scope"
25+
SchemaScopesKey = "scopes"
26+
SchemaTargetTypeKey = "target_type"
27+
SchemaRulesKey = "rules"
2028
)

sysdig/common_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sysdig_test
22

33
import (
4+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
45
"os"
56
"strings"
67
"testing"
@@ -39,3 +40,7 @@ func sysdigOrIBMMonitorPreCheck(t *testing.T) func() {
3940
}
4041
}
4142
}
43+
44+
func randomText(len int) string {
45+
return acctest.RandStringFromCharSet(len, acctest.CharSetAlphaNum)
46+
}

sysdig/internal/client/v2/client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type Common interface {
4343
UserInterface
4444
TeamInterface
4545
NotificationChannelInterface
46+
IdentityContextInterface
4647
}
4748

4849
type MonitorCommon interface {
@@ -51,6 +52,7 @@ type MonitorCommon interface {
5152

5253
type SecureCommon interface {
5354
PosturePolicyInterface
55+
PostureZoneInterface
5456
}
5557

5658
type Requester interface {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package v2
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
)
8+
9+
const GetIdentityContextPath = "%s/api/identity/context"
10+
11+
type IdentityContextInterface interface {
12+
GetIdentityContext(ctx context.Context) (*IdentityContext, error)
13+
}
14+
15+
func (client *Client) GetIdentityContext(ctx context.Context) (*IdentityContext, error) {
16+
response, err := client.requester.Request(ctx, http.MethodGet, client.GetIdentityContextURL(), nil)
17+
if err != nil {
18+
return nil, err
19+
}
20+
defer response.Body.Close()
21+
22+
if response.StatusCode != http.StatusOK {
23+
return nil, client.ErrorFromResponse(response)
24+
}
25+
26+
return Unmarshal[*IdentityContext](response.Body)
27+
}
28+
29+
func (client *Client) GetIdentityContextURL() string {
30+
return fmt.Sprintf(GetIdentityContextPath, client.config.url)
31+
}

sysdig/internal/client/v2/model.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,3 +627,52 @@ type PosturePolicy struct {
627627
type PostureZonePolicyListResponse struct {
628628
Data []PosturePolicy `json:"data"`
629629
}
630+
631+
type PostureZoneScope struct {
632+
ID string `json:"id,omitempty"`
633+
TargetType string `json:"targetType"`
634+
Rules string `json:"rules"`
635+
}
636+
637+
type PostureZonePolicySlim struct {
638+
ID string `json:"id,omitempty"`
639+
Name string `json:"name"`
640+
Type int `json:"type"`
641+
Kind int `json:"kind"`
642+
}
643+
644+
type PostureZone struct {
645+
ID string `json:"id"`
646+
Name string `json:"name"`
647+
Description string `json:"description"`
648+
Author string `json:"author"`
649+
LastModifiedBy string `json:"lastModifiedBy"`
650+
LastUpdated string `json:"lastUpdated"`
651+
IsSystem bool `json:"isSystem"`
652+
Scopes []PostureZoneScope `json:"scopes"`
653+
Policies []PostureZonePolicySlim `json:"policies"`
654+
}
655+
656+
type PostureZoneRequest struct {
657+
ID string `json:"id"`
658+
Name string `json:"name"`
659+
Description string `json:"description"`
660+
PolicyIDs []string `json:"policyIds"`
661+
Scopes []PostureZoneScope `json:"scopes"`
662+
Username string `json:"username"`
663+
}
664+
665+
type PostureZoneResponse struct {
666+
Data PostureZone `json:"data"`
667+
}
668+
669+
type IdentityContext struct {
670+
IdentityType string `json:"identityType"`
671+
CustomerID int `json:"customerId"`
672+
TeamID int `json:"teamId"`
673+
TeamName string `json:"teamName"`
674+
UserID int `json:"userId"`
675+
Username string `json:"username"`
676+
ServiceAccountID int `json:"serviceAccountId"`
677+
ServiceAccountName string `json:"serviceAccountName"`
678+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package v2
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
)
8+
9+
const (
10+
ZonesPath = "%s/api/cspm/v1/policy/zones"
11+
ZonePath = "%s/api/cspm/v1/policy/zones/%d"
12+
)
13+
14+
type PostureZoneInterface interface {
15+
Base
16+
CreateOrUpdatePostureZone(ctx context.Context, z *PostureZoneRequest) (*PostureZone, error)
17+
GetPostureZone(ctx context.Context, id int) (*PostureZone, error)
18+
DeletePostureZone(ctx context.Context, id int) error
19+
}
20+
21+
func (client *Client) CreateOrUpdatePostureZone(ctx context.Context, r *PostureZoneRequest) (*PostureZone, error) {
22+
if r.ID == "" {
23+
r.ID = "0"
24+
}
25+
26+
payload, err := Marshal(r)
27+
if err != nil {
28+
return nil, err
29+
}
30+
31+
response, err := client.requester.Request(ctx, http.MethodPost, client.createZoneURL(), payload)
32+
if err != nil {
33+
return nil, err
34+
}
35+
defer response.Body.Close()
36+
37+
wrapper, err := Unmarshal[PostureZoneResponse](response.Body)
38+
if err != nil {
39+
return nil, err
40+
}
41+
42+
return &wrapper.Data, nil
43+
}
44+
45+
func (client *Client) GetPostureZone(ctx context.Context, id int) (*PostureZone, error) {
46+
response, err := client.requester.Request(ctx, http.MethodGet, client.getZoneURL(id), nil)
47+
if err != nil {
48+
return nil, err
49+
}
50+
defer response.Body.Close()
51+
52+
wrapper, err := Unmarshal[PostureZoneResponse](response.Body)
53+
if err != nil {
54+
return nil, err
55+
}
56+
57+
return &wrapper.Data, nil
58+
}
59+
60+
func (client *Client) DeletePostureZone(ctx context.Context, id int) error {
61+
response, err := client.requester.Request(ctx, http.MethodDelete, client.getZoneURL(id), nil)
62+
if err != nil {
63+
return err
64+
}
65+
defer response.Body.Close()
66+
67+
if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNotFound {
68+
return client.ErrorFromResponse(response)
69+
}
70+
71+
return nil
72+
}
73+
74+
func (client *Client) createZoneURL() string {
75+
return fmt.Sprintf(ZonesPath, client.config.url)
76+
}
77+
78+
func (client *Client) getZoneURL(id int) string {
79+
return fmt.Sprintf(ZonePath, client.config.url, id)
80+
}

sysdig/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ func Provider() *schema.Provider {
151151
"sysdig_monitor_notification_channel_msteams": resourceSysdigMonitorNotificationChannelMSTeams(),
152152
"sysdig_monitor_team": resourceSysdigMonitorTeam(),
153153
"sysdig_monitor_cloud_account": resourceSysdigMonitorCloudAccount(),
154+
"sysdig_secure_posture_zone": resourceSysdigSecurePostureZone(),
154155
},
155156
DataSourcesMap: map[string]*schema.Resource{
156157
"sysdig_secure_trusted_cloud_identity": dataSourceSysdigSecureTrustedCloudIdentity(),

0 commit comments

Comments
 (0)