Skip to content

Commit 6477be3

Browse files
add product_tags
1 parent 01017fd commit 6477be3

7 files changed

+57
-20
lines changed

datadog/fwprovider/resource_datadog_csm_threats_multi_policy_agent_rule.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type csmThreatsMultiPolicyAgentRuleModel struct {
2727
Description types.String `tfsdk:"description"`
2828
Enabled types.Bool `tfsdk:"enabled"`
2929
Expression types.String `tfsdk:"expression"`
30+
ProductTags types.Set `tfsdk:"product_tags"`
3031
}
3132

3233
func NewCSMThreatsMultiPolicyAgentRuleResource() resource.Resource {
@@ -75,6 +76,11 @@ func (r *csmThreatsMultiPolicyAgentRuleResource) Schema(_ context.Context, _ res
7576
stringplanmodifier.RequiresReplace(),
7677
},
7778
},
79+
"product_tags": schema.SetAttribute{
80+
Optional: true,
81+
ElementType: types.StringType,
82+
Description: "The list of product tags associated with the rule",
83+
},
7884
},
7985
}
8086
}
@@ -198,42 +204,54 @@ func (r *csmThreatsMultiPolicyAgentRuleResource) Delete(ctx context.Context, req
198204
}
199205

200206
func (r *csmThreatsMultiPolicyAgentRuleResource) buildCreateCSMThreatsAgentRulePayload(state *csmThreatsMultiPolicyAgentRuleModel) (*datadogV2.CloudWorkloadSecurityAgentRuleCreateRequest, error) {
201-
_, policyId, name, description, enabled, expression := r.extractAgentRuleAttributesFromResource(state)
207+
_, policyId, name, description, enabled, expression, productTags := r.extractAgentRuleAttributesFromResource(state)
202208

203209
attributes := datadogV2.CloudWorkloadSecurityAgentRuleCreateAttributes{}
204210
attributes.Expression = expression
205211
attributes.Name = name
206212
attributes.Description = description
207213
attributes.Enabled = &enabled
208214
attributes.PolicyId = &policyId
215+
attributes.ProductTags = productTags
209216

210217
data := datadogV2.NewCloudWorkloadSecurityAgentRuleCreateData(attributes, datadogV2.CLOUDWORKLOADSECURITYAGENTRULETYPE_AGENT_RULE)
211218
return datadogV2.NewCloudWorkloadSecurityAgentRuleCreateRequest(*data), nil
212219
}
213220

214221
func (r *csmThreatsMultiPolicyAgentRuleResource) buildUpdateCSMThreatsAgentRulePayload(state *csmThreatsMultiPolicyAgentRuleModel) (*datadogV2.CloudWorkloadSecurityAgentRuleUpdateRequest, error) {
215-
agentRuleId, policyId, _, description, enabled, _ := r.extractAgentRuleAttributesFromResource(state)
222+
agentRuleId, policyId, _, description, enabled, _, productTags := r.extractAgentRuleAttributesFromResource(state)
216223

217224
attributes := datadogV2.CloudWorkloadSecurityAgentRuleUpdateAttributes{}
218225
attributes.Description = description
219226
attributes.Enabled = &enabled
220227
attributes.PolicyId = &policyId
228+
attributes.ProductTags = productTags
221229

222230
data := datadogV2.NewCloudWorkloadSecurityAgentRuleUpdateData(attributes, datadogV2.CLOUDWORKLOADSECURITYAGENTRULETYPE_AGENT_RULE)
223231
data.Id = &agentRuleId
224232
return datadogV2.NewCloudWorkloadSecurityAgentRuleUpdateRequest(*data), nil
225233
}
226234

