Skip to content

Commit 0d839f7

Browse files
restore legacy anget_rule resource, fix tests
1 parent 1d306ae commit 0d839f7

13 files changed

+646
-514
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package fwprovider
2+
3+
import (
4+
"context"
5+
"crypto/sha256"
6+
"fmt"
7+
"strings"
8+
9+
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
10+
"github.com/hashicorp/terraform-plugin-framework/attr"
11+
"github.com/hashicorp/terraform-plugin-framework/datasource"
12+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
13+
"github.com/hashicorp/terraform-plugin-framework/types"
14+
15+
"github.com/terraform-providers/terraform-provider-datadog/datadog/internal/utils"
16+
)
17+
18+
var (
19+
_ datasource.DataSourceWithConfigure = &csmThreatsAgentRulesDataSource{}
20+
)
21+
22+
type csmThreatsAgentRulesDataSource struct {
23+
api *datadogV2.CSMThreatsApi
24+
auth context.Context
25+
}
26+
27+
type csmThreatsAgentRulesDataSourceModel struct {
28+
Id types.String `tfsdk:"id"`
29+
AgentRulesIds types.List `tfsdk:"agent_rules_ids"`
30+
AgentRules []csmThreatsAgentRuleModel `tfsdk:"agent_rules"`
31+
}
32+
33+
func NewCSMThreatsAgentRulesDataSource() datasource.DataSource {
34+
return &csmThreatsAgentRulesDataSource{}
35+
}
36+
37+
func (r *csmThreatsAgentRulesDataSource) Configure(_ context.Context, request datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
38+
providerData := request.ProviderData.(*FrameworkProvider)
39+
r.api = providerData.DatadogApiInstances.GetCSMThreatsApiV2()
40+
r.auth = providerData.Auth
41+
}
42+
43+
func (*csmThreatsAgentRulesDataSource) Metadata(_ context.Context, _ datasource.MetadataRequest, response *datasource.MetadataResponse) {
44+
response.TypeName = "csm_threats_agent_rules"
45+
}
46+
47+
func (r *csmThreatsAgentRulesDataSource) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) {
48+
var state csmThreatsAgentRulesDataSourceModel
49+
response.Diagnostics.Append(request.Config.Get(ctx, &state)...)
50+
if response.Diagnostics.HasError() {
51+
return
52+
}
53+
54+
res, _, err := r.api.ListCSMThreatsAgentRules(r.auth)
55+
if err != nil {
56+
response.Diagnostics.Append(utils.FrameworkErrorDiag(err, "error while fetching agent rules"))
57+
return
58+
}
59+
60+
data := res.GetData()
61+
agentRuleIds := make([]string, len(data))
62+
agentRules := make([]csmThreatsAgentRuleModel, len(data))
63+
64+
for idx, agentRule := range res.GetData() {
65+
var agentRuleModel csmThreatsAgentRuleModel
66+
agentRuleModel.Id = types.StringValue(agentRule.GetId())
67+
attributes := agentRule.Attributes
68+
agentRuleModel.Name = types.StringValue(attributes.GetName())
69+
agentRuleModel.Description = types.StringValue(attributes.GetDescription())
70+
agentRuleModel.Enabled = types.BoolValue(attributes.GetEnabled())
71+
agentRuleModel.Expression = types.StringValue(*attributes.Expression)
72+
73+
agentRuleIds[idx] = agentRule.GetId()
74+
agentRules[idx] = agentRuleModel
75+
}
76+
77+
stateId := strings.Join(agentRuleIds, "--")
78+
state.Id = types.StringValue(computeAgentRulesDataSourceID(&stateId))
79+
tfAgentRuleIds, diags := types.ListValueFrom(ctx, types.StringType, agentRuleIds)
80+
response.Diagnostics.Append(diags...)
81+
state.AgentRulesIds = tfAgentRuleIds
82+
state.AgentRules = agentRules
83+
84+
response.Diagnostics.Append(response.State.Set(ctx, &state)...)
85+
}
86+
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+
100+
func (*csmThreatsAgentRulesDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, response *datasource.SchemaResponse) {
101+
response.Schema = schema.Schema{
102+
Description: "Use this data source to retrieve information about existing Agent rules.",
103+
Attributes: map[string]schema.Attribute{
104+
"id": utils.ResourceIDAttribute(),
105+
"agent_rules_ids": schema.ListAttribute{
106+
Computed: true,
107+
Description: "List of IDs for the Agent rules.",
108+
ElementType: types.StringType,
109+
},
110+
"agent_rules": schema.ListAttribute{
111+
Computed: true,
112+
Description: "List of Agent rules",
113+
ElementType: types.ObjectType{
114+
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,
120+
},
121+
},
122+
},
123+
},
124+
}
125+
}

