Skip to content

Commit 2481912

Browse files
remove multi_policy_agent_rule resource
1 parent b528f99 commit 2481912

File tree

33 files changed

+1635
-2263
lines changed

33 files changed

+1635
-2263
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package datadog
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/terraform-providers/terraform-provider-datadog/datadog/internal/utils"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
)
12+
13+
func dataSourceDatadogCloudWorkloadSecurityAgentRules() *schema.Resource {
14+
return &schema.Resource{
15+
Description: "Use this data source to retrieve information about existing Cloud Workload Security Agent Rules for use in other resources. Deprecated, use datadog_csm_threats_agent_rules data source instead: https://registry.terraform.io/providers/DataDog/datadog/latest/docs/data-sources/csm_threats_agent_rules",
16+
DeprecationMessage: "This data source is deprecated — use the datadog_csm_threats_agent_rules data source instead: https://registry.terraform.io/providers/DataDog/datadog/latest/docs/data-sources/csm_threats_agent_rules",
17+
ReadContext: dataSourceDatadogCloudWorkloadSecurityAgentRulesRead,
18+
19+
SchemaFunc: func() map[string]*schema.Schema {
20+
return map[string]*schema.Schema{
21+
// Computed
22+
"agent_rules": {
23+
Description: "List of Agent rules.",
24+
Type: schema.TypeList,
25+
Computed: true,
26+
Elem: &schema.Resource{
27+
Schema: map[string]*schema.Schema{
28+
"id": {
29+
Type: schema.TypeString,
30+
Computed: true,
31+
Description: "The id of the Agent rule.",
32+
},
33+
"description": {
34+
Type: schema.TypeString,
35+
Computed: true,
36+
Description: "The description of the Agent rule.",
37+
},
38+
"enabled": {
39+
Type: schema.TypeBool,
40+
Computed: true,
41+
Description: "Whether the Agent rule is enabled.",
42+
},
43+
"expression": {
44+
Type: schema.TypeString,
45+
Computed: true,
46+
Description: "The SECL expression of the Agent rule.",
47+
},
48+
"name": {
49+
Type: schema.TypeString,
50+
Computed: true,
51+
Description: "The name of the Agent rule.",
52+
},
53+
},
54+
},
55+
},
56+
}
57+
},
58+
}
59+
}
60+
61+
func dataSourceDatadogCloudWorkloadSecurityAgentRulesRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
62+
providerConf := meta.(*ProviderConfiguration)
63+
apiInstances := providerConf.DatadogApiInstances
64+
auth := providerConf.Auth
65+
66+
agentRules := make([]map[string]interface{}, 0)
67+
response, httpresp, err := apiInstances.GetCSMThreatsApiV2().ListCloudWorkloadSecurityAgentRules(auth)
68+
if err != nil {
69+
return utils.TranslateClientErrorDiag(err, httpresp, "error listing agent rules")
70+
}
71+
72+
diags := diag.Diagnostics{}
73+
for _, agentRule := range response.GetData() {
74+
if err := utils.CheckForUnparsed(agentRule); err != nil {
75+
diags = append(diags, diag.Diagnostic{
76+
Severity: diag.Warning,
77+
Summary: fmt.Sprintf("skipping agent rule with id: %s", agentRule.GetId()),
78+
Detail: fmt.Sprintf("rule contains unparsed object: %v", err),
79+
})
80+
continue
81+
}
82+
83+
// extract agent rule
84+
agentRuleTF := make(map[string]interface{})
85+
attributes := agentRule.GetAttributes()
86+
87+
agentRuleTF["id"] = agentRule.GetId()
88+
agentRuleTF["name"] = attributes.GetName()
89+
agentRuleTF["description"] = attributes.GetDescription()
90+
agentRuleTF["expression"] = attributes.GetExpression()
91+
agentRuleTF["enabled"] = attributes.GetEnabled()
92+
93+
agentRules = append(agentRules, agentRuleTF)
94+
}
95+
96+
d.SetId("cloud-workload-security-agent-rules")
97+
d.Set("agent_rules", agentRules)
98+
99+
return diags
100+
}

datadog/fwprovider/data_source_datadog_csm_threats_agent_rule.go

