Skip to content

Commit 5da3fb7

Browse files
committed
supporting cws multi-policy in terraform
1 parent 20a2aee commit 5da3fb7

14 files changed

+1211
-5
lines changed

datadog/fwprovider/data_source_datadog_csm_threats_agent_rule.go

+17-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type csmThreatsAgentRulesDataSource struct {
2525
}
2626

2727
type csmThreatsAgentRulesDataSourceModel struct {
28+
PolicyId types.String `tfsdk:"policy_id"`
2829
Id types.String `tfsdk:"id"`
2930
AgentRulesIds types.List `tfsdk:"agent_rules_ids"`
3031
AgentRules []csmThreatsAgentRuleModel `tfsdk:"agent_rules"`
@@ -51,7 +52,12 @@ func (r *csmThreatsAgentRulesDataSource) Read(ctx context.Context, request datas
5152
return
5253
}
5354

54-
res, _, err := r.api.ListCSMThreatsAgentRules(r.auth)
55+
policyId := state.PolicyId.ValueStringPointer()
56+
params := datadogV2.NewListCSMThreatsAgentRulesOptionalParameters()
57+
if !state.PolicyId.IsNull() && !state.PolicyId.IsUnknown() {
58+
params.WithPolicyId(*policyId)
59+
}
60+
res, _, err := r.api.ListCSMThreatsAgentRules(r.auth, *params)
5561
if err != nil {
5662
response.Diagnostics.Append(utils.FrameworkErrorDiag(err, "error while fetching agent rules"))
5763
return
@@ -75,7 +81,7 @@ func (r *csmThreatsAgentRulesDataSource) Read(ctx context.Context, request datas
7581
}
7682

7783
stateId := strings.Join(agentRuleIds, "--")
78-
state.Id = types.StringValue(computeAgentRulesDataSourceID(&stateId))
84+
state.Id = types.StringValue(computeDataSourceID(&stateId))
7985
tfAgentRuleIds, diags := types.ListValueFrom(ctx, types.StringType, agentRuleIds)
8086
response.Diagnostics.Append(diags...)
8187
state.AgentRulesIds = tfAgentRuleIds
@@ -84,11 +90,11 @@ func (r *csmThreatsAgentRulesDataSource) Read(ctx context.Context, request datas
8490
response.Diagnostics.Append(response.State.Set(ctx, &state)...)
8591
}
8692

87-
func computeAgentRulesDataSourceID(agentruleIds *string) string {
93+
func computeDataSourceID(ids *string) string {
8894
// Key for hashing
8995
var b strings.Builder
90-
if agentruleIds != nil {
91-
b.WriteString(*agentruleIds)
96+
if ids != nil {
97+
b.WriteString(*ids)
9298
}
9399
keyStr := b.String()
94100
h := sha256.New()
@@ -101,6 +107,12 @@ func (*csmThreatsAgentRulesDataSource) Schema(_ context.Context, _ datasource.Sc
101107
response.Schema = schema.Schema{
102108
Description: "Use this data source to retrieve information about existing Agent rules.",
103109
Attributes: map[string]schema.Attribute{
110+
// Input
111+
"policy_id": schema.StringAttribute{
112+
Description: "Listing only the rules in the policy with this field as the ID",
113+
Optional: true,
114+
},
115+
// Output
104116
"id": utils.ResourceIDAttribute(),
105117
"agent_rules_ids": schema.ListAttribute{
106118
Computed: true,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package fwprovider
2+
3+
import (
4+
"context"
5+
"strings"
6+
7+
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
8+
"github.com/hashicorp/terraform-plugin-framework/attr"
9+
"github.com/hashicorp/terraform-plugin-framework/datasource"
10+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
11+
"github.com/hashicorp/terraform-plugin-framework/types"
12+
13+
"github.com/terraform-providers/terraform-provider-datadog/datadog/internal/utils"
14+
)
15+
16+
var (
17+
_ datasource.DataSourceWithConfigure = &csmThreatsPoliciesDataSource{}
18+
)
19+
20+
type csmThreatsPoliciesDataSource struct {
21+
api *datadogV2.CSMThreatsApi
22+
auth context.Context
23+
}
24+
25+
type csmThreatsPoliciesDataSourceModel struct {
26+
Id types.String `tfsdk:"id"`
27+
PolicyIds types.List `tfsdk:"policy_ids"`
28+
Policies []csmThreatsPolicyModel `tfsdk:"policies"`
29+
}
30+
31+
func NewCSMThreatsPoliciesDataSource() datasource.DataSource {
32+
return &csmThreatsPoliciesDataSource{}
33+
}
34+
35+
func (r *csmThreatsPoliciesDataSource) Configure(_ context.Context, request datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
36+
providerData := request.ProviderData.(*FrameworkProvider)
37+
r.api = providerData.DatadogApiInstances.GetCSMThreatsApiV2()
38+
r.auth = providerData.Auth
39+
}
40+
41+
func (*csmThreatsPoliciesDataSource) Metadata(_ context.Context, _ datasource.MetadataRequest, response *datasource.MetadataResponse) {
42+
response.TypeName = "csm_threats_policies"
43+
}
44+
45+
func (r *csmThreatsPoliciesDataSource) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) {
46+
var state csmThreatsPoliciesDataSourceModel
47+
response.Diagnostics.Append(request.Config.Get(ctx, &state)...)
48+
if response.Diagnostics.HasError() {
49+
return
50+
}
51+
52+
res, _, err := r.api.ListCSMThreatsAgentPolicies(r.auth)
53+
if err != nil {
54+
response.Diagnostics.Append(utils.FrameworkErrorDiag(err, "error while fetching agent rules"))
55+
return
56+
}
57+
58+
data := res.GetData()
59+
policyIds := make([]string, len(data))
60+
policies := make([]csmThreatsPolicyModel, len(data))
61+
62+
for idx, policy := range res.GetData() {
63+
var policyModel csmThreatsPolicyModel
64+
policyModel.Id = types.StringValue(policy.GetId())
65+
attributes := policy.Attributes
66+
policyModel.Name = types.StringValue(attributes.GetName())
67+
policyModel.Description = types.StringValue(attributes.GetDescription())
68+
policyModel.Enabled = types.BoolValue(attributes.GetEnabled())
69+
policyModel.Tags, _ = types.SetValueFrom(ctx, types.StringType, attributes.GetHostTags())
70+
policyIds[idx] = policy.GetId()
71+
policies[idx] = policyModel
72+
}
73+
74+
stateId := strings.Join(policyIds, "--")
75+
state.Id = types.StringValue(computeDataSourceID(&stateId))
76+
tfAgentRuleIds, diags := types.ListValueFrom(ctx, types.StringType, policyIds)
77+
response.Diagnostics.Append(diags...)
78+
state.PolicyIds = tfAgentRuleIds
79+
state.Policies = policies
80+
81+
response.Diagnostics.Append(response.State.Set(ctx, &state)...)
82+
}
83+
84+
func (*csmThreatsPoliciesDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, response *datasource.SchemaResponse) {
85+
response.Schema = schema.Schema{
86+
Description: "Use this data source to retrieve information about existing policies.",
87+
Attributes: map[string]schema.Attribute{
88+
"id": utils.ResourceIDAttribute(),
89+
"policy_ids": schema.ListAttribute{
90+
Computed: true,
91+
Description: "List of IDs for the policies.",
92+
ElementType: types.StringType,
93+
},
94+
"policies": schema.ListAttribute{
95+
Computed: true,
96+
Description: "List of policies",
97+
ElementType: types.ObjectType{
98+
AttrTypes: map[string]attr.Type{
99+
"id": types.StringType,
100+
"tags": types.SetType{ElemType: types.StringType},
101+
"name": types.StringType,
102+
"description": types.StringType,
103+
"enabled": types.BoolType,
104+
},
105+
},
106+
},
107+
},
108+
}
109+
}

datadog/fwprovider/framework_provider.go

+3
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ var Resources = []func() resource.Resource{
6666
NewWebhookResource,
6767
NewWebhookCustomVariableResource,
6868
NewLogsCustomDestinationResource,
69+
NewCSMThreatsPolicyResource,
70+
NewCSMThreatsMultiPolicyAgentRuleResource,
6971
}
7072

7173
var Datasources = []func() datasource.DataSource{
@@ -86,6 +88,7 @@ var Datasources = []func() datasource.DataSource{
8688
NewDatadogRoleUsersDataSource,
8789
NewSecurityMonitoringSuppressionDataSource,
8890
NewCSMThreatsAgentRulesDataSource,
91+
NewCSMThreatsPoliciesDataSource,
8992
}
9093

9194
// FrameworkProvider struct

0 commit comments

Comments
 (0)