Skip to content

Commit 595f718

Browse files
generate docs + modify test file
1 parent 6fc7905 commit 595f718

4 files changed

+86
-122
lines changed

datadog/fwprovider/resource_datadog_csm_threats_multi_policies.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/hashicorp/terraform-plugin-framework/path"
1010
"github.com/hashicorp/terraform-plugin-framework/resource"
1111
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
12-
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
1312
"github.com/hashicorp/terraform-plugin-framework/types"
1413

1514
"github.com/terraform-providers/terraform-provider-datadog/datadog/internal/utils"
@@ -78,22 +77,23 @@ func (r *csmThreatsPoliciesListResource) Schema(_ context.Context, _ resource.Sc
7877
},
7978
"name": schema.StringAttribute{
8079
Description: "Name of the policy.",
81-
Optional: true,
80+
Required: true,
8281
},
8382
"description": schema.StringAttribute{
8483
Description: "A description for the policy.",
8584
Optional: true,
85+
Computed: true,
8686
},
8787
"enabled": schema.BoolAttribute{
8888
Description: "Indicates whether the policy is enabled.",
8989
Optional: true,
90-
Default: booldefault.StaticBool(false),
9190
Computed: true,
9291
},
9392
"tags": schema.SetAttribute{
9493
Description: "Host tags that define where the policy is deployed.",
9594
Optional: true,
9695
ElementType: types.StringType,
96+
Computed: true,
9797
},
9898
},
9999
},
@@ -242,7 +242,7 @@ func (r *csmThreatsPoliciesListResource) applyBatchPolicies(ctx context.Context,
242242

243243
// add deleted policies to the batch request
244244
for _, policy := range toDelete {
245-
policyID := policy.PolicyLabel.ValueString()
245+
policyID := policy.ID.ValueString()
246246
DeleteTrue := true
247247
item := datadogV2.CloudWorkloadSecurityAgentPolicyBatchUpdateAttributesPoliciesItems{
248248
Id: &policyID,
@@ -257,7 +257,7 @@ func (r *csmThreatsPoliciesListResource) applyBatchPolicies(ctx context.Context,
257257
name := policy.Name.ValueString()
258258
description := policy.Description.ValueString()
259259
enabled := policy.Enabled.ValueBool()
260-
var tags []string
260+
tags := []string{}
261261
if !policy.Tags.IsNull() && !policy.Tags.IsUnknown() {
262262
for _, tag := range policy.Tags.Elements() {
263263
tagStr, ok := tag.(types.String)
@@ -307,6 +307,7 @@ func (r *csmThreatsPoliciesListResource) applyBatchPolicies(ctx context.Context,
307307
respMapByName := make(map[string]datadogV2.CloudWorkloadSecurityAgentPolicyAttributes)
308308

309309
for _, policy := range batchResp.GetData() {
310+
310311
respID := policy.GetId()
311312
respAttr := policy.Attributes
312313
if respAttr == nil {

datadog/tests/resource_datadog_csm_threats_policies_list_test.go

+38-81
Original file line numberDiff line numberDiff line change
@@ -12,51 +12,49 @@ import (
1212
)
1313

1414
// Create a policies_list and update the name and priority of its policy
15-
func TestAccCSMThreatsPoliciesList_CreateAndUpdate(t *testing.T) {
15+
func TestAccCSMThreatsPolicies_CreateAndUpdate(t *testing.T) {
1616
_, providers, accProviders := testAccFrameworkMuxProviders(context.Background(), t)
1717

18-
resourceName := "datadog_csm_threats_policies_list.all"
18+
resourceName := "datadog_csm_threats_policies.all_policies"
1919

2020
resource.Test(t, resource.TestCase{
2121
PreCheck: func() { testAccPreCheck(t) },
2222
ProtoV5ProviderFactories: accProviders,
23-
CheckDestroy: testAccCheckCSMThreatsPoliciesListDestroy(providers.frameworkProvider),
23+
CheckDestroy: testAccCheckCSMThreatsPoliciesDestroy(providers.frameworkProvider),
2424
Steps: []resource.TestStep{
2525
{
26-
Config: testAccCSMThreatsPoliciesListConfigBasic(),
26+
Config: testAccCSMThreatsPoliciesConfig(),
2727
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"),
28+
testAccCheckCSMThreatsPoliciesExists(providers.frameworkProvider, resourceName),
29+
resource.TestCheckResourceAttr(resourceName, "policies.0.name", "terraform_policy"),
30+
resource.TestCheckResourceAttr(resourceName, "policies.0.description", "description"),
31+
resource.TestCheckResourceAttr(resourceName, "policies.0.enabled", "false"),
32+
resource.TestCheckResourceAttr(resourceName, "policies.0.tags.0", "env:staging"),
3433
),
3534
},
3635
{
37-
Config: testAccCSMThreatsPoliciesListConfigUpdate(),
36+
Config: testAccCSMThreatsPoliciesConfigUpdate(),
3837
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"),
38+
testAccCheckCSMThreatsPoliciesExists(providers.frameworkProvider, resourceName),
39+
resource.TestCheckResourceAttr(resourceName, "policies.0.name", "terraform_policy updated"),
40+
resource.TestCheckResourceAttr(resourceName, "policies.0.description", "new description"),
41+
resource.TestCheckResourceAttr(resourceName, "policies.0.enabled", "true"),
42+
resource.TestCheckResourceAttr(resourceName, "policies.0.tags.0", "foo:bar"),
4543
),
4644
},
4745
},
4846
})
4947
}
5048

51-
func testAccCheckCSMThreatsPoliciesListExists(accProvider *fwprovider.FrameworkProvider, resourceName string) resource.TestCheckFunc {
49+
func testAccCheckCSMThreatsPoliciesExists(accProvider *fwprovider.FrameworkProvider, resourceName string) resource.TestCheckFunc {
5250
return func(s *terraform.State) error {
5351
rs, ok := s.RootModule().Resources[resourceName]
5452
if !ok {
5553
return fmt.Errorf("resource '%s' not found in state", resourceName)
5654
}
57-
if rs.Type != "datadog_csm_threats_policies_list" {
55+
if rs.Type != "datadog_csm_threats_policies" {
5856
return fmt.Errorf(
59-
"resource %s is not a datadog_csm_threats_policies_list, got: %s",
57+
"resource %s is not a datadog_csm_threats_policies, got: %s",
6058
resourceName,
6159
rs.Type,
6260
)
@@ -70,85 +68,44 @@ func testAccCheckCSMThreatsPoliciesListExists(accProvider *fwprovider.FrameworkP
7068
}
7169
}
7270

73-
func testAccCheckCSMThreatsPoliciesListDestroy(accProvider *fwprovider.FrameworkProvider) resource.TestCheckFunc {
71+
func testAccCheckCSMThreatsPoliciesDestroy(accProvider *fwprovider.FrameworkProvider) resource.TestCheckFunc {
7472
return func(s *terraform.State) error {
75-
apiInstances := accProvider.DatadogApiInstances
76-
auth := accProvider.Auth
77-
7873
for _, r := range s.RootModule().Resources {
79-
if r.Type != "datadog_csm_threats_policies_list" {
74+
if r.Type != "datadog_csm_threats_policies" {
8075
continue
8176
}
8277

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")
78+
if _, ok := s.RootModule().Resources[r.Primary.ID]; ok {
79+
return fmt.Errorf("Resource %s still exists in state", r.Primary.ID)
9380
}
9481
}
9582
return nil
9683
}
9784
}
9885

99-
func testAccCSMThreatsPoliciesListConfigBasic() string {
86+
func testAccCSMThreatsPoliciesConfig() string {
10087
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
88+
resource "datadog_csm_threats_policies" "all_policies" {
89+
policies {
90+
policy_label = "policy"
91+
name = "terraform_policy"
92+
description = "description"
93+
enabled = false
94+
tags = ["env:staging"]
12395
}
12496
}
12597
`
12698
}
12799

128-
func testAccCSMThreatsPoliciesListConfigUpdate() string {
100+
func testAccCSMThreatsPoliciesConfigUpdate() string {
129101
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
102+
resource "datadog_csm_threats_policies" "all_policies" {
103+
policies {
104+
policy_label = "policy"
105+
name = "terraform_policy updated"
106+
description = "new description"
107+
enabled = true
108+
tags = ["foo:bar"]
152109
}
153110
}
154111
`
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "datadog_csm_threats_policies Resource - terraform-provider-datadog"
4+
subcategory: ""
5+
description: |-
6+
Manages multiple Datadog CSM Threats policies in a single resource.
7+
---
8+
9+
# datadog_csm_threats_policies (Resource)
10+
11+
Manages multiple Datadog CSM Threats policies in a single resource.
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Optional
19+
20+
- `policies` (Block Set) Set of policy blocks. Each block requires a unique policy_label. (see [below for nested schema](#nestedblock--policies))
21+
22+
### Read-Only
23+
24+
- `id` (String) The ID of this resource.
25+
26+
<a id="nestedblock--policies"></a>
27+
### Nested Schema for `policies`
28+
29+
Required:
30+
31+
- `name` (String) Name of the policy.
32+
- `policy_label` (String) The ID of the policy to manage (from csm_threats_policy).
33+
34+
Optional:
35+
36+
- `description` (String) A description for the policy.
37+
- `enabled` (Boolean) Indicates whether the policy is enabled.
38+
- `tags` (Set of String) Host tags that define where the policy is deployed.
39+
40+
Read-Only:
41+
42+
- `id` (String) The Datadog-assigned policy ID.

docs/resources/csm_threats_policies_list.md

-36
This file was deleted.

0 commit comments

Comments
 (0)