Skip to content

Commit cb59e61

Browse files
authored
feat(secure): add name-based lookup to sysdig_secure_posture_policy data source (#723)
The `sysdig_secure_posture_policy` data source now supports lookup by `name` in addition to `id`, using `ExactlyOneOf` validation. Name lookup lists all policies, filters by exact match, then fetches the full detail by ID. - Add `name` as optional input with `ExactlyOneOf` id/name - Make `id` optional (was required) - Two-step name lookup: list policies, match name, fetch full detail by ID - Add acceptance test for name-based lookup - Update documentation with both lookup examples
1 parent 5c44c13 commit cb59e61

3 files changed

Lines changed: 75 additions & 13 deletions

File tree

sysdig/data_source_sysdig_secure_posture_policy.go

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package sysdig
22

33
import (
44
"context"
5+
"fmt"
56
"strconv"
67
"time"
78

@@ -17,12 +18,16 @@ func dataSourceSysdigSecurePosturePolicy() *schema.Resource {
1718
},
1819
Schema: map[string]*schema.Schema{
1920
SchemaIDKey: {
20-
Type: schema.TypeString,
21-
Required: true,
21+
Type: schema.TypeString,
22+
Optional: true,
23+
Computed: true,
24+
ExactlyOneOf: []string{SchemaIDKey, SchemaNameKey},
2225
},
2326
SchemaNameKey: {
24-
Type: schema.TypeString,
25-
Computed: true,
27+
Type: schema.TypeString,
28+
Optional: true,
29+
Computed: true,
30+
ExactlyOneOf: []string{SchemaIDKey, SchemaNameKey},
2631
},
2732
SchemaDescriptionKey: {
2833
Type: schema.TypeString,
@@ -67,11 +72,38 @@ func dataSourceSysdigSecurePosturePolicyRead(ctx context.Context, d *schema.Reso
6772
return diag.FromErr(err)
6873
}
6974

70-
id, err := strconv.ParseInt(d.Get("id").(string), 10, 64)
71-
if err != nil {
72-
return diag.FromErr(err)
75+
var policyID int64
76+
77+
if idRaw, hasID := d.GetOk(SchemaIDKey); hasID {
78+
policyID, err = strconv.ParseInt(idRaw.(string), 10, 64)
79+
if err != nil {
80+
return diag.FromErr(fmt.Errorf("invalid policy id: %s", err))
81+
}
82+
} else if nameRaw, hasName := d.GetOk(SchemaNameKey); hasName {
83+
name := nameRaw.(string)
84+
policies, listErr := client.ListPosturePolicies(ctx)
85+
if listErr != nil {
86+
return diag.FromErr(fmt.Errorf("error listing posture policies: %s", listErr))
87+
}
88+
var matchedID string
89+
for _, p := range policies {
90+
if p.Name == name {
91+
matchedID = p.ID
92+
break
93+
}
94+
}
95+
if matchedID == "" {
96+
return diag.FromErr(fmt.Errorf("posture policy with name %q not found", name))
97+
}
98+
policyID, err = strconv.ParseInt(matchedID, 10, 64)
99+
if err != nil {
100+
return diag.FromErr(fmt.Errorf("invalid policy id %q: %s", matchedID, err))
101+
}
102+
} else {
103+
return diag.FromErr(fmt.Errorf("either id or name must be specified"))
73104
}
74-
policy, err := client.GetPosturePolicyByID(ctx, id)
105+
106+
policy, err := client.GetPosturePolicyByID(ctx, policyID)
75107
if err != nil {
76108
return diag.FromErr(err)
77109
}

sysdig/data_source_sysdig_secure_posture_policy_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,29 @@ import (
1212
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1313
)
1414

15+
func TestAccPosturePolicyDataSource_ByName(t *testing.T) {
16+
resource.ParallelTest(t, resource.TestCase{
17+
PreCheck: preCheckAnyEnv(t, SysdigSecureApiTokenEnv),
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: `
26+
data "sysdig_secure_posture_policy" "by_name" {
27+
name = "Sysdig Kubernetes"
28+
}`,
29+
Check: resource.ComposeTestCheckFunc(
30+
resource.TestCheckResourceAttr("data.sysdig_secure_posture_policy.by_name", "id", "2"),
31+
resource.TestCheckResourceAttr("data.sysdig_secure_posture_policy.by_name", "name", "Sysdig Kubernetes"),
32+
),
33+
},
34+
},
35+
})
36+
}
37+
1538
func TestAccPosturePolicyDataSource(t *testing.T) {
1639
resource.ParallelTest(t, resource.TestCase{
1740
PreCheck: preCheckAnyEnv(t, SysdigSecureApiTokenEnv),

website/docs/d/secure_posture_policy.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,33 @@ subcategory: "Sysdig Secure"
33
layout: "sysdig"
44
page_title: "Sysdig: sysdig_secure_posture_policy"
55
description: |-
6-
Retrieves Posture policy by ID.
6+
Retrieves Posture policy by ID or name.
77
---
88

99
# Data Source: sysdig_secure_posture_policy
1010

11-
Retrieves the information of a Posture Policy.
11+
Retrieves the information of a Posture Policy by ID or name.
1212

1313
-> **Note:** Sysdig Terraform Provider is under rapid development at this point. If you experience any issue or discrepancy while using it, please make sure you have the latest version. If the issue persists, or you have a Feature Request to support an additional set of resources, please open a [new issue](https://github.com/sysdiglabs/terraform-provider-sysdig/issues/new) in the GitHub repository.
1414

1515
## Example Usage
1616

1717
```terraform
18-
data sysdig_secure_posture_policiy policy {
19-
id = "454678"
18+
data "sysdig_secure_posture_policy" "by_id" {
19+
id = "2"
20+
}
21+
22+
data "sysdig_secure_posture_policy" "by_name" {
23+
name = "Sysdig Kubernetes"
2024
}
2125
```
2226

2327
## Argument Reference
2428

25-
- `id` - (Required) The ID of the Posture Policy, eg. `2`
29+
Exactly one of the following arguments must be provided:
30+
31+
- `id` - (Optional) The ID of the Posture Policy.
32+
- `name` - (Optional) The name of the Posture Policy. Policy names are unique.
2633

2734
## Attributes Reference
2835

0 commit comments

Comments
 (0)