@@ -25,22 +25,44 @@ type csmThreatsAgentRulesDataSource struct {
25
25
}
26
26
27
27
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"`
31
41
}
32
42
33
43
func NewCSMThreatsAgentRulesDataSource () datasource.DataSource {
34
44
return & csmThreatsAgentRulesDataSource {}
35
45
}
36
46
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
+
39
61
r .api = providerData .DatadogApiInstances .GetCSMThreatsApiV2 ()
40
62
r .auth = providerData .Auth
41
63
}
42
64
43
- func (* csmThreatsAgentRulesDataSource ) Metadata (_ context.Context , _ datasource.MetadataRequest , response * datasource.MetadataResponse ) {
65
+ func (r * csmThreatsAgentRulesDataSource ) Metadata (_ context.Context , request datasource.MetadataRequest , response * datasource.MetadataResponse ) {
44
66
response .TypeName = "csm_threats_agent_rules"
45
67
}
46
68
@@ -51,31 +73,52 @@ func (r *csmThreatsAgentRulesDataSource) Read(ctx context.Context, request datas
51
73
return
52
74
}
53
75
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 )
55
83
if err != nil {
56
84
response .Diagnostics .Append (utils .FrameworkErrorDiag (err , "error while fetching agent rules" ))
57
85
return
58
86
}
59
87
60
88
data := res .GetData ()
61
89
agentRuleIds := make ([]string , len (data ))
62
- agentRules := make ([]csmThreatsAgentRuleModel , len (data ))
90
+ agentRules := make ([]csmThreatsAgentRuleDataSourceModel , len (data ))
63
91
64
92
for idx , agentRule := range res .GetData () {
65
- var agentRuleModel csmThreatsAgentRuleModel
93
+ var agentRuleModel csmThreatsAgentRuleDataSourceModel
66
94
agentRuleModel .Id = types .StringValue (agentRule .GetId ())
67
95
attributes := agentRule .Attributes
68
96
agentRuleModel .Name = types .StringValue (attributes .GetName ())
69
97
agentRuleModel .Description = types .StringValue (attributes .GetDescription ())
70
98
agentRuleModel .Enabled = types .BoolValue (attributes .GetEnabled ())
71
99
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
73
116
agentRuleIds [idx ] = agentRule .GetId ()
74
117
agentRules [idx ] = agentRuleModel
75
118
}
76
119
77
120
stateId := strings .Join (agentRuleIds , "--" )
78
- state .Id = types .StringValue (computeAgentRulesDataSourceID (& stateId ))
121
+ state .Id = types .StringValue (computeDataSourceID (& stateId ))
79
122
tfAgentRuleIds , diags := types .ListValueFrom (ctx , types .StringType , agentRuleIds )
80
123
response .Diagnostics .Append (diags ... )
81
124
state .AgentRulesIds = tfAgentRuleIds
@@ -84,24 +127,20 @@ func (r *csmThreatsAgentRulesDataSource) Read(ctx context.Context, request datas
84
127
response .Diagnostics .Append (response .State .Set (ctx , & state )... )
85
128
}
86
129
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
130
func (* csmThreatsAgentRulesDataSource ) Schema (_ context.Context , _ datasource.SchemaRequest , response * datasource.SchemaResponse ) {
101
131
response .Schema = schema.Schema {
102
132
Description : "Use this data source to retrieve information about existing Agent rules." ,
103
133
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
+ },
105
144
"agent_rules_ids" : schema.ListAttribute {
106
145
Computed : true ,
107
146
Description : "List of IDs for the Agent rules." ,
@@ -112,14 +151,28 @@ func (*csmThreatsAgentRulesDataSource) Schema(_ context.Context, _ datasource.Sc
112
151
Description : "List of Agent rules" ,
113
152
ElementType : types.ObjectType {
114
153
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 },
120
160
},
121
161
},
122
162
},
123
163
},
124
164
}
125
165
}
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