Skip to content

Commit 6d0b647

Browse files
authored
feat(posture-zone): add data source for posture zone (#627)
* add data source for posture zone * fix typo in acc test
1 parent 792a6ac commit 6d0b647

4 files changed

+252
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package sysdig
2+
3+
import (
4+
"context"
5+
"strconv"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
)
10+
11+
func dataSourceSysdigSecurePostureZone() *schema.Resource {
12+
return &schema.Resource{
13+
ReadContext: dataSourceSysdigSecurePostureZoneRead,
14+
15+
Schema: map[string]*schema.Schema{
16+
"id": {
17+
Type: schema.TypeString,
18+
Required: true,
19+
},
20+
"name": {
21+
Type: schema.TypeString,
22+
Computed: true,
23+
},
24+
"description": {
25+
Type: schema.TypeString,
26+
Computed: true,
27+
},
28+
"policy_ids": {
29+
Type: schema.TypeSet,
30+
Computed: true,
31+
Elem: &schema.Schema{
32+
Type: schema.TypeInt,
33+
},
34+
},
35+
"author": {
36+
Type: schema.TypeString,
37+
Computed: true,
38+
},
39+
"last_modified_by": {
40+
Type: schema.TypeString,
41+
Computed: true,
42+
},
43+
"last_updated": {
44+
Type: schema.TypeString,
45+
Computed: true,
46+
},
47+
"scopes": {
48+
Type: schema.TypeSet,
49+
Computed: true,
50+
Elem: &schema.Resource{
51+
Schema: map[string]*schema.Schema{
52+
"target_type": {
53+
Type: schema.TypeString,
54+
Computed: true,
55+
},
56+
"rules": {
57+
Type: schema.TypeString,
58+
Computed: true,
59+
},
60+
},
61+
},
62+
},
63+
},
64+
}
65+
}
66+
67+
func dataSourceSysdigSecurePostureZoneRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
68+
client, err := getPostureZoneClient(meta.(SysdigClients))
69+
if err != nil {
70+
return diag.FromErr(err)
71+
}
72+
73+
id, err := strconv.Atoi(d.Get("id").(string))
74+
if err != nil {
75+
return diag.FromErr(err)
76+
}
77+
78+
postureZone, err := client.GetPostureZone(ctx, id)
79+
if err != nil {
80+
return diag.FromErr(err)
81+
}
82+
83+
d.SetId(postureZone.ID)
84+
err = d.Set("name", postureZone.Name)
85+
if err != nil {
86+
return diag.FromErr(err)
87+
}
88+
89+
err = d.Set("description", postureZone.Description)
90+
if err != nil {
91+
return diag.FromErr(err)
92+
}
93+
94+
err = d.Set("author", postureZone.Author)
95+
if err != nil {
96+
return diag.FromErr(err)
97+
}
98+
99+
err = d.Set("last_modified_by", postureZone.LastModifiedBy)
100+
if err != nil {
101+
return diag.FromErr(err)
102+
}
103+
104+
err = d.Set("last_updated", postureZone.LastUpdated)
105+
if err != nil {
106+
return diag.FromErr(err)
107+
}
108+
109+
pIDs := make([]int, len(postureZone.Policies))
110+
for i, p := range postureZone.Policies {
111+
id, err := strconv.Atoi(p.ID)
112+
if err != nil {
113+
return diag.FromErr(err)
114+
}
115+
pIDs[i] = id
116+
}
117+
err = d.Set("policy_ids", pIDs)
118+
if err != nil {
119+
return diag.FromErr(err)
120+
}
121+
122+
scopes := make([]map[string]interface{}, len(postureZone.Scopes))
123+
for i, s := range postureZone.Scopes {
124+
scopes[i] = map[string]interface{}{
125+
"target_type": s.TargetType,
126+
"rules": s.Rules,
127+
}
128+
}
129+
err = d.Set("scopes", scopes)
130+
if err != nil {
131+
return diag.FromErr(err)
132+
}
133+
134+
return nil
135+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//go:build tf_acc_sysdig_secure || tf_acc_ibm_secure || tf_acc_onprem_secure
2+
3+
package sysdig_test
4+
5+
import (
6+
"fmt"
7+
"github.com/draios/terraform-provider-sysdig/sysdig"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"testing"
10+
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
13+
)
14+
15+
func TestAccDataSourceSysdigSecurePostureZones(t *testing.T) {
16+
resource.Test(t, resource.TestCase{
17+
PreCheck: preCheckAnyEnv(t, SysdigSecureApiTokenEnv, SysdigIBMSecureAPIKeyEnv),
18+
ProviderFactories: map[string]func() (*schema.Provider, error){
19+
"sysdig": func() (*schema.Provider, error) {
20+
return sysdig.Provider(), nil
21+
},
22+
},
23+
Steps: []resource.TestStep{
24+
{
25+
Config: testAccDataSourceSysdigSecurePostureZonesWithMultipleResourcesConfig(),
26+
Check: resource.ComposeTestCheckFunc(
27+
testAccCheckDataSourceSysdigSecurePostureZonesExists("data.sysdig_secure_posture_zone.test_posture_zone"),
28+
resource.TestCheckResourceAttr("data.sysdig_secure_posture_zone.test_posture_zone", "name", "test-zone-1"),
29+
resource.TestCheckResourceAttr("data.sysdig_secure_posture_zone.test_posture_zone", "description", "Test description 1"),
30+
resource.TestCheckTypeSetElemNestedAttrs(
31+
"data.sysdig_secure_posture_zone.test_posture_zone",
32+
"scopes.*",
33+
map[string]string{
34+
"target_type": "aws",
35+
"rules": "organization in (\"o1\", \"o2\") and account in (\"a1\", \"a2\")",
36+
},
37+
),
38+
),
39+
},
40+
},
41+
})
42+
}
43+
44+
func testAccDataSourceSysdigSecurePostureZonesWithMultipleResourcesConfig() string {
45+
return `
46+
resource "sysdig_secure_posture_zone" "test_posture_zone" {
47+
name = "test-zone-1"
48+
description = "Test description 1"
49+
50+
scopes {
51+
scope {
52+
target_type = "aws"
53+
rules = "organization in (\"o1\", \"o2\") and account in (\"a1\", \"a2\")"
54+
}
55+
}
56+
}
57+
58+
data "sysdig_secure_posture_zone" "test_posture_zone" {
59+
id = sysdig_secure_posture_zone.test_posture_zone.id
60+
}
61+
`
62+
}
63+
64+
func testAccCheckDataSourceSysdigSecurePostureZonesExists(resourceName string) resource.TestCheckFunc {
65+
return func(s *terraform.State) error {
66+
rs, ok := s.RootModule().Resources[resourceName]
67+
if !ok {
68+
return fmt.Errorf("not found: %s", resourceName)
69+
}
70+
71+
if rs.Primary.ID == "" {
72+
return fmt.Errorf("no ID is set")
73+
}
74+
75+
return nil
76+
}
77+
}

sysdig/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ func (p *SysdigProvider) Provider() *schema.Provider {
240240
"sysdig_secure_zone": dataSourceSysdigSecureZone(),
241241
"sysdig_secure_team": dataSourceSysdigSecureTeam(),
242242
"sysdig_secure_teams": dataSourceSysdigSecureTeams(),
243+
"sysdig_secure_posture_zone": dataSourceSysdigSecurePostureZone(),
243244

244245
"sysdig_current_user": dataSourceSysdigCurrentUser(),
245246
"sysdig_user": dataSourceSysdigUser(),

website/docs/d/secure_posture_zone.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
subcategory: "Sysdig Secure"
3+
layout: "sysdig"
4+
page_title: "Sysdig: sysdig_secure_posture_zone"
5+
description: |-
6+
Retrieves Posture Zone by ID.
7+
---
8+
9+
# sysdig_secure_posture_zone Data Source
10+
11+
The `sysdig_secure_posture_zone` data source allows you to retrieve information about a specific secure posture zone by its ID.
12+
13+
## Example Usage
14+
15+
```terraform
16+
data "sysdig_secure_posture_zone" "example" {
17+
id = "454678"
18+
}
19+
```
20+
21+
## Argument Reference
22+
23+
The following arguments are supported:
24+
25+
- `id` (Required) - The ID of the secure posture zone to retrieve.
26+
27+
## Attribute Reference
28+
29+
The following attributes are exported:
30+
31+
- `name` - The name of the secure posture zone.
32+
- `description` - The description of the secure posture zone.
33+
- `policy_ids` - A list of policy IDs associated with the secure posture zone.
34+
- `author` - The author of the secure posture zone.
35+
- `last_modified_by` - The user who last modified the secure posture zone.
36+
- `last_updated` - The timestamp of the last update to the secure posture zone.
37+
- `scopes` - A list of scopes associated with the secure posture zone. Each scope contains:
38+
- `target_type` - The target type of the scope.
39+
- `rules` - The rules associated with the scope.

0 commit comments

Comments
 (0)