Skip to content

Commit

Permalink
Merge pull request #968 from Juniper/967-nil-pointer-derek-when-incre…
Browse files Browse the repository at this point in the history
…mental-config-not-available

Fix nil pointer deref when `GetNodeRenderedConfigDiff()` returns 404
  • Loading branch information
chrismarget-j authored Nov 6, 2024
2 parents cef801c + db15243 commit 29f741a
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions apstra/data_source_blueprint_device_rendered_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/Juniper/terraform-provider-apstra/apstra/utils"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)

var (
Expand Down Expand Up @@ -48,64 +49,66 @@ func (o *dataSourceBlueprintNodeConfig) Read(ctx context.Context, req datasource

bpId := apstra.ObjectId(config.BlueprintId.ValueString())

var deployed, staged string
var diff *apstra.RenderDiff
var err error
var deployed, staged, incremental string
var ace apstra.ClientErr

switch {
case !config.NodeId.IsNull():
node := apstra.ObjectId(config.NodeId.ValueString())
deployed, err = o.client.GetNodeRenderedConfig(ctx, bpId, node, enum.RenderedConfigTypeDeployed)
deployed, err = o.client.GetNodeRenderedConfig(ctx, bpId, apstra.ObjectId(config.NodeId.ValueString()), enum.RenderedConfigTypeDeployed)
if err != nil {
if !errors.As(err, &ace) || ace.Type() != apstra.ErrNotfound {
resp.Diagnostics.AddError(fmt.Sprintf("failed to fetch deployed configuration for node %s", config.NodeId), err.Error())
return
}
}
staged, err = o.client.GetNodeRenderedConfig(ctx, bpId, node, enum.RenderedConfigTypeStaging)
staged, err = o.client.GetNodeRenderedConfig(ctx, bpId, apstra.ObjectId(config.NodeId.ValueString()), enum.RenderedConfigTypeStaging)
if err != nil {
if !errors.As(err, &ace) || ace.Type() != apstra.ErrNotfound {
resp.Diagnostics.AddError(fmt.Sprintf("failed to fetch staged configuration for node %s", config.NodeId), err.Error())
return
}
}
diff, err := o.client.GetNodeRenderedConfigDiff(ctx, bpId, node)
diff, err = o.client.GetNodeRenderedConfigDiff(ctx, bpId, apstra.ObjectId(config.NodeId.ValueString()))
if err != nil {
if !errors.As(err, &ace) || ace.Type() != apstra.ErrNotfound {
resp.Diagnostics.AddError(fmt.Sprintf("failed to fetch incremental configuration for node %s", config.NodeId), err.Error())
return
}
}
incremental = diff.Config
case !config.SystemId.IsNull():
system := apstra.ObjectId(config.SystemId.ValueString())
deployed, err = o.client.GetSystemRenderedConfig(ctx, bpId, system, enum.RenderedConfigTypeDeployed)
deployed, err = o.client.GetSystemRenderedConfig(ctx, bpId, apstra.ObjectId(config.SystemId.ValueString()), enum.RenderedConfigTypeDeployed)
if err != nil {
if !errors.As(err, &ace) || ace.Type() != apstra.ErrNotfound {
resp.Diagnostics.AddError(fmt.Sprintf("failed to fetch deployed configuration for system %s", config.SystemId), err.Error())
return
}
}
staged, err = o.client.GetSystemRenderedConfig(ctx, bpId, system, enum.RenderedConfigTypeStaging)
staged, err = o.client.GetSystemRenderedConfig(ctx, bpId, apstra.ObjectId(config.SystemId.ValueString()), enum.RenderedConfigTypeStaging)
if err != nil {
if !errors.As(err, &ace) || ace.Type() != apstra.ErrNotfound {
resp.Diagnostics.AddError(fmt.Sprintf("failed to fetch staged configuration for system %s", config.SystemId), err.Error())
return
}
}
diff, err := o.client.GetSystemRenderedConfigDiff(ctx, bpId, system)
diff, err = o.client.GetSystemRenderedConfigDiff(ctx, bpId, apstra.ObjectId(config.SystemId.ValueString()))
if err != nil {
if !errors.As(err, &ace) || ace.Type() != apstra.ErrNotfound {
resp.Diagnostics.AddError(fmt.Sprintf("failed to fetch incremental configuration for system %s", config.SystemId), err.Error())
return
}
}
incremental = diff.Config
}

if diff != nil {
config.Incremental = utils.StringValueOrNull(ctx, diff.Config, &resp.Diagnostics)
} else {
config.Incremental = types.StringNull()
}

config.DeployedCfg = utils.StringValueOrNull(ctx, deployed, &resp.Diagnostics)
config.StagedCfg = utils.StringValueOrNull(ctx, staged, &resp.Diagnostics)
config.Incremental = utils.StringValueOrNull(ctx, incremental, &resp.Diagnostics)

// set state
resp.Diagnostics.Append(resp.State.Set(ctx, &config)...)
Expand Down

0 comments on commit 29f741a

Please sign in to comment.