@@ -35,6 +35,7 @@ import (
35
35
"github.com/hashicorp/terraform-plugin-framework/types"
36
36
"github.com/hashicorp/terraform-plugin-log/tflog"
37
37
"github.com/netascode/go-fmc"
38
+ "github.com/tidwall/sjson"
38
39
)
39
40
40
41
// End of section. //template:end imports
@@ -192,7 +193,7 @@ func (r *DeviceClusterResource) Create(ctx context.Context, req resource.CreateR
192
193
}
193
194
194
195
taskID := res .Get ("metadata.task.id" ).String ()
195
- tflog .Debug (ctx , fmt .Sprintf ("%s: Async task initiated successfully" , taskID ))
196
+ tflog .Debug (ctx , fmt .Sprintf ("%s: Async task initiated successfully (id: %s)" , plan . Id . ValueString () , taskID ))
196
197
197
198
diags = helpers .FMCWaitForJobToFinish (ctx , r .client , taskID , reqMods ... )
198
199
if resp .Diagnostics .Append (diags ... ); resp .Diagnostics .HasError () {
@@ -273,8 +274,6 @@ func (r *DeviceClusterResource) Read(ctx context.Context, req resource.ReadReque
273
274
274
275
// End of section. //template:end read
275
276
276
- // Section below is generated&owned by "gen/generator.go". //template:begin update
277
-
278
277
func (r * DeviceClusterResource ) Update (ctx context.Context , req resource.UpdateRequest , resp * resource.UpdateResponse ) {
279
278
var plan , state DeviceCluster
280
279
@@ -298,11 +297,87 @@ func (r *DeviceClusterResource) Update(ctx context.Context, req resource.UpdateR
298
297
299
298
tflog .Debug (ctx , fmt .Sprintf ("%s: Beginning Update" , plan .Id .ValueString ()))
300
299
301
- body := plan .toBody (ctx , state )
302
- res , err := r .client .Put (plan .getPath ()+ "/" + url .QueryEscape (plan .Id .ValueString ()), body , reqMods ... )
303
- if err != nil {
304
- resp .Diagnostics .AddError ("Client Error" , fmt .Sprintf ("Failed to configure object (PUT), got error: %s, %s" , err , res .String ()))
305
- return
300
+ // Check if name has changed
301
+ if plan .Name .ValueString () != state .Name .ValueString () {
302
+ tflog .Debug (ctx , fmt .Sprintf ("%s: Name has changed" , plan .Id .ValueString ()))
303
+ body := plan .toBody (ctx , DeviceCluster {})
304
+ body , _ = sjson .Set (body , "action" , "UPDATE_CLUSTER_NAME" )
305
+ res , err := r .client .Put (plan .getPath ()+ "/" + url .QueryEscape (plan .Id .ValueString ()), body , reqMods ... )
306
+ if err != nil {
307
+ resp .Diagnostics .AddError ("Client Error" , fmt .Sprintf ("Failed to configure object (PUT), got error: %s, %s" , err , res .String ()))
308
+ return
309
+ }
310
+ }
311
+
312
+ // Get list of state data devices
313
+ stateDevices := make ([]string , len (state .DataDevices ))
314
+ for i , v := range state .DataDevices {
315
+ stateDevices [i ] = v .DataNodeDeviceId .ValueString ()
316
+ }
317
+
318
+ // Get list of plan data devices
319
+ planDevices := make ([]string , len (plan .DataDevices ))
320
+ for i , v := range plan .DataDevices {
321
+ planDevices [i ] = v .DataNodeDeviceId .ValueString ()
322
+ }
323
+
324
+ // Check if any device needs to be removed from cluster
325
+ toBeRemoved := plan
326
+ toBeRemoved .DataDevices = []DeviceClusterDataDevices {}
327
+ for _ , v := range state .DataDevices {
328
+ if ! helpers .Contains (planDevices , v .DataNodeDeviceId .ValueString ()) {
329
+ toBeRemoved .DataDevices = append (toBeRemoved .DataDevices , v )
330
+ }
331
+ }
332
+
333
+ if len (toBeRemoved .DataDevices ) > 0 {
334
+ tflog .Debug (ctx , fmt .Sprintf ("%s: Data devices to be removed from cluster: %v" , plan .Id .ValueString (), toBeRemoved .DataDevices ))
335
+ body := toBeRemoved .toBody (ctx , DeviceCluster {})
336
+ body , _ = sjson .Set (body , "action" , "REMOVE_NODES" )
337
+ res , err := r .client .Put (plan .getPath ()+ "/" + url .QueryEscape (plan .Id .ValueString ()), body , reqMods ... )
338
+ if err != nil {
339
+ resp .Diagnostics .AddError ("Client Error" , fmt .Sprintf ("Failed to configure object (PUT), got error: %s, %s" , err , res .String ()))
340
+ return
341
+ }
342
+
343
+ // Adding code to poll object
344
+ taskID := res .Get ("metadata.task.id" ).String ()
345
+ tflog .Debug (ctx , fmt .Sprintf ("%s: Async task initiated successfully (id: %s)" , plan .Id .ValueString (), taskID ))
346
+
347
+ diags = helpers .FMCWaitForJobToFinish (ctx , r .client , taskID , reqMods ... )
348
+ if resp .Diagnostics .Append (diags ... ); resp .Diagnostics .HasError () {
349
+ return
350
+ }
351
+
352
+ }
353
+
354
+ // Check if any device needs to be added to cluster
355
+ toBeAdded := plan
356
+ toBeAdded .DataDevices = []DeviceClusterDataDevices {}
357
+ for _ , v := range plan .DataDevices {
358
+ if ! helpers .Contains (stateDevices , v .DataNodeDeviceId .ValueString ()) {
359
+ toBeAdded .DataDevices = append (toBeAdded .DataDevices , v )
360
+ }
361
+ }
362
+
363
+ if len (toBeAdded .DataDevices ) > 0 {
364
+ tflog .Debug (ctx , fmt .Sprintf ("%s: Data devices to be added to cluster: %v" , plan .Id .ValueString (), toBeAdded .DataDevices ))
365
+ body := toBeAdded .toBody (ctx , DeviceCluster {})
366
+ body , _ = sjson .Set (body , "action" , "ADD_NODES" )
367
+ res , err := r .client .Put (plan .getPath ()+ "/" + url .QueryEscape (plan .Id .ValueString ()), body , reqMods ... )
368
+ if err != nil {
369
+ resp .Diagnostics .AddError ("Client Error" , fmt .Sprintf ("Failed to configure object (PUT), got error: %s, %s" , err , res .String ()))
370
+ return
371
+ }
372
+
373
+ // Adding code to poll object
374
+ taskID := res .Get ("metadata.task.id" ).String ()
375
+ tflog .Debug (ctx , fmt .Sprintf ("%s: Async task initiated successfully (id: %s)" , plan .Id .ValueString (), taskID ))
376
+
377
+ diags = helpers .FMCWaitForJobToFinish (ctx , r .client , taskID , reqMods ... )
378
+ if resp .Diagnostics .Append (diags ... ); resp .Diagnostics .HasError () {
379
+ return
380
+ }
306
381
}
307
382
308
383
tflog .Debug (ctx , fmt .Sprintf ("%s: Update finished successfully" , plan .Id .ValueString ()))
@@ -311,8 +386,6 @@ func (r *DeviceClusterResource) Update(ctx context.Context, req resource.UpdateR
311
386
resp .Diagnostics .Append (diags ... )
312
387
}
313
388
314
- // End of section. //template:end update
315
-
316
389
func (r * DeviceClusterResource ) Delete (ctx context.Context , req resource.DeleteRequest , resp * resource.DeleteResponse ) {
317
390
var state DeviceCluster
318
391
0 commit comments