@@ -13,6 +13,8 @@ import (
13
13
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
14
14
"github.com/hashicorp/terraform-plugin-framework/types"
15
15
16
+ "net/http"
17
+
16
18
"github.com/terraform-providers/terraform-provider-datadog/datadog/internal/utils"
17
19
)
18
20
@@ -57,7 +59,7 @@ func (r *csmThreatsAgentRuleResource) Schema(_ context.Context, _ resource.Schem
57
59
Attributes : map [string ]schema.Attribute {
58
60
"id" : utils .ResourceIDAttribute (),
59
61
"policy_id" : schema.StringAttribute {
60
- Required : true ,
62
+ Optional : true ,
61
63
Description : "The ID of the agent policy in which the rule is saved" ,
62
64
},
63
65
"name" : schema.StringAttribute {
@@ -73,15 +75,13 @@ func (r *csmThreatsAgentRuleResource) Schema(_ context.Context, _ resource.Schem
73
75
Computed : true ,
74
76
},
75
77
"enabled" : schema.BoolAttribute {
76
- Required : true ,
77
- Description : "Indicates Whether the Agent rule is enabled." ,
78
+ Optional : true ,
79
+ Description : "Indicates whether the Agent rule is enabled. Must not be used without policy_id." ,
80
+ Computed : true ,
78
81
},
79
82
"expression" : schema.StringAttribute {
80
83
Required : true ,
81
84
Description : "The SECL expression of the Agent rule" ,
82
- PlanModifiers : []planmodifier.String {
83
- stringplanmodifier .RequiresReplace (),
84
- },
85
85
},
86
86
"product_tags" : schema.SetAttribute {
87
87
Optional : true ,
@@ -95,13 +95,15 @@ func (r *csmThreatsAgentRuleResource) Schema(_ context.Context, _ resource.Schem
95
95
96
96
func (r * csmThreatsAgentRuleResource ) ImportState (ctx context.Context , request resource.ImportStateRequest , response * resource.ImportStateResponse ) {
97
97
result := strings .SplitN (request .ID , ":" , 2 )
98
- if len (result ) != 2 {
99
- response .Diagnostics .AddError ("error retrieving policy_id or rule_id from given ID" , "" )
100
- return
101
- }
102
98
103
- response .Diagnostics .Append (response .State .SetAttribute (ctx , path .Root ("policy_id" ), result [0 ])... )
104
- response .Diagnostics .Append (response .State .SetAttribute (ctx , path .Root ("id" ), result [1 ])... )
99
+ if len (result ) == 2 {
100
+ response .Diagnostics .Append (response .State .SetAttribute (ctx , path .Root ("policy_id" ), result [0 ])... )
101
+ response .Diagnostics .Append (response .State .SetAttribute (ctx , path .Root ("id" ), result [1 ])... )
102
+ } else if len (result ) == 1 {
103
+ response .Diagnostics .Append (response .State .SetAttribute (ctx , path .Root ("id" ), result [0 ])... )
104
+ } else {
105
+ response .Diagnostics .AddError ("unexpected import format" , "expected '<policy_id>:<rule_id>' or '<rule_id>'" )
106
+ }
105
107
}
106
108
107
109
func (r * csmThreatsAgentRuleResource ) Create (ctx context.Context , request resource.CreateRequest , response * resource.CreateResponse ) {
@@ -111,6 +113,9 @@ func (r *csmThreatsAgentRuleResource) Create(ctx context.Context, request resour
111
113
return
112
114
}
113
115
116
+ csmThreatsMutex .Lock ()
117
+ defer csmThreatsMutex .Unlock ()
118
+
114
119
agentRulePayload , err := r .buildCreateCSMThreatsAgentRulePayload (& state )
115
120
if err != nil {
116
121
response .Diagnostics .AddError ("error while parsing resource" , err .Error ())
@@ -137,11 +142,23 @@ func (r *csmThreatsAgentRuleResource) Read(ctx context.Context, request resource
137
142
return
138
143
}
139
144
145
+ csmThreatsMutex .Lock ()
146
+ defer csmThreatsMutex .Unlock ()
147
+
140
148
agentRuleId := state .Id .ValueString ()
141
- policyId := state .PolicyId .ValueString ()
142
- res , httpResponse , err := r .api .GetCSMThreatsAgentRule (r .auth , agentRuleId , * datadogV2 .NewGetCSMThreatsAgentRuleOptionalParameters ().WithPolicyId (policyId ))
149
+
150
+ var res datadogV2.CloudWorkloadSecurityAgentRuleResponse
151
+ var httpResp * http.Response
152
+ var err error
153
+ if ! state .PolicyId .IsNull () && ! state .PolicyId .IsUnknown () {
154
+ policyId := state .PolicyId .ValueString ()
155
+ res , httpResp , err = r .api .GetCSMThreatsAgentRule (r .auth , agentRuleId , * datadogV2 .NewGetCSMThreatsAgentRuleOptionalParameters ().WithPolicyId (policyId ))
156
+ } else {
157
+ res , httpResp , err = r .api .GetCSMThreatsAgentRule (r .auth , agentRuleId )
158
+ }
159
+
143
160
if err != nil {
144
- if httpResponse != nil && httpResponse .StatusCode == 404 {
161
+ if httpResp .StatusCode == 404 {
145
162
response .State .RemoveResource (ctx )
146
163
return
147
164
}
@@ -164,9 +181,13 @@ func (r *csmThreatsAgentRuleResource) Update(ctx context.Context, request resour
164
181
return
165
182
}
166
183
184
+ csmThreatsMutex .Lock ()
185
+ defer csmThreatsMutex .Unlock ()
186
+
167
187
agentRulePayload , err := r .buildUpdateCSMThreatsAgentRulePayload (& state )
168
188
if err != nil {
169
189
response .Diagnostics .AddError ("error while parsing resource" , err .Error ())
190
+ return
170
191
}
171
192
172
193
res , _ , err := r .api .UpdateCSMThreatsAgentRule (r .auth , state .Id .ValueString (), * agentRulePayload )
@@ -190,11 +211,22 @@ func (r *csmThreatsAgentRuleResource) Delete(ctx context.Context, request resour
190
211
return
191
212
}
192
213
214
+ csmThreatsMutex .Lock ()
215
+ defer csmThreatsMutex .Unlock ()
216
+
193
217
id := state .Id .ValueString ()
194
- policyId := state .PolicyId .ValueString ()
195
- httpResp , err := r .api .DeleteCSMThreatsAgentRule (r .auth , id , * datadogV2 .NewDeleteCSMThreatsAgentRuleOptionalParameters ().WithPolicyId (policyId ))
218
+
219
+ var httpResp * http.Response
220
+ var err error
221
+ if ! state .PolicyId .IsNull () && ! state .PolicyId .IsUnknown () {
222
+ policyId := state .PolicyId .ValueString ()
223
+ httpResp , err = r .api .DeleteCSMThreatsAgentRule (r .auth , id , * datadogV2 .NewDeleteCSMThreatsAgentRuleOptionalParameters ().WithPolicyId (policyId ))
224
+ } else {
225
+ httpResp , err = r .api .DeleteCSMThreatsAgentRule (r .auth , id )
226
+ }
227
+
196
228
if err != nil {
197
- if httpResp != nil && httpResp .StatusCode == 404 {
229
+ if httpResp .StatusCode == 404 {
198
230
return
199
231
}
200
232
response .Diagnostics .Append (utils .FrameworkErrorDiag (err , "error deleting agent rule" ))
@@ -210,32 +242,39 @@ func (r *csmThreatsAgentRuleResource) buildCreateCSMThreatsAgentRulePayload(stat
210
242
attributes .Name = name
211
243
attributes .Description = description
212
244
attributes .Enabled = & enabled
213
- attributes .PolicyId = & policyId
245
+ attributes .PolicyId = policyId
214
246
attributes .ProductTags = productTags
215
247
216
248
data := datadogV2 .NewCloudWorkloadSecurityAgentRuleCreateData (attributes , datadogV2 .CLOUDWORKLOADSECURITYAGENTRULETYPE_AGENT_RULE )
217
249
return datadogV2 .NewCloudWorkloadSecurityAgentRuleCreateRequest (* data ), nil
218
250
}
219
251
220
252
func (r * csmThreatsAgentRuleResource ) buildUpdateCSMThreatsAgentRulePayload (state * csmThreatsAgentRuleModel ) (* datadogV2.CloudWorkloadSecurityAgentRuleUpdateRequest , error ) {
221
- agentRuleId , policyId , _ , description , enabled , _ , productTags := r .extractAgentRuleAttributesFromResource (state )
253
+ agentRuleId , policyId , _ , description , enabled , expression , productTags := r .extractAgentRuleAttributesFromResource (state )
222
254
223
255
attributes := datadogV2.CloudWorkloadSecurityAgentRuleUpdateAttributes {}
256
+ attributes .Expression = & expression
224
257
attributes .Description = description
225
258
attributes .Enabled = & enabled
226
- attributes .PolicyId = & policyId
259
+ attributes .PolicyId = policyId
227
260
attributes .ProductTags = productTags
228
261
229
262
data := datadogV2 .NewCloudWorkloadSecurityAgentRuleUpdateData (attributes , datadogV2 .CLOUDWORKLOADSECURITYAGENTRULETYPE_AGENT_RULE )
230
263
data .Id = & agentRuleId
231
264
return datadogV2 .NewCloudWorkloadSecurityAgentRuleUpdateRequest (* data ), nil
232
265
}
233
266
234
- func (r * csmThreatsAgentRuleResource ) extractAgentRuleAttributesFromResource (state * csmThreatsAgentRuleModel ) (string , string , string , * string , bool , string , []string ) {
267
+ func (r * csmThreatsAgentRuleResource ) extractAgentRuleAttributesFromResource (state * csmThreatsAgentRuleModel ) (string , * string , string , * string , bool , string , []string ) {
235
268
// Mandatory fields
236
269
id := state .Id .ValueString ()
237
- policyId := state .PolicyId .ValueString ()
238
270
name := state .Name .ValueString ()
271
+
272
+ // Optional fields
273
+ var policyId * string
274
+ if ! state .PolicyId .IsNull () && ! state .PolicyId .IsUnknown () {
275
+ val := state .PolicyId .ValueString ()
276
+ policyId = & val
277
+ }
239
278
enabled := state .Enabled .ValueBool ()
240
279
expression := state .Expression .ValueString ()
241
280
description := state .Description .ValueStringPointer ()
@@ -244,7 +283,7 @@ func (r *csmThreatsAgentRuleResource) extractAgentRuleAttributesFromResource(sta
244
283
for _ , tag := range state .ProductTags .Elements () {
245
284
tagStr , ok := tag .(types.String )
246
285
if ! ok {
247
- return "" , "" , "" , nil , false , "" , nil
286
+ return "" , nil , "" , nil , false , "" , nil
248
287
}
249
288
productTags = append (productTags , tagStr .ValueString ())
250
289
}
0 commit comments