Skip to content

Commit ed17880

Browse files
fix cws agent_rule resource and data_source + fix test files
1 parent 4a05429 commit ed17880

7 files changed

+21
-19
lines changed

datadog/fwprovider/data_source_datadog_csm_threats_agent_rule.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (r *csmThreatsAgentRulesDataSource) Read(ctx context.Context, request datas
5151
return
5252
}
5353

54-
res, _, err := r.api.ListCSMThreatsAgentRules(r.auth)
54+
res, _, err := r.api.ListCloudWorkloadSecurityAgentRules(r.auth)
5555
if err != nil {
5656
response.Diagnostics.Append(utils.FrameworkErrorDiag(err, "error while fetching agent rules"))
5757
return

datadog/fwprovider/data_source_datadog_csm_threats_policy.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ func (r *csmThreatsPoliciesDataSource) Read(ctx context.Context, request datasou
6767
policyModel.Description = types.StringValue(attributes.GetDescription())
6868
policyModel.Enabled = types.BoolValue(attributes.GetEnabled())
6969
policyModel.Tags, _ = types.SetValueFrom(ctx, types.StringType, attributes.GetHostTags())
70+
policyModel.HostTagsLists, _ = types.SetValueFrom(ctx, types.ListType{
71+
ElemType: types.StringType,
72+
}, attributes.GetHostTagsLists())
7073
policyIds[idx] = policy.GetId()
7174
policies[idx] = policyModel
7275
}
@@ -96,8 +99,13 @@ func (*csmThreatsPoliciesDataSource) Schema(_ context.Context, _ datasource.Sche
9699
Description: "List of policies",
97100
ElementType: types.ObjectType{
98101
AttrTypes: map[string]attr.Type{
99-
"id": types.StringType,
100-
"tags": types.SetType{ElemType: types.StringType},
102+
"id": types.StringType,
103+
"tags": types.SetType{ElemType: types.StringType},
104+
"host_tags_lists": types.SetType{
105+
ElemType: types.ListType{
106+
ElemType: types.StringType,
107+
},
108+
},
101109
"name": types.StringType,
102110
"description": types.StringType,
103111
"enabled": types.BoolType,

datadog/fwprovider/resource_datadog_csm_threats_agent_rule.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ func (r *csmThreatsAgentRuleResource) Schema(_ context.Context, _ resource.Schem
7272
"expression": schema.StringAttribute{
7373
Required: true,
7474
Description: "The SECL expression of the Agent rule",
75-
PlanModifiers: []planmodifier.String{
76-
stringplanmodifier.RequiresReplace(),
77-
},
7875
},
7976
},
8077
}
@@ -99,7 +96,7 @@ func (r *csmThreatsAgentRuleResource) Create(ctx context.Context, request resour
9996
response.Diagnostics.AddError("error while parsing resource", err.Error())
10097
}
10198

102-
res, _, err := r.api.CreateCSMThreatsAgentRule(r.auth, *agentRulePayload)
99+
res, _, err := r.api.CreateCloudWorkloadSecurityAgentRule(r.auth, *agentRulePayload)
103100
if err != nil {
104101
response.Diagnostics.Append(utils.FrameworkErrorDiag(err, "error creating agent rule"))
105102
return
@@ -121,7 +118,7 @@ func (r *csmThreatsAgentRuleResource) Read(ctx context.Context, request resource
121118
}
122119

123120
agentRuleId := state.Id.ValueString()
124-
res, httpResponse, err := r.api.GetCSMThreatsAgentRule(r.auth, agentRuleId)
121+
res, httpResponse, err := r.api.GetCloudWorkloadSecurityAgentRule(r.auth, agentRuleId)
125122
if err != nil {
126123
if httpResponse != nil && httpResponse.StatusCode == 404 {
127124
response.State.RemoveResource(ctx)
@@ -154,7 +151,7 @@ func (r *csmThreatsAgentRuleResource) Update(ctx context.Context, request resour
154151
response.Diagnostics.AddError("error while parsing resource", err.Error())
155152
}
156153

157-
res, _, err := r.api.UpdateCSMThreatsAgentRule(r.auth, state.Id.ValueString(), *agentRulePayload)
154+
res, _, err := r.api.UpdateCloudWorkloadSecurityAgentRule(r.auth, state.Id.ValueString(), *agentRulePayload)
158155
if err != nil {
159156
response.Diagnostics.Append(utils.FrameworkErrorDiag(err, "error updating agent rule"))
160157
return
@@ -180,7 +177,7 @@ func (r *csmThreatsAgentRuleResource) Delete(ctx context.Context, request resour
180177

181178
id := state.Id.ValueString()
182179

183-
httpResp, err := r.api.DeleteCSMThreatsAgentRule(r.auth, id)
180+
httpResp, err := r.api.DeleteCloudWorkloadSecurityAgentRule(r.auth, id)
184181
if err != nil {
185182
if httpResp != nil && httpResp.StatusCode == 404 {
186183
return
@@ -204,11 +201,12 @@ func (r *csmThreatsAgentRuleResource) buildCreateCSMThreatsAgentRulePayload(stat
204201
}
205202

206203
func (r *csmThreatsAgentRuleResource) buildUpdateCSMThreatsAgentRulePayload(state *csmThreatsAgentRuleModel) (*datadogV2.CloudWorkloadSecurityAgentRuleUpdateRequest, error) {
207-
agentRuleId, _, description, enabled, _ := r.extractAgentRuleAttributesFromResource(state)
204+
agentRuleId, _, description, enabled, expression := r.extractAgentRuleAttributesFromResource(state)
208205

209206
attributes := datadogV2.CloudWorkloadSecurityAgentRuleUpdateAttributes{}
210207
attributes.Description = description
211208
attributes.Enabled = &enabled
209+
attributes.Expression = &expression
212210

213211
data := datadogV2.NewCloudWorkloadSecurityAgentRuleUpdateData(attributes, datadogV2.CLOUDWORKLOADSECURITYAGENTRULETYPE_AGENT_RULE)
214212
data.Id = &agentRuleId

datadog/fwprovider/resource_datadog_csm_threats_policy.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,6 @@ func (r *csmThreatsPolicyResource) updateStateFromResponse(ctx context.Context,
270270
state.Enabled = types.BoolValue(attributes.GetEnabled())
271271
state.Tags, _ = types.SetValueFrom(ctx, types.StringType, attributes.GetHostTags())
272272
state.HostTagsLists, _ = types.SetValueFrom(ctx, types.ListType{
273-
ElemType: types.ListType{
274-
ElemType: types.StringType,
275-
},
273+
ElemType: types.StringType,
276274
}, attributes.GetHostTagsLists())
277275
}

datadog/tests/data_source_datadog_csm_threats_agent_rule_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func checkCSMThreatsAgentRulesDataSourceContent(accProvider *fwprovider.Framewor
5757
auth := accProvider.Auth
5858
apiInstances := accProvider.DatadogApiInstances
5959

60-
allAgentRulesResponse, _, err := apiInstances.GetCSMThreatsApiV2().ListCSMThreatsAgentRules(auth)
60+
allAgentRulesResponse, _, err := apiInstances.GetCSMThreatsApiV2().ListCloudWorkloadSecurityAgentRules(auth)
6161
if err != nil {
6262
return err
6363
}

datadog/tests/data_source_datadog_csm_threats_policies_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ func TestAccCSMThreatsPoliciesDataSource(t *testing.T) {
2222
name = "%s"
2323
enabled = true
2424
description = "im a policy"
25-
tags = ["host_name:test_host"]
2625
host_tags_lists = [["host_name:test_host", "env:prod"], ["host_name:test_host2", "env:staging"]]
2726
}
2827
`, policyName)
@@ -101,7 +100,6 @@ func checkCSMThreatsPoliciesDataSourceContent(accProvider *fwprovider.FrameworkP
101100
return resource.ComposeTestCheckFunc(
102101
resource.TestCheckResourceAttr(dataSourceName, fmt.Sprintf("policies.%d.name", idx), policyName),
103102
resource.TestCheckResourceAttr(dataSourceName, fmt.Sprintf("policies.%d.enabled", idx), "true"),
104-
resource.TestCheckResourceAttr(dataSourceName, fmt.Sprintf("policies.%d.tags.0", idx), "host_name:test_host"),
105103
resource.TestCheckResourceAttr(dataSourceName, fmt.Sprintf("policies.%d.host_tags_lists.0.0", idx), "host_name:test_host"),
106104
resource.TestCheckResourceAttr(dataSourceName, fmt.Sprintf("policies.%d.host_tags_lists.0.1", idx), "env:prod"),
107105
resource.TestCheckResourceAttr(dataSourceName, fmt.Sprintf("policies.%d.host_tags_lists.1.0", idx), "host_name:test_host2"),

datadog/tests/resource_datadog_csm_threats_agent_rule_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func testAccCheckCSMThreatsAgentRuleExists(accProvider *fwprovider.FrameworkProv
8989
auth := accProvider.Auth
9090
apiInstances := accProvider.DatadogApiInstances
9191

92-
_, _, err := apiInstances.GetCSMThreatsApiV2().GetCSMThreatsAgentRule(auth, resource.Primary.ID)
92+
_, _, err := apiInstances.GetCSMThreatsApiV2().GetCloudWorkloadSecurityAgentRule(auth, resource.Primary.ID)
9393
if err != nil {
9494
return fmt.Errorf("received an error retrieving agent rule: %s", err)
9595
}
@@ -105,7 +105,7 @@ func testAccCheckCSMThreatsAgentRuleDestroy(accProvider *fwprovider.FrameworkPro
105105

106106
for _, resource := range s.RootModule().Resources {
107107
if resource.Type == "datadog_csm_threats_agent_rule" {
108-
_, httpResponse, err := apiInstances.GetCSMThreatsApiV2().GetCSMThreatsAgentRule(auth, resource.Primary.ID)
108+
_, httpResponse, err := apiInstances.GetCSMThreatsApiV2().GetCloudWorkloadSecurityAgentRule(auth, resource.Primary.ID)
109109
if err == nil {
110110
return errors.New("agent rule still exists")
111111
}

0 commit comments

Comments
 (0)