Lines changed: 83 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,44 @@ type csmThreatsAgentRulesDataSource struct {
2525
}
2626

2727
type csmThreatsAgentRulesDataSourceModel struct {
28-
Id types.String `tfsdk:"id"`
29-
AgentRulesIds types.List `tfsdk:"agent_rules_ids"`
30-
AgentRules []csmThreatsAgentRuleModel `tfsdk:"agent_rules"`
28+
PolicyId types.String `tfsdk:"policy_id"`
29+
Id types.String `tfsdk:"id"`
30+
AgentRulesIds types.List `tfsdk:"agent_rules_ids"`
31+
AgentRules []csmThreatsAgentRuleDataSourceModel `tfsdk:"agent_rules"`
32+
}
33+
34+
type csmThreatsAgentRuleDataSourceModel struct {
35+
Id types.String `tfsdk:"id"`
36+
Name types.String `tfsdk:"name"`
37+
Description types.String `tfsdk:"description"`
38+
Enabled types.Bool `tfsdk:"enabled"`
39+
Expression types.String `tfsdk:"expression"`
40+
ProductTags types.Set `tfsdk:"product_tags"`
3141
}
3242

3343
func NewCSMThreatsAgentRulesDataSource() datasource.DataSource {
3444
return &csmThreatsAgentRulesDataSource{}
3545
}
3646

