Skip to content

Commit 4c19fd8

Browse files
add test file for policies_list
1 parent ecd85ce commit 4c19fd8

6 files changed

+167
-17
lines changed

datadog/fwprovider/resource_datadog_csm_threats_multi_policies.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ func (r *csmThreatsPoliciesListResource) Schema(_ context.Context, _ resource.Sc
6060
},
6161
Blocks: map[string]schema.Block{
6262
"entries": schema.SetNestedBlock{
63-
Description: "A set of policies that belong to this list/batch. All non-listed policies get deleted.",
63+
Description: "A set of policies that belong to this list. Only one policies_list resource can be defined in Terraform, containing all unique policies. All non-listed policies get deleted.",
6464
NestedObject: schema.NestedBlockObject{
6565
Attributes: map[string]schema.Attribute{
6666
"policy_id": schema.StringAttribute{
67-
Description: "The ID of the policy to manage (from `csm_threats_policy`).",
67+
Description: "The ID of the policy to manage (from csm_threats_policy).",
6868
Required: true,
6969
},
7070
"priority": schema.Int64Attribute{

datadog/tests/provider_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ var testFiles2EndpointTags = map[string]string{
115115
"tests/resource_datadog_csm_threats_agent_rule_test": "cloud-workload-security",
116116
"tests/resource_datadog_csm_threats_multi_policy_agent_rule_test": "cloud-workload-security",
117117
"tests/resource_datadog_csm_threats_policy_test": "cloud-workload-security",
118+
"tests/resource_datadog_csm_threats_policies_list_test": "cloud-workload-security",
118119
"tests/resource_datadog_dashboard_alert_graph_test": "dashboards",
119120
"tests/resource_datadog_dashboard_alert_value_test": "dashboards",
120121
"tests/resource_datadog_dashboard_change_test": "dashboards",
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-testing/terraform"
10+
11+
"github.com/terraform-providers/terraform-provider-datadog/datadog/fwprovider"
12+
)
13+
14+
// Create a policies_list and update the name and priority of its policy
15+
func TestAccCSMThreatsPoliciesList_CreateAndUpdate(t *testing.T) {
16+
_, providers, accProviders := testAccFrameworkMuxProviders(context.Background(), t)
17+
18+
resourceName := "datadog_csm_threats_policies_list.all"
19+
20+
resource.Test(t, resource.TestCase{
21+
PreCheck: func() { testAccPreCheck(t) },
22+
ProtoV5ProviderFactories: accProviders,
23+
CheckDestroy: testAccCheckCSMThreatsPoliciesListDestroy(providers.frameworkProvider),
24+
Steps: []resource.TestStep{
25+
{
26+
Config: testAccCSMThreatsPoliciesListConfigBasic(),
27+
Check: resource.ComposeTestCheckFunc(
28+
testAccCheckCSMThreatsPoliciesListExists(providers.frameworkProvider, resourceName),
29+
resource.TestCheckResourceAttr(resourceName, "entries.#", "2"),
30+
resource.TestCheckResourceAttr(resourceName, "entries.0.name", "TERRAFORM_POLICY1"),
31+
resource.TestCheckResourceAttr(resourceName, "entries.0.priority", "2"),
32+
resource.TestCheckResourceAttr(resourceName, "entries.1.name", "TERRAFORM_POLICY2"),
33+
resource.TestCheckResourceAttr(resourceName, "entries.1.priority", "3"),
34+
),
35+
},
36+
{
37+
Config: testAccCSMThreatsPoliciesListConfigUpdate(),
38+
Check: resource.ComposeTestCheckFunc(
39+
testAccCheckCSMThreatsPoliciesListExists(providers.frameworkProvider, resourceName),
40+
resource.TestCheckResourceAttr(resourceName, "entries.#", "2"),
41+
resource.TestCheckResourceAttr(resourceName, "entries.0.name", "TERRAFORM_POLICY1"),
42+
resource.TestCheckResourceAttr(resourceName, "entries.0.priority", "2"),
43+
resource.TestCheckResourceAttr(resourceName, "entries.1.name", "TERRAFORM_POLICY2 UPDATED"),
44+
resource.TestCheckResourceAttr(resourceName, "entries.1.priority", "5"),
45+
),
46+
},
47+
},
48+
})
49+
}
50+
51+
func testAccCheckCSMThreatsPoliciesListExists(accProvider *fwprovider.FrameworkProvider, resourceName string) resource.TestCheckFunc {
52+
return func(s *terraform.State) error {
53+
rs, ok := s.RootModule().Resources[resourceName]
54+
if !ok {
55+
return fmt.Errorf("resource '%s' not found in state", resourceName)
56+
}
57+
if rs.Type != "datadog_csm_threats_policies_list" {
58+
return fmt.Errorf(
59+
"resource %s is not a datadog_csm_threats_policies_list, got: %s",
60+
resourceName,
61+
rs.Type,
62+
)
63+
}
64+
65+
if rs.Primary.ID != "policies_list" {
66+
return fmt.Errorf("expected resource ID to be 'policies_list', got %s", rs.Primary.ID)
67+
}
68+
69+
return nil
70+
}
71+
}
72+
73+
func testAccCheckCSMThreatsPoliciesListDestroy(accProvider *fwprovider.FrameworkProvider) resource.TestCheckFunc {
74+
return func(s *terraform.State) error {
75+
apiInstances := accProvider.DatadogApiInstances
76+
auth := accProvider.Auth
77+
78+
for _, r := range s.RootModule().Resources {
79+
if r.Type != "datadog_csm_threats_policies_list" {
80+
continue
81+
}
82+
83+
resp, httpResponse, err := apiInstances.GetCSMThreatsApiV2().ListCSMThreatsAgentPolicies(auth)
84+
if err != nil {
85+
if httpResponse != nil && httpResponse.StatusCode == 404 {
86+
return nil
87+
}
88+
return fmt.Errorf("Received an error while listing the policies: %s", err)
89+
}
90+
91+
if len(resp.GetData()) > 1 { // CWS_DD is always present
92+
return fmt.Errorf("Policies list not empty, some policies are still present")
93+
}
94+
}
95+
return nil
96+
}
97+
}
98+
99+
func testAccCSMThreatsPoliciesListConfigBasic() string {
100+
return `
101+
resource "datadog_csm_threats_policy" "policy1" {
102+
description = "created with terraform"
103+
enabled = false
104+
tags = []
105+
}
106+
107+
resource "datadog_csm_threats_policy" "policy2" {
108+
description = "created with terraform 2"
109+
enabled = true
110+
tags = ["env:staging"]
111+
}
112+
113+
resource "datadog_csm_threats_policies_list" "all" {
114+
entries {
115+
policy_id = datadog_csm_threats_policy.policy1.id
116+
name = "TERRAFORM_POLICY1"
117+
priority = 2
118+
}
119+
entries {
120+
policy_id = datadog_csm_threats_policy.policy2.id
121+
name = "TERRAFORM_POLICY2"
122+
priority = 3
123+
}
124+
}
125+
`
126+
}
127+
128+
func testAccCSMThreatsPoliciesListConfigUpdate() string {
129+
return `
130+
resource "datadog_csm_threats_policy" "policy1" {
131+
description = "created with terraform"
132+
enabled = false
133+
tags = []
134+
}
135+
136+
resource "datadog_csm_threats_policy" "policy2" {
137+
description = "created with terraform 2"
138+
enabled = true
139+
tags = ["env:staging"]
140+
}
141+
142+
resource "datadog_csm_threats_policies_list" "all" {
143+
entries {
144+
policy_id = datadog_csm_threats_policy.policy1.id
145+
name = "TERRAFORM_POLICY1"
146+
priority = 2
147+
}
148+
entries {
149+
policy_id = datadog_csm_threats_policy.policy2.id
150+
name = "TERRAFORM_POLICY2 UPDATED"
151+
priority = 5
152+
}
153+
}
154+
`
155+
}

datadog/tests/resource_datadog_csm_threats_policy_test.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ import (
1414

1515
// Create an agent policy and update its description
1616
func TestAccCSMThreatsPolicy_CreateAndUpdate(t *testing.T) {
17-
ctx, providers, accProviders := testAccFrameworkMuxProviders(context.Background(), t)
17+
_, providers, accProviders := testAccFrameworkMuxProviders(context.Background(), t)
1818

19-
policyName := uniqueAgentRuleName(ctx)
2019
resourceName := "datadog_csm_threats_policy.policy_test"
2120
tags := []string{"host_name:test_host"}
2221
resource.Test(t, resource.TestCase{
@@ -25,39 +24,35 @@ func TestAccCSMThreatsPolicy_CreateAndUpdate(t *testing.T) {
2524
CheckDestroy: testAccCheckCSMThreatsPolicyDestroy(providers.frameworkProvider),
2625
Steps: []resource.TestStep{
2726
{
28-
Config: fmt.Sprintf(`
27+
Config: `
2928
resource "datadog_csm_threats_policy" "policy_test" {
30-
name = "%s"
3129
enabled = true
3230
description = "im a policy"
3331
tags = ["host_name:test_host"]
3432
}
35-
`, policyName),
33+
`,
3634
Check: resource.ComposeTestCheckFunc(
3735
testAccCheckCSMThreatsPolicyExists(providers.frameworkProvider, "datadog_csm_threats_policy.policy_test"),
3836
checkCSMThreatsPolicyContent(
3937
resourceName,
40-
policyName,
4138
"im a policy",
4239
tags,
4340
),
4441
),
4542
},
4643
// Update description
4744
{
48-
Config: fmt.Sprintf(`
45+
Config: `
4946
resource "datadog_csm_threats_policy" "policy_test" {
50-
name = "%s"
5147
enabled = true
5248
description = "updated policy for terraform provider test"
5349
tags = ["host_name:test_host"]
5450
}
55-
`, policyName),
51+
`,
5652
Check: resource.ComposeTestCheckFunc(
5753
testAccCheckCSMThreatsPolicyExists(providers.frameworkProvider, resourceName),
5854
checkCSMThreatsPolicyContent(
5955
resourceName,
60-
policyName,
6156
"updated policy for terraform provider test",
6257
tags,
6358
),
@@ -67,9 +62,9 @@ func TestAccCSMThreatsPolicy_CreateAndUpdate(t *testing.T) {
6762
})
6863
}
6964

70-
func checkCSMThreatsPolicyContent(resourceName string, name string, description string, tags []string) resource.TestCheckFunc {
65+
func checkCSMThreatsPolicyContent(resourceName string, description string, tags []string) resource.TestCheckFunc {
7166
return resource.ComposeTestCheckFunc(
72-
resource.TestCheckResourceAttr(resourceName, "name", name),
67+
resource.TestCheckResourceAttrSet(resourceName, "name"),
7368
resource.TestCheckResourceAttr(resourceName, "description", description),
7469
resource.TestCheckResourceAttr(resourceName, "enabled", "true"),
7570
resource.TestCheckResourceAttr(resourceName, "tags.0", tags[0]),

docs/resources/csm_threats_policies_list.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Provides a Datadog CSM Threats policies API resource.
1717

1818
### Optional
1919

20-
- `entries` (Block Set) A set of policies that belong to this list/batch. All non-listed policies get deleted. (see [below for nested schema](#nestedblock--entries))
20+
- `entries` (Block Set) A set of policies that belong to this list. Only one policies_list resource can be defined in Terraform, containing all unique policies. All non-listed policies get deleted. (see [below for nested schema](#nestedblock--entries))
2121

2222
### Read-Only
2323

@@ -28,7 +28,7 @@ Provides a Datadog CSM Threats policies API resource.
2828

2929
Required:
3030

31-
- `policy_id` (String) The ID of the policy to manage (from `csm_threats_policy`).
31+
- `policy_id` (String) The ID of the policy to manage (from csm_threats_policy).
3232
- `priority` (Number) The priority of the policy in this list.
3333

3434
Optional:

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,3 @@ require (
9797
)
9898

9999
go 1.23
100-
replace github.com/DataDog/datadog-api-client-go/v2 v2.34.1-0.20241226155556-e60f30b0e84e => ../datadog-api-spec/generated/datadog-api-client-go

0 commit comments

Comments
 (0)