@@ -2,6 +2,7 @@ package fwprovider
2
2
3
3
import (
4
4
"context"
5
+ "strings"
5
6
6
7
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
7
8
"github.com/hashicorp/terraform-plugin-framework/attr"
@@ -29,13 +30,6 @@ type customFrameworkModel struct {
29
30
Requirements types.Set `tfsdk:"requirements"` // have to define requirements as a set to be unordered
30
31
}
31
32
32
- // the only way I could get the requirements to be unordered was to define them as a set in Terraform :(
33
- // if I could define requirements as a list, I could use the following:
34
- // type requirementModel struct {
35
- // Name types.String `tfsdk:"name"`
36
- // Controls types.Set `tfsdk:"controls"`
37
- // }
38
-
39
33
func NewCustomFrameworkResource () resource.Resource {
40
34
return & customFrameworkResource {}
41
35
}
@@ -122,6 +116,7 @@ func (r *customFrameworkResource) Create(ctx context.Context, request resource.C
122
116
}
123
117
124
118
_ , _ , err := r .Api .CreateCustomFramework (r .Auth , * buildCreateFrameworkRequest (state ))
119
+
125
120
if err != nil {
126
121
response .Diagnostics .Append (utils .FrameworkErrorDiag (err , "error creating custom framework" ))
127
122
return
@@ -158,12 +153,37 @@ func (r *customFrameworkResource) Read(ctx context.Context, request resource.Rea
158
153
response .Diagnostics .Append (utils .FrameworkErrorDiag (err , "error reading custom framework" ))
159
154
return
160
155
}
161
- state .ID = types .StringValue (data .GetData ().Attributes .Handle + string ('-' ) + data .GetData ().Attributes .Version )
156
+ databaseState := readStateFromDatabase (data , state .Handle .ValueString (), state .Version .ValueString ())
157
+ diags = response .State .Set (ctx , & databaseState )
158
+ response .Diagnostics .Append (diags ... )
159
+ }
160
+
161
+ func (r * customFrameworkResource ) Update (ctx context.Context , request resource.UpdateRequest , response * resource.UpdateResponse ) {
162
+ var state customFrameworkModel
163
+ diags := request .Config .Get (ctx , & state )
164
+ response .Diagnostics .Append (diags ... )
165
+ if response .Diagnostics .HasError () {
166
+ return
167
+ }
168
+
169
+ _ , _ , err := r .Api .UpdateCustomFramework (r .Auth , state .Handle .ValueString (), state .Version .ValueString (), * buildUpdateFrameworkRequest (state ))
170
+ if err != nil {
171
+ response .Diagnostics .Append (utils .FrameworkErrorDiag (err , "error updating custom framework" ))
172
+ return
173
+ }
174
+ diags = response .State .Set (ctx , & state )
175
+ response .Diagnostics .Append (diags ... )
176
+ }
177
+
178
+ func readStateFromDatabase (data datadogV2.RetrieveCustomFrameworkResponse , handle string , version string ) customFrameworkModel {
179
+ // Set the state
180
+ var state customFrameworkModel
181
+ state .ID = types .StringValue (handle + "-" + version )
182
+ state .Handle = types .StringValue (handle )
183
+ state .Version = types .StringValue (version )
184
+ state .Name = types .StringValue (data .GetData ().Attributes .Name )
162
185
state .Description = types .StringValue (data .GetData ().Attributes .Description )
163
186
state .IconURL = types .StringValue (data .GetData ().Attributes .IconUrl )
164
- state .Version = types .StringValue (data .GetData ().Attributes .Version )
165
- state .Handle = types .StringValue (data .GetData ().Attributes .Handle )
166
- state .Name = types .StringValue (data .GetData ().Attributes .Name )
167
187
168
188
// Convert requirements to set
169
189
requirements := make ([]attr.Value , len (data .GetData ().Attributes .Requirements ))
@@ -186,7 +206,6 @@ func (r *customFrameworkResource) Read(ctx context.Context, request resource.Rea
186
206
},
187
207
)
188
208
}
189
- // set requirement
190
209
requirements [i ] = types .ObjectValueMust (
191
210
map [string ]attr.Type {
192
211
"name" : types .StringType ,
@@ -204,7 +223,6 @@ func (r *customFrameworkResource) Read(ctx context.Context, request resource.Rea
204
223
},
205
224
)
206
225
}
207
- // set state requirements
208
226
state .Requirements = types .SetValueMust (
209
227
types.ObjectType {AttrTypes : map [string ]attr.Type {
210
228
"name" : types .StringType ,
@@ -215,25 +233,29 @@ func (r *customFrameworkResource) Read(ctx context.Context, request resource.Rea
215
233
}},
216
234
requirements ,
217
235
)
218
- diags = response .State .Set (ctx , & state )
219
- response .Diagnostics .Append (diags ... )
236
+ return state
220
237
}
221
238
222
- func (r * customFrameworkResource ) Update (ctx context.Context , request resource.UpdateRequest , response * resource.UpdateResponse ) {
223
- var state customFrameworkModel
224
- diags := request .Config .Get (ctx , & state )
225
- response .Diagnostics .Append (diags ... )
226
- if response .Diagnostics .HasError () {
239
+ // 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
240
+ func (r * customFrameworkResource ) ImportState (ctx context.Context , request resource.ImportStateRequest , response * resource.ImportStateResponse ) {
241
+ // Split the ID into handle and version
242
+ // The last hyphen separates handle and version
243
+ lastHyphenIndex := strings .LastIndex (request .ID , "-" )
244
+ if lastHyphenIndex == - 1 {
245
+ response .Diagnostics .AddError ("Invalid import ID" , "Import ID must contain a hyphen to separate handle and version" )
227
246
return
228
247
}
248
+ handle := request .ID [:lastHyphenIndex ]
249
+ version := request .ID [lastHyphenIndex + 1 :]
229
250
230
- _ , _ , err := r .Api .UpdateCustomFramework (r .Auth , state . Handle . ValueString (), state . Version . ValueString (), * buildUpdateFrameworkRequest ( state ) )
251
+ data , _ , err := r .Api .RetrieveCustomFramework (r .Auth , handle , version )
231
252
if err != nil {
232
- response .Diagnostics .Append ( utils . FrameworkErrorDiag ( err , "error updating custom framework" ))
253
+ response .Diagnostics .AddError ( "Error importing resource" , err . Error ( ))
233
254
return
234
255
}
235
- diags = response .State .Set (ctx , & state )
236
- response .Diagnostics .Append (diags ... )
256
+
257
+ state := readStateFromDatabase (data , handle , version )
258
+ response .Diagnostics .Append (response .State .Set (ctx , & state )... )
237
259
}
238
260
239
261
func convertStateRequirementsToFrameworkRequirements (requirements types.Set ) []datadogV2.CustomFrameworkRequirement {
0 commit comments