37-
func (r *csmThreatsAgentRulesDataSource) Configure(_ context.Context, request datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
38-
providerData := request.ProviderData.(*FrameworkProvider)
47+
func (r *csmThreatsAgentRulesDataSource) Configure(_ context.Context, request datasource.ConfigureRequest, response *datasource.ConfigureResponse) {
48+
if request.ProviderData == nil {
49+
return
50+
}
51+
52+
providerData, ok := request.ProviderData.(*FrameworkProvider)
53+
if !ok {
54+
response.Diagnostics.AddError(
55+
"Unexpected Resource Configure Type",
56+
fmt.Sprintf("Expected *FrameworkProvider, got: %T. Please report this issue to the provider developers.", request.ProviderData),
57+
)
58+
return
59+
}
60+
3961
r.api = providerData.DatadogApiInstances.GetCSMThreatsApiV2()
4062
r.auth = providerData.Auth
4163
}
4264

43-
func (*csmThreatsAgentRulesDataSource) Metadata(_ context.Context, _ datasource.MetadataRequest, response *datasource.MetadataResponse) {
65+
func (r *csmThreatsAgentRulesDataSource) Metadata(_ context.Context, request datasource.MetadataRequest, response *datasource.MetadataResponse) {
4466
response.TypeName = "csm_threats_agent_rules"
4567
}
4668

@@ -51,31 +73,52 @@ func (r *csmThreatsAgentRulesDataSource) Read(ctx context.Context, request datas
5173
return
5274
}
5375

54-
res, _, err := r.api.ListCloudWorkloadSecurityAgentRules(r.auth)
76+
params := datadogV2.NewListCSMThreatsAgentRulesOptionalParameters()
77+
if !state.PolicyId.IsNull() && !state.PolicyId.IsUnknown() {
78+
policyId := state.PolicyId.ValueString()
79+
params.WithPolicyId(policyId)
80+
}
81+
82+
res, _, err := r.api.ListCSMThreatsAgentRules(r.auth, *params)
5583
if err != nil {
5684
response.Diagnostics.Append(utils.FrameworkErrorDiag(err, "error while fetching agent rules"))
5785
return
5886
}
5987

6088
data := res.GetData()
6189
agentRuleIds := make([]string, len(data))
62-
agentRules := make([]csmThreatsAgentRuleModel, len(data))
90+
agentRules := make([]csmThreatsAgentRuleDataSourceModel, len(data))
6391

6492
for idx, agentRule := range res.GetData() {
65-
var agentRuleModel csmThreatsAgentRuleModel
93+
var agentRuleModel csmThreatsAgentRuleDataSourceModel
6694
agentRuleModel.Id = types.StringValue(agentRule.GetId())
6795
attributes := agentRule.Attributes
6896
agentRuleModel.Name = types.StringValue(attributes.GetName())
6997
agentRuleModel.Description = types.StringValue(attributes.GetDescription())
7098
agentRuleModel.Enabled = types.BoolValue(attributes.GetEnabled())
7199
agentRuleModel.Expression = types.StringValue(*attributes.Expression)
72-
100+
tags := attributes.GetProductTags()
101+
tagSet := make(map[string]struct{})
102+
for _, tag := range tags {
103+
tagSet[tag] = struct{}{}
104+
}
105+
uniqueTags := make([]string, 0, len(tagSet))
106+
for tag := range tagSet {
107+
uniqueTags = append(uniqueTags, tag)
108+
}
109+
110+
productTags, diags := types.SetValueFrom(ctx, types.StringType, uniqueTags)
111+
if diags.HasError() {
112+
response.Diagnostics.Append(diags...)
113+
continue
114+
}
115+
agentRuleModel.ProductTags = productTags
73116
agentRuleIds[idx] = agentRule.GetId()
74117
agentRules[idx] = agentRuleModel
75118
}
76119

77120
stateId := strings.Join(agentRuleIds, "--")
78-
state.Id = types.StringValue(computeAgentRulesDataSourceID(&stateId))
121+
state.Id = types.StringValue(computeDataSourceID(&stateId))
79122
tfAgentRuleIds, diags := types.ListValueFrom(ctx, types.StringType, agentRuleIds)
80123
response.Diagnostics.Append(diags...)
81124
state.AgentRulesIds = tfAgentRuleIds
@@ -84,24 +127,20 @@ func (r *csmThreatsAgentRulesDataSource) Read(ctx context.Context, request datas
84127
response.Diagnostics.Append(response.State.Set(ctx, &state)...)
85128
}
86129

87-
func computeAgentRulesDataSourceID(agentruleIds *string) string {
88-
// Key for hashing
89-
var b strings.Builder
90-
if agentruleIds != nil {
91-
b.WriteString(*agentruleIds)
92-
}
93-
keyStr := b.String()
94-
h := sha256.New()
95-
h.Write([]byte(keyStr))
96-
97-
return fmt.Sprintf("%x", h.Sum(nil))
98-
}
99-
100130
func (*csmThreatsAgentRulesDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, response *datasource.SchemaResponse) {
101131
response.Schema = schema.Schema{
102132
Description: "Use this data source to retrieve information about existing Agent rules.",
103133
Attributes: map[string]schema.Attribute{
104-
"id": utils.ResourceIDAttribute(),
134+
// Input
135+
"policy_id": schema.StringAttribute{
136+
Description: "Listing only the rules in the policy with this field as the ID",
137+
Optional: true,
138+
},
139+
// Output
140+
"id": schema.StringAttribute{
141+
Description: "The ID of the data source",
142+
Computed: true,
143+
},
105144
"agent_rules_ids": schema.ListAttribute{
106145
Computed: true,
107146
Description: "List of IDs for the Agent rules.",
@@ -112,14 +151,28 @@ func (*csmThreatsAgentRulesDataSource) Schema(_ context.Context, _ datasource.Sc
112151
Description: "List of Agent rules",
113152
ElementType: types.ObjectType{
114153
AttrTypes: map[string]attr.Type{
115-
"id": types.StringType,
116-
"name": types.StringType,
117-
"description": types.StringType,
118-
"enabled": types.BoolType,
119-
"expression": types.StringType,
154+
"id": types.StringType,
155+
"name": types.StringType,
156+
"description": types.StringType,
157+
"enabled": types.BoolType,
158+
"expression": types.StringType,
159+
"product_tags": types.SetType{ElemType: types.StringType},
120160
},
121161
},
122162
},
123163
},
124164
}
125165
}
166+
167+
func computeDataSourceID(ids *string) string {
168+
// Key for hashing
169+
var b strings.Builder
170+
if ids != nil {
171+
b.WriteString(*ids)
172+
}
173+
keyStr := b.String()
174+
h := sha256.New()
175+
h.Write([]byte(keyStr))
176+
177+
return fmt.Sprintf("%x", h.Sum(nil))
178+
}

0 commit comments

Comments
 (0)