Skip to content

Commit ab80c58

Browse files
committed
cluster add/remove data device
1 parent de12af3 commit ab80c58

File tree

3 files changed

+86
-13
lines changed

3 files changed

+86
-13
lines changed

internal/provider/resource_fmc_device.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func (r *DeviceResource) Create(ctx context.Context, req resource.CreateRequest,
206206
}
207207

208208
taskID := res.Get("metadata.task.id").String()
209-
tflog.Debug(ctx, fmt.Sprintf("%s: Async task initiated successfully", taskID))
209+
tflog.Debug(ctx, fmt.Sprintf("%s: Async task initiated successfully (id: %s)", plan.Id.ValueString(), taskID))
210210

211211
diags = helpers.FMCWaitForJobToFinish(ctx, r.client, taskID, reqMods...)
212212
if resp.Diagnostics.Append(diags...); resp.Diagnostics.HasError() {

internal/provider/resource_fmc_device_cluster.go

+83-10
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/hashicorp/terraform-plugin-framework/types"
3636
"github.com/hashicorp/terraform-plugin-log/tflog"
3737
"github.com/netascode/go-fmc"
38+
"github.com/tidwall/sjson"
3839
)
3940

4041
// End of section. //template:end imports
@@ -192,7 +193,7 @@ func (r *DeviceClusterResource) Create(ctx context.Context, req resource.CreateR
192193
}
193194

194195
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))
196197

197198
diags = helpers.FMCWaitForJobToFinish(ctx, r.client, taskID, reqMods...)
198199
if resp.Diagnostics.Append(diags...); resp.Diagnostics.HasError() {
@@ -273,8 +274,6 @@ func (r *DeviceClusterResource) Read(ctx context.Context, req resource.ReadReque
273274

274275
// End of section. //template:end read
275276

276-
// Section below is generated&owned by "gen/generator.go". //template:begin update
277-
278277
func (r *DeviceClusterResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
279278
var plan, state DeviceCluster
280279

@@ -298,11 +297,87 @@ func (r *DeviceClusterResource) Update(ctx context.Context, req resource.UpdateR
298297

299298
tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Update", plan.Id.ValueString()))
300299

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+
}
306381
}
307382

308383
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
311386
resp.Diagnostics.Append(diags...)
312387
}
313388

314-
// End of section. //template:end update
315-
316389
func (r *DeviceClusterResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
317390
var state DeviceCluster
318391

internal/provider/resource_fmc_device_ha_pair.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ func (r *DeviceHAPairResource) Create(ctx context.Context, req resource.CreateRe
389389

390390
// Adding code to poll object
391391
taskID := res.Get("metadata.task.id").String()
392-
tflog.Debug(ctx, fmt.Sprintf("%s: Async task initiated successfully", taskID))
392+
tflog.Debug(ctx, fmt.Sprintf("%s: Async task initiated successfully (id: %s)", plan.Id.ValueString(), taskID))
393393

394394
diags = helpers.FMCWaitForJobToFinish(ctx, r.client, taskID, reqMods...)
395395
if resp.Diagnostics.Append(diags...); resp.Diagnostics.HasError() {
@@ -555,7 +555,7 @@ func (r *DeviceHAPairResource) Delete(ctx context.Context, req resource.DeleteRe
555555

556556
// Adding code to poll object
557557
taskID := res.Get("metadata.task.id").String()
558-
tflog.Debug(ctx, fmt.Sprintf("%s: Async task initiated successfully", taskID))
558+
tflog.Debug(ctx, fmt.Sprintf("%s: Async task initiated successfully (id: %s)", state.Id.ValueString(), taskID))
559559

560560
diags = helpers.FMCWaitForJobToFinish(ctx, r.client, taskID, reqMods...)
561561
if resp.Diagnostics.Append(diags...); resp.Diagnostics.HasError() {

0 commit comments

Comments
 (0)