From fa16db5ea9a7532f9f5b3f462482aff9c46123ff Mon Sep 17 00:00:00 2001 From: Chris Marget Date: Mon, 20 Nov 2023 16:50:08 -0500 Subject: [PATCH] change get/set deploy mode to return error rather than add diags --- .../resource_datacenter_device_allocation.go | 39 +++++++++++++++---- apstra/utils/node.go | 22 +++++------ 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/apstra/resource_datacenter_device_allocation.go b/apstra/resource_datacenter_device_allocation.go index cd8005e4..972eb469 100644 --- a/apstra/resource_datacenter_device_allocation.go +++ b/apstra/resource_datacenter_device_allocation.go @@ -2,6 +2,7 @@ package tfapstra import ( "context" + "errors" "fmt" "github.com/Juniper/apstra-go-sdk/apstra" "github.com/Juniper/terraform-provider-apstra/apstra/blueprint" @@ -95,9 +96,18 @@ func (o *resourceDeviceAllocation) Create(ctx context.Context, req resource.Crea switch { case plan.DeployMode.IsNull(): // not expected with Optional+Computed, nothing to do here case plan.DeployMode.IsUnknown(): // config is null, get the Computed value - plan.DeployMode = types.StringValue(utils.GetNodeDeployMode(ctx, bp, plan.NodeId.ValueString(), &resp.Diagnostics)) + deployMode, err := utils.GetNodeDeployMode(ctx, bp, plan.NodeId.ValueString()) + if err != nil { + resp.Diagnostics.AddError("failed to fetch node deploy mode", err.Error()) + return + } + plan.DeployMode = types.StringValue(deployMode) default: // value provided via config - utils.SetNodeDeployMode(ctx, bp, plan.NodeId.ValueString(), plan.DeployMode.ValueString(), &resp.Diagnostics) + err = utils.SetNodeDeployMode(ctx, bp, plan.NodeId.ValueString(), plan.DeployMode.ValueString()) + if err != nil { + resp.Diagnostics.AddError("failed while setting node deploy mode", err.Error()) + return + } } if resp.Diagnostics.HasError() { return @@ -156,10 +166,15 @@ func (o *resourceDeviceAllocation) Read(ctx context.Context, req resource.ReadRe return } - state.DeployMode = types.StringValue(utils.GetNodeDeployMode(ctx, bp, state.NodeId.ValueString(), &resp.Diagnostics)) - if resp.Diagnostics.HasError() { - return + deployMode, err := utils.GetNodeDeployMode(ctx, bp, state.NodeId.ValueString()) + if err != nil { + var ace apstra.ClientErr + if errors.As(err, &ace) && ace.Type() == apstra.ErrNotfound { + resp.State.RemoveResource(ctx) + return + } } + state.DeployMode = types.StringValue(deployMode) state.GetInterfaceMapName(ctx, o.client, &resp.Diagnostics) if resp.Diagnostics.HasError() { @@ -228,9 +243,19 @@ func (o *resourceDeviceAllocation) Update(ctx context.Context, req resource.Upda switch { case plan.DeployMode.IsNull(): // not expected with Optional+Computed, nothing to do here case plan.DeployMode.IsUnknown(): // config is null, get the Computed value - plan.DeployMode = types.StringValue(utils.GetNodeDeployMode(ctx, bp, plan.NodeId.ValueString(), &resp.Diagnostics)) + deployMode, err := utils.GetNodeDeployMode(ctx, bp, plan.NodeId.ValueString()) + if err != nil { + resp.Diagnostics.AddError("failed reading node deploy mode", err.Error()) + return + } + + plan.DeployMode = types.StringValue(deployMode) default: // value provided via config - utils.SetNodeDeployMode(ctx, bp, plan.NodeId.ValueString(), plan.DeployMode.ValueString(), &resp.Diagnostics) + err := utils.SetNodeDeployMode(ctx, bp, plan.NodeId.ValueString(), plan.DeployMode.ValueString()) + if err != nil { + resp.Diagnostics.AddError("failed setting node deploy mode", err.Error()) + return + } } if resp.Diagnostics.HasError() { return diff --git a/apstra/utils/node.go b/apstra/utils/node.go index aabe115e..11ccab2c 100644 --- a/apstra/utils/node.go +++ b/apstra/utils/node.go @@ -2,9 +2,7 @@ package utils import ( "context" - "fmt" "github.com/Juniper/apstra-go-sdk/apstra" - "github.com/hashicorp/terraform-plugin-framework/diag" ) func AllNodeDeployModes() []string { @@ -17,7 +15,7 @@ func AllNodeDeployModes() []string { return result } -func GetNodeDeployMode(ctx context.Context, client *apstra.TwoStageL3ClosClient, nodeId string, diags *diag.Diagnostics) string { +func GetNodeDeployMode(ctx context.Context, client *apstra.TwoStageL3ClosClient, nodeId string) (string, error) { var node struct { Id string `json:"id"` Type string `json:"type"` @@ -25,26 +23,23 @@ func GetNodeDeployMode(ctx context.Context, client *apstra.TwoStageL3ClosClient, } err := client.Client().GetNode(ctx, client.Id(), apstra.ObjectId(nodeId), &node) if err != nil { - diags.AddError("failed to fetch blueprint node", err.Error()) - return "" + return "", err } var deployMode apstra.NodeDeployMode err = deployMode.FromString(node.DeployMode) if err != nil { - diags.AddError(fmt.Sprintf("error parsing deploy mode %q", node.DeployMode), err.Error()) - return "" + return "", err } - return StringersToFriendlyString(deployMode) + return StringersToFriendlyString(deployMode), nil } -func SetNodeDeployMode(ctx context.Context, client *apstra.TwoStageL3ClosClient, nodeId string, modeString string, diags *diag.Diagnostics) { +func SetNodeDeployMode(ctx context.Context, client *apstra.TwoStageL3ClosClient, nodeId string, modeString string) error { var modeIota apstra.NodeDeployMode err := ApiStringerFromFriendlyString(&modeIota, modeString) if err != nil { - diags.AddError(fmt.Sprintf("error parsing deploy mode %q", modeString), err.Error()) - return + return err } type patch struct { @@ -67,7 +62,8 @@ func SetNodeDeployMode(ctx context.Context, client *apstra.TwoStageL3ClosClient, err = client.Client().PatchNode(ctx, client.Id(), apstra.ObjectId(nodeId), &setDeployMode, nil) if err != nil { - diags.AddError("error setting deploy mode", err.Error()) - return + return err } + + return nil }