6
6
7
7
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
8
8
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
9
+ "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
9
10
"github.com/hashicorp/terraform-plugin-framework/attr"
10
11
"github.com/hashicorp/terraform-plugin-framework/resource"
11
12
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
@@ -16,14 +17,14 @@ import (
16
17
"github.com/terraform-providers/terraform-provider-datadog/datadog/internal/validators"
17
18
)
18
19
19
- var _ resource.Resource = & customFrameworkResource {}
20
+ var _ resource.Resource = & complianceCustomFrameworkResource {}
20
21
21
- type customFrameworkResource struct {
22
+ type complianceCustomFrameworkResource struct {
22
23
Api * datadogV2.SecurityMonitoringApi
23
24
Auth context.Context
24
25
}
25
26
26
- type customFrameworkModel struct {
27
+ type complianceCustomFrameworkModel struct {
27
28
ID types.String `tfsdk:"id"`
28
29
Description types.String `tfsdk:"description"`
29
30
Version types.String `tfsdk:"version"`
@@ -33,33 +34,42 @@ type customFrameworkModel struct {
33
34
Requirements types.Set `tfsdk:"requirements"` // have to define requirements as a set to be unordered
34
35
}
35
36
36
- func NewCustomFrameworkResource () resource.Resource {
37
- return & customFrameworkResource {}
37
+ func NewComplianceCustomFrameworkResource () resource.Resource {
38
+ return & complianceCustomFrameworkResource {}
38
39
}
39
40
40
- func (r * customFrameworkResource ) Metadata (_ context.Context , _ resource.MetadataRequest , response * resource.MetadataResponse ) {
41
- response .TypeName = "custom_framework "
41
+ func (r * complianceCustomFrameworkResource ) Metadata (_ context.Context , _ resource.MetadataRequest , response * resource.MetadataResponse ) {
42
+ response .TypeName = "compliance_custom_framework "
42
43
}
43
44
44
- func (r * customFrameworkResource ) Schema (_ context.Context , _ resource.SchemaRequest , response * resource.SchemaResponse ) {
45
+ func (r * complianceCustomFrameworkResource ) Schema (_ context.Context , _ resource.SchemaRequest , response * resource.SchemaResponse ) {
45
46
response .Schema = schema.Schema {
46
- Description : "Manages custom framework in Datadog ." ,
47
+ Description : "Provides a Datadog Compliance Custom Framework resource, which is used to create and manage compliance custom frameworks ." ,
47
48
Attributes : map [string ]schema.Attribute {
48
49
"id" : schema.StringAttribute {
49
- Description : "The ID of the custom framework resource." ,
50
+ Description : "The ID of the compliance custom framework resource." ,
50
51
Computed : true ,
51
52
},
52
53
"version" : schema.StringAttribute {
53
54
Description : "The framework version." ,
54
- Required : true ,
55
+ Validators : []validator.String {
56
+ stringvalidator .LengthAtLeast (1 ),
57
+ },
58
+ Required : true ,
55
59
},
56
60
"handle" : schema.StringAttribute {
57
61
Description : "The framework handle." ,
58
- Required : true ,
62
+ Validators : []validator.String {
63
+ stringvalidator .LengthAtLeast (1 ),
64
+ },
65
+ Required : true ,
59
66
},
60
67
"name" : schema.StringAttribute {
61
68
Description : "The framework name." ,
62
- Required : true ,
69
+ Validators : []validator.String {
70
+ stringvalidator .LengthAtLeast (1 ),
71
+ },
72
+ Required : true ,
63
73
},
64
74
"icon_url" : schema.StringAttribute {
65
75
Description : "The URL of the icon representing the framework." ,
@@ -74,28 +84,34 @@ func (r *customFrameworkResource) Schema(_ context.Context, _ resource.SchemaReq
74
84
"requirements" : schema.SetNestedBlock {
75
85
Description : "The requirements of the framework." ,
76
86
Validators : []validator.Set {
77
- setvalidator .SizeAtLeast ( 1 ),
87
+ setvalidator .IsRequired ( ),
78
88
validators .RequirementNameValidator (),
79
89
},
80
90
NestedObject : schema.NestedBlockObject {
81
91
Attributes : map [string ]schema.Attribute {
82
92
"name" : schema.StringAttribute {
83
93
Description : "The name of the requirement." ,
84
94
Required : true ,
95
+ Validators : []validator.String {
96
+ stringvalidator .LengthAtLeast (1 ),
97
+ },
85
98
},
86
99
},
87
100
Blocks : map [string ]schema.Block {
88
101
"controls" : schema.SetNestedBlock {
89
102
Description : "The controls of the requirement." ,
90
103
Validators : []validator.Set {
91
- setvalidator .SizeAtLeast ( 1 ),
104
+ setvalidator .IsRequired ( ),
92
105
validators .ControlNameValidator (),
93
106
},
94
107
NestedObject : schema.NestedBlockObject {
95
108
Attributes : map [string ]schema.Attribute {
96
109
"name" : schema.StringAttribute {
97
110
Description : "The name of the control." ,
98
111
Required : true ,
112
+ Validators : []validator.String {
113
+ stringvalidator .LengthAtLeast (1 ),
114
+ },
99
115
},
100
116
"rules_id" : schema.SetAttribute {
101
117
Description : "The list of rules IDs for the control." ,
@@ -112,14 +128,14 @@ func (r *customFrameworkResource) Schema(_ context.Context, _ resource.SchemaReq
112
128
}
113
129
}
114
130
115
- func (r * customFrameworkResource ) Configure (_ context.Context , request resource.ConfigureRequest , response * resource.ConfigureResponse ) {
131
+ func (r * complianceCustomFrameworkResource ) Configure (_ context.Context , request resource.ConfigureRequest , response * resource.ConfigureResponse ) {
116
132
providerData , _ := request .ProviderData .(* FrameworkProvider )
117
133
r .Api = providerData .DatadogApiInstances .GetSecurityMonitoringApiV2 ()
118
134
r .Auth = providerData .Auth
119
135
}
120
136
121
- func (r * customFrameworkResource ) Create (ctx context.Context , request resource.CreateRequest , response * resource.CreateResponse ) {
122
- var state customFrameworkModel
137
+ func (r * complianceCustomFrameworkResource ) Create (ctx context.Context , request resource.CreateRequest , response * resource.CreateResponse ) {
138
+ var state complianceCustomFrameworkModel
123
139
diags := request .Config .Get (ctx , & state )
124
140
response .Diagnostics .Append (diags ... )
125
141
if response .Diagnostics .HasError () {
@@ -129,16 +145,16 @@ func (r *customFrameworkResource) Create(ctx context.Context, request resource.C
129
145
_ , _ , err := r .Api .CreateCustomFramework (r .Auth , * buildCreateFrameworkRequest (state ))
130
146
131
147
if err != nil {
132
- response .Diagnostics .Append (utils .FrameworkErrorDiag (err , "error creating custom framework" ))
148
+ response .Diagnostics .Append (utils .FrameworkErrorDiag (err , "error creating compliance custom framework" ))
133
149
return
134
150
}
135
151
state .ID = types .StringValue (state .Handle .ValueString () + string ('-' ) + state .Version .ValueString ())
136
152
diags = response .State .Set (ctx , & state )
137
153
response .Diagnostics .Append (diags ... )
138
154
}
139
155
140
- func (r * customFrameworkResource ) Delete (ctx context.Context , request resource.DeleteRequest , response * resource.DeleteResponse ) {
141
- var state customFrameworkModel
156
+ func (r * complianceCustomFrameworkResource ) Delete (ctx context.Context , request resource.DeleteRequest , response * resource.DeleteResponse ) {
157
+ var state complianceCustomFrameworkModel
142
158
diags := request .State .Get (ctx , & state )
143
159
response .Diagnostics .Append (diags ... )
144
160
if response .Diagnostics .HasError () {
@@ -151,8 +167,8 @@ func (r *customFrameworkResource) Delete(ctx context.Context, request resource.D
151
167
}
152
168
}
153
169
154
- func (r * customFrameworkResource ) Read (ctx context.Context , request resource.ReadRequest , response * resource.ReadResponse ) {
155
- var state customFrameworkModel
170
+ func (r * complianceCustomFrameworkResource ) Read (ctx context.Context , request resource.ReadRequest , response * resource.ReadResponse ) {
171
+ var state complianceCustomFrameworkModel
156
172
diags := request .State .Get (ctx , & state )
157
173
response .Diagnostics .Append (diags ... )
158
174
if response .Diagnostics .HasError () {
@@ -169,16 +185,16 @@ func (r *customFrameworkResource) Read(ctx context.Context, request resource.Rea
169
185
}
170
186
// this is for any other error
171
187
if err != nil {
172
- response .Diagnostics .Append (utils .FrameworkErrorDiag (err , "error reading custom framework" ))
188
+ response .Diagnostics .Append (utils .FrameworkErrorDiag (err , "error reading compliance custom framework" ))
173
189
return
174
190
}
175
191
databaseState := readStateFromDatabase (data , state .Handle .ValueString (), state .Version .ValueString ())
176
192
diags = response .State .Set (ctx , & databaseState )
177
193
response .Diagnostics .Append (diags ... )
178
194
}
179
195
180
- func (r * customFrameworkResource ) Update (ctx context.Context , request resource.UpdateRequest , response * resource.UpdateResponse ) {
181
- var state customFrameworkModel
196
+ func (r * complianceCustomFrameworkResource ) Update (ctx context.Context , request resource.UpdateRequest , response * resource.UpdateResponse ) {
197
+ var state complianceCustomFrameworkModel
182
198
diags := request .Config .Get (ctx , & state )
183
199
response .Diagnostics .Append (diags ... )
184
200
if response .Diagnostics .HasError () {
@@ -187,7 +203,7 @@ func (r *customFrameworkResource) Update(ctx context.Context, request resource.U
187
203
188
204
_ , _ , err := r .Api .UpdateCustomFramework (r .Auth , state .Handle .ValueString (), state .Version .ValueString (), * buildUpdateFrameworkRequest (state ))
189
205
if err != nil {
190
- response .Diagnostics .Append (utils .FrameworkErrorDiag (err , "error updating custom framework" ))
206
+ response .Diagnostics .Append (utils .FrameworkErrorDiag (err , "error updating compliance custom framework" ))
191
207
return
192
208
}
193
209
diags = response .State .Set (ctx , & state )
@@ -238,9 +254,9 @@ func setRequirements(requirements []attr.Value) types.Set {
238
254
requirements ,
239
255
)
240
256
}
241
- func readStateFromDatabase (data datadogV2.GetCustomFrameworkResponse , handle string , version string ) customFrameworkModel {
257
+ func readStateFromDatabase (data datadogV2.GetCustomFrameworkResponse , handle string , version string ) complianceCustomFrameworkModel {
242
258
// Set the state
243
- var state customFrameworkModel
259
+ var state complianceCustomFrameworkModel
244
260
state .ID = types .StringValue (handle + "-" + version )
245
261
state .Handle = types .StringValue (handle )
246
262
state .Version = types .StringValue (version )
@@ -267,7 +283,7 @@ func readStateFromDatabase(data datadogV2.GetCustomFrameworkResponse, handle str
267
283
}
268
284
269
285
// ImportState is used to import a resource from an existing framework so we can update it if it exists in the database and not in terraform
270
- func (r * customFrameworkResource ) ImportState (ctx context.Context , request resource.ImportStateRequest , response * resource.ImportStateResponse ) {
286
+ func (r * complianceCustomFrameworkResource ) ImportState (ctx context.Context , request resource.ImportStateRequest , response * resource.ImportStateResponse ) {
271
287
// Split the ID into handle and version
272
288
// The last hyphen separates handle and version
273
289
lastHyphenIndex := strings .LastIndex (request .ID , "-" )
@@ -306,7 +322,7 @@ func convertStateRequirementsToFrameworkRequirements(requirements types.Set) []d
306
322
return frameworkRequirements
307
323
}
308
324
309
- func buildCreateFrameworkRequest (state customFrameworkModel ) * datadogV2.CreateCustomFrameworkRequest {
325
+ func buildCreateFrameworkRequest (state complianceCustomFrameworkModel ) * datadogV2.CreateCustomFrameworkRequest {
310
326
createFrameworkRequest := datadogV2 .NewCreateCustomFrameworkRequestWithDefaults ()
311
327
description := state .Description .ValueString ()
312
328
iconURL := state .IconURL .ValueString ()
@@ -324,7 +340,7 @@ func buildCreateFrameworkRequest(state customFrameworkModel) *datadogV2.CreateCu
324
340
return createFrameworkRequest
325
341
}
326
342
327
- func buildUpdateFrameworkRequest (state customFrameworkModel ) * datadogV2.UpdateCustomFrameworkRequest {
343
+ func buildUpdateFrameworkRequest (state complianceCustomFrameworkModel ) * datadogV2.UpdateCustomFrameworkRequest {
328
344
updateFrameworkRequest := datadogV2 .NewUpdateCustomFrameworkRequestWithDefaults ()
329
345
description := state .Description .ValueString ()
330
346
iconURL := state .IconURL .ValueString ()
0 commit comments