Skip to content

Commit

Permalink
change get/set deploy mode to return error rather than add diags
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismarget-j committed Nov 20, 2023
1 parent 774a6ba commit fa16db5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
39 changes: 32 additions & 7 deletions apstra/resource_datacenter_device_allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand Down
22 changes: 9 additions & 13 deletions apstra/utils/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -17,34 +15,31 @@ 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"`
DeployMode string `json:"deploy_mode"`
}
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 {
Expand All @@ -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
}

0 comments on commit fa16db5

Please sign in to comment.