Skip to content

Commit

Permalink
add product_tags
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGuillard committed Feb 27, 2025
1 parent 6fc7905 commit a4741de
Showing 1 changed file with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type csmThreatsMultiPolicyAgentRuleModel struct {
Description types.String `tfsdk:"description"`
Enabled types.Bool `tfsdk:"enabled"`
Expression types.String `tfsdk:"expression"`
ProductTags types.Set `tfsdk:"product_tags"`
}

func NewCSMThreatsMultiPolicyAgentRuleResource() resource.Resource {
Expand Down Expand Up @@ -75,6 +76,11 @@ func (r *csmThreatsMultiPolicyAgentRuleResource) Schema(_ context.Context, _ res
stringplanmodifier.RequiresReplace(),
},
},
"product_tags": schema.SetAttribute{
Optional: true,
ElementType: types.StringType,
Description: "The list of product tags associated with the rule",
},
},
}
}
Expand Down Expand Up @@ -198,42 +204,54 @@ func (r *csmThreatsMultiPolicyAgentRuleResource) Delete(ctx context.Context, req
}

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

attributes := datadogV2.CloudWorkloadSecurityAgentRuleCreateAttributes{}
attributes.Expression = expression
attributes.Name = name
attributes.Description = description
attributes.Enabled = &enabled
attributes.PolicyId = &policyId
attributes.ProductTags = productTags

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

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

attributes := datadogV2.CloudWorkloadSecurityAgentRuleUpdateAttributes{}
attributes.Description = description
attributes.Enabled = &enabled
attributes.PolicyId = &policyId
attributes.ProductTags = productTags

data := datadogV2.NewCloudWorkloadSecurityAgentRuleUpdateData(attributes, datadogV2.CLOUDWORKLOADSECURITYAGENTRULETYPE_AGENT_RULE)
data.Id = &agentRuleId
return datadogV2.NewCloudWorkloadSecurityAgentRuleUpdateRequest(*data), nil
}

func (r *csmThreatsMultiPolicyAgentRuleResource) extractAgentRuleAttributesFromResource(state *csmThreatsMultiPolicyAgentRuleModel) (string, string, string, *string, bool, string) {
func (r *csmThreatsMultiPolicyAgentRuleResource) extractAgentRuleAttributesFromResource(state *csmThreatsMultiPolicyAgentRuleModel) (string, string, string, *string, bool, string, []string) {
// Mandatory fields
id := state.Id.ValueString()
policyId := state.PolicyId.ValueString()
name := state.Name.ValueString()
enabled := state.Enabled.ValueBool()
expression := state.Expression.ValueString()
description := state.Description.ValueStringPointer()
var productTags []string
if !state.ProductTags.IsNull() && !state.ProductTags.IsUnknown() {
for _, tag := range state.ProductTags.Elements() {
tagStr, ok := tag.(types.String)
if !ok {
return "", "", "", nil, false, "", nil
}
productTags = append(productTags, tagStr.ValueString())
}
}

return id, policyId, name, description, enabled, expression
return id, policyId, name, description, enabled, expression, productTags
}

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

0 comments on commit a4741de

Please sign in to comment.