Skip to content

Commit e41830c

Browse files
committed
Merge branch 'master' into issue/612
2 parents 9614597 + 0a5e6a8 commit e41830c

File tree

12 files changed

+846
-11
lines changed

12 files changed

+846
-11
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package sysdig
2+
3+
import (
4+
"context"
5+
"fmt"
6+
v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2"
7+
"strconv"
8+
"time"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
)
13+
14+
func dataSourceSysdigSecureZone() *schema.Resource {
15+
return &schema.Resource{
16+
ReadContext: dataSourceSysdigSecureZoneRead,
17+
18+
Schema: map[string]*schema.Schema{
19+
SchemaDescriptionKey: {
20+
Type: schema.TypeString,
21+
Computed: true,
22+
},
23+
SchemaIsSystemKey: {
24+
Type: schema.TypeBool,
25+
Computed: true,
26+
},
27+
SchemaAuthorKey: {
28+
Type: schema.TypeString,
29+
Computed: true,
30+
},
31+
SchemaLastModifiedBy: {
32+
Type: schema.TypeString,
33+
Computed: true,
34+
},
35+
SchemaLastUpdated: {
36+
Type: schema.TypeString,
37+
Computed: true,
38+
},
39+
SchemaScopeKey: {
40+
Type: schema.TypeSet,
41+
Computed: true,
42+
Elem: &schema.Resource{
43+
Schema: map[string]*schema.Schema{
44+
SchemaIDKey: {
45+
Type: schema.TypeInt,
46+
Computed: true,
47+
},
48+
SchemaTargetTypeKey: {
49+
Type: schema.TypeString,
50+
Computed: true,
51+
},
52+
SchemaRulesKey: {
53+
Type: schema.TypeString,
54+
Computed: true,
55+
},
56+
},
57+
},
58+
},
59+
"id": {
60+
Type: schema.TypeString,
61+
Optional: true,
62+
ExactlyOneOf: []string{"id", "name"},
63+
Description: "The ID of the zone to retrieve.",
64+
},
65+
"name": {
66+
Type: schema.TypeString,
67+
Optional: true,
68+
ExactlyOneOf: []string{"id", "name"},
69+
Description: "The name of the zone to retrieve.",
70+
},
71+
},
72+
}
73+
}
74+
75+
func dataSourceSysdigSecureZoneRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
76+
client, err := getZoneClient(m.(SysdigClients))
77+
if err != nil {
78+
return diag.FromErr(err)
79+
}
80+
81+
var zone *v2.Zone
82+
zoneIDRaw, hasZoneID := d.GetOk("id")
83+
if hasZoneID {
84+
zoneID, err := strconv.Atoi(zoneIDRaw.(string))
85+
if err != nil {
86+
return diag.FromErr(fmt.Errorf("invalid zone id: %s", err))
87+
}
88+
zone, err = client.GetZoneById(ctx, zoneID)
89+
if err != nil {
90+
return diag.FromErr(fmt.Errorf("error fetching zone by ID: %s", err))
91+
}
92+
} else if nameRaw, hasName := d.GetOk("name"); hasName {
93+
name := nameRaw.(string)
94+
zones, err := client.GetZones(ctx, name)
95+
if err != nil {
96+
return diag.FromErr(fmt.Errorf("error fetching zones: %s", err))
97+
}
98+
for _, z := range zones {
99+
if z.Name == name {
100+
zone = &z
101+
break
102+
}
103+
}
104+
if zone == nil {
105+
return diag.FromErr(fmt.Errorf("zone with name '%s' not found", name))
106+
}
107+
} else {
108+
return diag.FromErr(fmt.Errorf("either id or name must be specified"))
109+
}
110+
111+
d.SetId(fmt.Sprintf("%d", zone.ID))
112+
_ = d.Set(SchemaNameKey, zone.Name)
113+
_ = d.Set(SchemaDescriptionKey, zone.Description)
114+
_ = d.Set(SchemaIsSystemKey, zone.IsSystem)
115+
_ = d.Set(SchemaAuthorKey, zone.Author)
116+
_ = d.Set(SchemaLastModifiedBy, zone.LastModifiedBy)
117+
_ = d.Set(SchemaLastUpdated, time.UnixMilli(zone.LastUpdated).Format(time.RFC3339))
118+
119+
if err := d.Set(SchemaScopeKey, fromZoneScopesResponse(zone.Scopes)); err != nil {
120+
return diag.FromErr(fmt.Errorf("error setting scope: %s", err))
121+
}
122+
123+
return nil
124+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//go:build tf_acc_sysdig_secure || tf_acc_onprem_secure
2+
3+
package sysdig_test
4+
5+
import (
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
11+
"github.com/draios/terraform-provider-sysdig/sysdig"
12+
)
13+
14+
func TestAccDataSourceSysdigSecureZone(t *testing.T) {
15+
resource.ParallelTest(t, resource.TestCase{
16+
PreCheck: preCheckAnyEnv(t, SysdigSecureApiTokenEnv),
17+
ProviderFactories: map[string]func() (*schema.Provider, error){
18+
"sysdig": func() (*schema.Provider, error) {
19+
return sysdig.Provider(), nil
20+
},
21+
},
22+
Steps: []resource.TestStep{
23+
{
24+
Config: testAccDataSourceSysdigSecureZoneConfig(),
25+
Check: resource.ComposeTestCheckFunc(
26+
resource.TestCheckResourceAttr("data.sysdig_secure_zone.test", "name", "test-secure-zone"),
27+
resource.TestCheckResourceAttrSet("data.sysdig_secure_zone.test", "description"),
28+
resource.TestCheckResourceAttrSet("data.sysdig_secure_zone.test", "is_system"),
29+
resource.TestCheckResourceAttrSet("data.sysdig_secure_zone.test", "author"),
30+
resource.TestCheckResourceAttrSet("data.sysdig_secure_zone.test", "last_modified_by"),
31+
resource.TestCheckResourceAttrSet("data.sysdig_secure_zone.test", "last_updated"),
32+
resource.TestCheckTypeSetElemNestedAttrs(
33+
"data.sysdig_secure_zone.test",
34+
"scope.*",
35+
map[string]string{
36+
"target_type": "aws",
37+
"rules": "organization in (\"o1\", \"o2\") and account in (\"a1\", \"a2\")",
38+
},
39+
),
40+
),
41+
},
42+
},
43+
})
44+
}
45+
46+
func testAccDataSourceSysdigSecureZoneConfig() string {
47+
return `
48+
resource "sysdig_secure_zone" "sample" {
49+
name = "test-secure-zone"
50+
description = "Test secure zone"
51+
scope {
52+
target_type = "aws"
53+
rules = "organization in (\"o1\", \"o2\") and account in (\"a1\", \"a2\")"
54+
}
55+
}
56+
57+
data "sysdig_secure_zone" "test" {
58+
depends_on = ["sysdig_secure_zone.sample"]
59+
name = "test-secure-zone"
60+
}
61+
`
62+
}

sysdig/internal/client/v2/client.go

Lines changed: 1 addition & 0 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 28 additions & 0 deletions
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:"data"`
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

Lines changed: 9 additions & 9 deletions
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
}

0 commit comments

Comments
 (0)