227-
func (r *csmThreatsMultiPolicyAgentRuleResource) extractAgentRuleAttributesFromResource(state *csmThreatsMultiPolicyAgentRuleModel) (string, string, string, *string, bool, string) {
235+
func (r *csmThreatsMultiPolicyAgentRuleResource) extractAgentRuleAttributesFromResource(state *csmThreatsMultiPolicyAgentRuleModel) (string, string, string, *string, bool, string, []string) {
228236
// Mandatory fields
229237
id := state.Id.ValueString()
230238
policyId := state.PolicyId.ValueString()
231239
name := state.Name.ValueString()
232240
enabled := state.Enabled.ValueBool()
233241
expression := state.Expression.ValueString()
234242
description := state.Description.ValueStringPointer()
243+
var productTags []string
244+
if !state.ProductTags.IsNull() && !state.ProductTags.IsUnknown() {
245+
for _, tag := range state.ProductTags.Elements() {
246+
tagStr, ok := tag.(types.String)
247+
if !ok {
248+
return "", "", "", nil, false, "", nil
249+
}
250+
productTags = append(productTags, tagStr.ValueString())
251+
}
252+
}
235253

236-
return id, policyId, name, description, enabled, expression
254+
return id, policyId, name, description, enabled, expression, productTags
237255
}
238256

239257
func (r *csmThreatsMultiPolicyAgentRuleResource) updateStateFromResponse(ctx context.Context, state *csmThreatsMultiPolicyAgentRuleModel, res *datadogV2.CloudWorkloadSecurityAgentRuleResponse) {
@@ -245,4 +263,5 @@ func (r *csmThreatsMultiPolicyAgentRuleResource) updateStateFromResponse(ctx con
245263
state.Description = types.StringValue(attributes.GetDescription())
246264
state.Enabled = types.BoolValue(attributes.GetEnabled())
247265
state.Expression = types.StringValue(attributes.GetExpression())
266+
state.ProductTags, _ = types.SetValueFrom(ctx, types.StringType, attributes.GetProductTags())
248267
}

datadog/fwprovider/resource_datadog_csm_threats_policy.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package fwprovider
33
import (
44
"context"
55
"fmt"
6-
mathrand "math/rand"
76

87
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
98
"github.com/hashicorp/terraform-plugin-framework/path"
@@ -48,7 +47,7 @@ func (r *csmThreatsPolicyResource) Schema(_ context.Context, _ resource.SchemaRe
4847
Attributes: map[string]schema.Attribute{
4948
"id": utils.ResourceIDAttribute(),
5049
"name": schema.StringAttribute{
51-
Computed: true,
50+
Required: true,
5251
Description: "The name of the policy.",
5352
},
5453
"description": schema.StringAttribute{
@@ -183,13 +182,13 @@ func (r *csmThreatsPolicyResource) Delete(ctx context.Context, request resource.
183182
}
184183

185184
func (r *csmThreatsPolicyResource) buildCreateCSMThreatsPolicyPayload(state *csmThreatsPolicyModel) (*datadogV2.CloudWorkloadSecurityAgentPolicyCreateRequest, error) {
186-
_, description, enabled, tags, err := r.extractPolicyAttributesFromResource(state)
185+
_, name, description, enabled, tags, err := r.extractPolicyAttributesFromResource(state)
187186
if err != nil {
188187
return nil, err
189188
}
190189

191190
attributes := datadogV2.CloudWorkloadSecurityAgentPolicyCreateAttributes{}
192-
attributes.Name = fmt.Sprintf("policy-%d", mathrand.Intn(1000))
191+
attributes.Name = name
193192
attributes.Description = description
194193
attributes.Enabled = enabled
195194
attributes.HostTags = tags
@@ -199,11 +198,12 @@ func (r *csmThreatsPolicyResource) buildCreateCSMThreatsPolicyPayload(state *csm
199198
}
200199

201200
func (r *csmThreatsPolicyResource) buildUpdateCSMThreatsPolicyPayload(state *csmThreatsPolicyModel) (*datadogV2.CloudWorkloadSecurityAgentPolicyUpdateRequest, error) {
202-
policyId, description, enabled, tags, err := r.extractPolicyAttributesFromResource(state)
201+
policyId, name, description, enabled, tags, err := r.extractPolicyAttributesFromResource(state)
203202
if err != nil {
204203
return nil, err
205204
}
206205
attributes := datadogV2.CloudWorkloadSecurityAgentPolicyUpdateAttributes{}
206+
attributes.Name = &name
207207
attributes.Description = description
208208
attributes.Enabled = enabled
209209
attributes.HostTags = tags
@@ -213,23 +213,24 @@ func (r *csmThreatsPolicyResource) buildUpdateCSMThreatsPolicyPayload(state *csm
213213
return datadogV2.NewCloudWorkloadSecurityAgentPolicyUpdateRequest(*data), nil
214214
}
215215

216-
func (r *csmThreatsPolicyResource) extractPolicyAttributesFromResource(state *csmThreatsPolicyModel) (string, *string, *bool, []string, error) {
216+
func (r *csmThreatsPolicyResource) extractPolicyAttributesFromResource(state *csmThreatsPolicyModel) (string, string, *string, *bool, []string, error) {
217217
// Mandatory fields
218218
id := state.Id.ValueString()
219+
name := state.Name.ValueString()
219220
enabled := state.Enabled.ValueBoolPointer()
220221
description := state.Description.ValueStringPointer()
221222
var tags []string
222223
if !state.Tags.IsNull() && !state.Tags.IsUnknown() {
223224
for _, tag := range state.Tags.Elements() {
224225
tagStr, ok := tag.(types.String)
225226
if !ok {
226-
return "", nil, nil, nil, fmt.Errorf("expected item to be of type types.String, got %T", tag)
227+
return "", "", nil, nil, nil, fmt.Errorf("expected item to be of type types.String, got %T", tag)
227228
}
228229
tags = append(tags, tagStr.ValueString())
229230
}
230231
}
231232

232-
return id, description, enabled, tags, nil
233+
return id, name, description, enabled, tags, nil
233234
}
234235

235236
func (r *csmThreatsPolicyResource) updateStateFromResponse(ctx context.Context, state *csmThreatsPolicyModel, res *datadogV2.CloudWorkloadSecurityAgentPolicyResponse) {

datadog/tests/resource_datadog_csm_threats_agent_rule_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func TestAccCSMThreatsAgentRule_CreateAndUpdate(t *testing.T) {
3030
enabled = true
3131
description = "im a rule"
3232
expression = "open.file.name == \"etc/shadow/password\""
33+
product_tags = ["compliance_framework:PCI-DSS"]
3334
}
3435
`, agentRuleName),
3536
Check: resource.ComposeTestCheckFunc(
@@ -39,6 +40,7 @@ func TestAccCSMThreatsAgentRule_CreateAndUpdate(t *testing.T) {
3940
agentRuleName,
4041
"im a rule",
4142
"open.file.name == \"etc/shadow/password\"",
43+
"compliance_framework:PCI-DSS\"]",
4244
),
4345
),
4446
},
@@ -50,6 +52,7 @@ func TestAccCSMThreatsAgentRule_CreateAndUpdate(t *testing.T) {
5052
enabled = true
5153
description = "updated agent rule for terraform provider test"
5254
expression = "open.file.name == \"etc/shadow/password\""
55+
product_tags = ["compliance_framework:ISO-27799"]
5356
}
5457
`, agentRuleName),
5558
Check: resource.ComposeTestCheckFunc(
@@ -59,19 +62,22 @@ func TestAccCSMThreatsAgentRule_CreateAndUpdate(t *testing.T) {
5962
agentRuleName,
6063
"updated agent rule for terraform provider test",
6164
"open.file.name == \"etc/shadow/password\"",
65+
"compliance_framework:ISO-27799",
6266
),
6367
),
6468
},
6569
},
6670
})
6771
}
6872

69-
func checkCSMThreatsAgentRuleContent(resourceName string, name string, description string, expression string) resource.TestCheckFunc {
73+
func checkCSMThreatsAgentRuleContent(resourceName string, name string, description string, expression string, product_tags string) resource.TestCheckFunc {
7074
return resource.ComposeTestCheckFunc(
7175
resource.TestCheckResourceAttr(resourceName, "name", name),
7276
resource.TestCheckResourceAttr(resourceName, "description", description),
7377
resource.TestCheckResourceAttr(resourceName, "enabled", "true"),
7478
resource.TestCheckResourceAttr(resourceName, "expression", expression),
79+
resource.TestCheckResourceAttr(resourceName, "product_tags.#", "1"),
80+
resource.TestCheckTypeSetElemAttr(resourceName, "product_tags.*", product_tags),
7581
)
7682
}
7783

datadog/tests/resource_datadog_csm_threats_multi_policy_agent_rule_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func TestAccCSMThreatsMultiPolicyAgentRule_CreateAndUpdate(t *testing.T) {
4949
enabled = true
5050
description = "im a rule"
5151
expression = "open.file.name == \"etc/shadow/password\""
52+
product_tags = ["compliance_framework:PCI-DSS"]
5253
}
5354
`, policyConfig, agentRuleName),
5455
Check: resource.ComposeTestCheckFunc(
@@ -58,6 +59,7 @@ func TestAccCSMThreatsMultiPolicyAgentRule_CreateAndUpdate(t *testing.T) {
5859
agentRuleName,
5960
"im a rule",
6061
"open.file.name == \"etc/shadow/password\"",
62+
"compliance_framework:PCI-DSS",
6163
),
6264
),
6365
},
@@ -71,6 +73,7 @@ func TestAccCSMThreatsMultiPolicyAgentRule_CreateAndUpdate(t *testing.T) {
7173
enabled = true
7274
description = "updated agent rule for terraform provider test"
7375
expression = "open.file.name == \"etc/shadow/password\""
76+
product_tags = ["compliance_framework:ISO-27799"]
7477
}
7578
`, policyConfig, agentRuleName),
7679
Check: resource.ComposeTestCheckFunc(
@@ -80,6 +83,7 @@ func TestAccCSMThreatsMultiPolicyAgentRule_CreateAndUpdate(t *testing.T) {
8083
agentRuleName,
8184
"updated agent rule for terraform provider test",
8285
"open.file.name == \"etc/shadow/password\"",
86+
"compliance_framework:ISO-27799",
8387
),
8488
),
8589
},

datadog/tests/resource_datadog_csm_threats_policy_test.go

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

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

19+
policyName := uniqueAgentRuleName(ctx)
1920
resourceName := "datadog_csm_threats_policy.policy_test"
2021
tags := []string{"host_name:test_host"}
2122
resource.Test(t, resource.TestCase{
@@ -24,35 +25,39 @@ func TestAccCSMThreatsPolicy_CreateAndUpdate(t *testing.T) {
2425
CheckDestroy: testAccCheckCSMThreatsPolicyDestroy(providers.frameworkProvider),
2526
Steps: []resource.TestStep{
2627
{
27-
Config: `
28+
Config: fmt.Sprintf(`
2829
resource "datadog_csm_threats_policy" "policy_test" {
30+
name = "%s"
2931
enabled = true
3032
description = "im a policy"
3133
tags = ["host_name:test_host"]
3234
}
33-
`,
35+
`, policyName),
3436
Check: resource.ComposeTestCheckFunc(
3537
testAccCheckCSMThreatsPolicyExists(providers.frameworkProvider, "datadog_csm_threats_policy.policy_test"),
3638
checkCSMThreatsPolicyContent(
3739
resourceName,
40+
policyName,
3841
"im a policy",
3942
tags,
4043
),
4144
),
4245
},
4346
// Update description
4447
{
45-
Config: `
48+
Config: fmt.Sprintf(`
4649
resource "datadog_csm_threats_policy" "policy_test" {
50+
name = "%s"
4751
enabled = true
4852
description = "updated policy for terraform provider test"
4953
tags = ["host_name:test_host"]
5054
}
51-
`,
55+
`, policyName),
5256
Check: resource.ComposeTestCheckFunc(
5357
testAccCheckCSMThreatsPolicyExists(providers.frameworkProvider, resourceName),
5458
checkCSMThreatsPolicyContent(
5559
resourceName,
60+
policyName,
5661
"updated policy for terraform provider test",
5762
tags,
5863
),
@@ -62,9 +67,9 @@ func TestAccCSMThreatsPolicy_CreateAndUpdate(t *testing.T) {
6267
})
6368
}
6469

65-
func checkCSMThreatsPolicyContent(resourceName string, description string, tags []string) resource.TestCheckFunc {
70+
func checkCSMThreatsPolicyContent(resourceName string, name string, description string, tags []string) resource.TestCheckFunc {
6671
return resource.ComposeTestCheckFunc(
67-
resource.TestCheckResourceAttrSet(resourceName, "name"),
72+
resource.TestCheckResourceAttr(resourceName, "name", name),
6873
resource.TestCheckResourceAttr(resourceName, "description", description),
6974
resource.TestCheckResourceAttr(resourceName, "enabled", "true"),
7075
resource.TestCheckResourceAttr(resourceName, "tags.0", tags[0]),

docs/resources/csm_threats_multi_policy_agent_rule.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Provides a Datadog CSM Threats Agent Rule API resource.
2525
### Optional
2626

2727
- `description` (String) A description for the Agent rule.
28+
- `product_tags` (Set of String) The list of product tags associated with the rule
2829

2930
### Read-Only
3031

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,4 @@ require (
102102
)
103103

104104
go 1.23.0
105+
replace github.com/DataDog/datadog-api-client-go/v2 v2.35.0 => ../datadog-api-spec/generated/datadog-api-client-go

0 commit comments

Comments
 (0)