Skip to content

Commit f715a3a

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

4 files changed

+81
-121
lines changed

datadog/fwprovider/resource_datadog_csm_threats_multi_policies.go

Lines changed: 6 additions & 5 deletions
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

Lines changed: 33 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -15,48 +15,42 @@ import (
1515
func TestAccCSMThreatsPoliciesList_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.enabled", "false"),
3431
),
3532
},
3633
{
37-
Config: testAccCSMThreatsPoliciesListConfigUpdate(),
34+
Config: testAccCSMThreatsPoliciesConfigUpdate(),
3835
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"),
36+
testAccCheckCSMThreatsPoliciesExists(providers.frameworkProvider, resourceName),
37+
resource.TestCheckResourceAttr(resourceName, "policies.0.name", "terraform_policy updated"),
38+
resource.TestCheckResourceAttr(resourceName, "policies.0.enabled", "true"),
4539
),
4640
},
4741
},
4842
})
4943
}
5044

51-
func testAccCheckCSMThreatsPoliciesListExists(accProvider *fwprovider.FrameworkProvider, resourceName string) resource.TestCheckFunc {
45+
func testAccCheckCSMThreatsPoliciesExists(accProvider *fwprovider.FrameworkProvider, resourceName string) resource.TestCheckFunc {
5246
return func(s *terraform.State) error {
5347
rs, ok := s.RootModule().Resources[resourceName]
5448
if !ok {
5549
return fmt.Errorf("resource '%s' not found in state", resourceName)
5650
}
57-
if rs.Type != "datadog_csm_threats_policies_list" {
51+
if rs.Type != "datadog_csm_threats_policies" {
5852
return fmt.Errorf(
59-
"resource %s is not a datadog_csm_threats_policies_list, got: %s",
53+
"resource %s is not a datadog_csm_threats_policies, got: %s",
6054
resourceName,
6155
rs.Type,
6256
)
@@ -70,85 +64,44 @@ func testAccCheckCSMThreatsPoliciesListExists(accProvider *fwprovider.FrameworkP
7064
}
7165
}
7266

73-
func testAccCheckCSMThreatsPoliciesListDestroy(accProvider *fwprovider.FrameworkProvider) resource.TestCheckFunc {
67+
func testAccCheckCSMThreatsPoliciesDestroy(accProvider *fwprovider.FrameworkProvider) resource.TestCheckFunc {
7468
return func(s *terraform.State) error {
75-
apiInstances := accProvider.DatadogApiInstances
76-
auth := accProvider.Auth
77-
7869
for _, r := range s.RootModule().Resources {
79-
if r.Type != "datadog_csm_threats_policies_list" {
70+
if r.Type != "datadog_csm_threats_policies" {
8071
continue
8172
}
8273

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")
74+
if _, ok := s.RootModule().Resources[r.Primary.ID]; ok {
75+
return fmt.Errorf("Resource %s still exists in state", r.Primary.ID)
9376
}
9477
}
9578
return nil
9679
}
9780
}
9881

99-
func testAccCSMThreatsPoliciesListConfigBasic() string {
82+
func testAccCSMThreatsPoliciesConfig() string {
10083
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
84+
resource "datadog_csm_threats_policies" "all_policies" {
85+
policies {
86+
policy_label = "policy1"
87+
name = "terraform_policy"
88+
description = "description"
89+
enabled = false
90+
tags = ["env:staging"]
12391
}
12492
}
12593
`
12694
}
12795

128-
func testAccCSMThreatsPoliciesListConfigUpdate() string {
96+
func testAccCSMThreatsPoliciesConfigUpdate() string {
12997
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
98+
resource "datadog_csm_threats_policies" "all_policies" {
99+
policies {
100+
policy_label = "policy"
101+
name = "terraform_policy updated"
102+
description = "new description"
103+
enabled = true
104+
tags = ["foo:bar"]
152105
}
153106
}
154107
`
Lines changed: 42 additions & 0 deletions
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

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)