datadog/fwprovider/data_source_datadog_csm_threats_multi_policy_agent_rules.go

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,28 @@ func (r *csmThreatsMultiPolicyAgentRulesDataSource) Read(ctx context.Context, re
9797
agentRuleModel.Description = types.StringValue(attributes.GetDescription())
9898
agentRuleModel.Enabled = types.BoolValue(attributes.GetEnabled())
9999
agentRuleModel.Expression = types.StringValue(*attributes.Expression)
100-
agentRuleModel.ProductTags, _ = types.SetValueFrom(ctx, types.StringType, attributes.GetProductTags())
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
101116
agentRuleIds[idx] = agentRule.GetId()
102117
agentRules[idx] = agentRuleModel
103118
}
104119

105120
stateId := strings.Join(agentRuleIds, "--")
106-
state.Id = types.StringValue(computeMultiPolicyAgentRulesID(&stateId))
121+
state.Id = types.StringValue(computeDataSourceID(&stateId))
107122
tfAgentRuleIds, diags := types.ListValueFrom(ctx, types.StringType, agentRuleIds)
108123
response.Diagnostics.Append(diags...)
109124
state.AgentRulesIds = tfAgentRuleIds
@@ -112,19 +127,6 @@ func (r *csmThreatsMultiPolicyAgentRulesDataSource) Read(ctx context.Context, re
112127
response.Diagnostics.Append(response.State.Set(ctx, &state)...)
113128
}
114129

115-
func computeMultiPolicyAgentRulesID(ids *string) string {
116-
// Key for hashing
117-
var b strings.Builder
118-
if ids != nil {
119-
b.WriteString(*ids)
120-
}
121-
keyStr := b.String()
122-
h := sha256.New()
123-
h.Write([]byte(keyStr))
124-
125-
return fmt.Sprintf("%x", h.Sum(nil))
126-
}
127-
128130
func (*csmThreatsMultiPolicyAgentRulesDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, response *datasource.SchemaResponse) {
129131
response.Schema = schema.Schema{
130132
Description: "Use this data source to retrieve information about existing Agent rules.",
@@ -135,7 +137,10 @@ func (*csmThreatsMultiPolicyAgentRulesDataSource) Schema(_ context.Context, _ da
135137
Optional: true,
136138
},
137139
// Output
138-
"id": utils.ResourceIDAttribute(),
140+
"id": schema.StringAttribute{
141+
Description: "The ID of the data source",
142+
Computed: true,
143+
},
139144
"agent_rules_ids": schema.ListAttribute{
140145
Computed: true,
141146
Description: "List of IDs for the Agent rules.",
@@ -158,3 +163,16 @@ func (*csmThreatsMultiPolicyAgentRulesDataSource) Schema(_ context.Context, _ da
158163
},
159164
}
160165
}
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+
}

datadog/fwprovider/data_source_datadog_csm_threats_policy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (r *csmThreatsPoliciesDataSource) Read(ctx context.Context, request datasou
7272
}
7373

7474
stateId := strings.Join(policyIds, "--")
75-
state.Id = types.StringValue(computeMultiPolicyAgentRulesID(&stateId))
75+
state.Id = types.StringValue(computeDataSourceID(&stateId))
7676
tfAgentRuleIds, diags := types.ListValueFrom(ctx, types.StringType, policyIds)
7777
response.Diagnostics.Append(diags...)
7878
state.PolicyIds = tfAgentRuleIds

datadog/fwprovider/framework_provider.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ var Resources = []func() resource.Resource{
7979
NewActionConnectionResource,
8080
NewWorkflowAutomationResource,
8181
NewAppBuilderAppResource,
82-
NewCSMThreatsPoliciesListResource,
82+
NewCSMThreatsAgentRuleResource,
8383
NewCSMThreatsPolicyResource,
8484
NewCSMThreatsMultiPolicyAgentRuleResource,
8585
}
@@ -110,6 +110,7 @@ var Datasources = []func() datasource.DataSource{
110110
NewDatadogSyntheticsGlobalVariableDataSource,
111111
NewWorkflowAutomationDataSource,
112112
NewDatadogAppBuilderAppDataSource,
113+
NewCSMThreatsAgentRulesDataSource,
113114
NewCSMThreatsPoliciesDataSource,
114115
}
115116

0 commit comments

Comments
 (0)