Skip to content

internal/fwserver: Prevent PlanResourceChange panic during changed path logging #797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/unreleased/BUG FIXES-20230711-161747.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: BUG FIXES
body: 'resource: Prevented panic during planning caused by `SetNestedAttribute` with
nested attribute `Default` and multiple configured elements'
time: 2023-07-11T16:17:47.990142-04:00
custom:
Issue: "783"
57 changes: 57 additions & 0 deletions internal/fwschemadata/data_get_at_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,40 @@ func TestDataGetAtPath(t *testing.T) {
expected any
expectedDiags diag.Diagnostics
}{
"invalid-path": {
data: fwschemadata.Data{
Schema: testschema.Schema{
Attributes: map[string]fwschema.Attribute{
"test": testschema.Attribute{
Optional: true,
Type: types.StringType,
},
},
},
TerraformValue: tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"test": tftypes.String,
},
},
map[string]tftypes.Value{
"test": tftypes.NewValue(tftypes.String, "test-value"),
},
),
},
path: path.Root("not-test"),
target: new(string),
expected: new(string),
expectedDiags: diag.Diagnostics{
diag.NewAttributeErrorDiagnostic(
path.Root("not-test"),
"Data Read Error",
"An unexpected error was encountered trying to retrieve type information at a given path. "+
"This is always an error in the provider. Please report the following to the provider developer:\n\n"+
"Error: AttributeName(\"not-test\") still remains in the path: could not find attribute or block \"not-test\" in schema",
),
},
},
"invalid-target": {
data: fwschemadata.Data{
Schema: testschema.Schema{
Expand Down Expand Up @@ -104,6 +138,29 @@ func TestDataGetAtPath(t *testing.T) {
),
},
},
"TerraformValue-null": {
data: fwschemadata.Data{
Schema: testschema.Schema{
Attributes: map[string]fwschema.Attribute{
"test": testschema.Attribute{
Optional: true,
Type: types.StringType,
},
},
},
TerraformValue: tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"test": tftypes.String,
},
},
nil,
),
},
path: path.Root("test"),
target: new(types.String),
expected: pointer(types.StringNull()),
},
"AttrTypeWithValidateError": {
data: fwschemadata.Data{
Schema: testschema.Schema{
Expand Down
8 changes: 8 additions & 0 deletions internal/fwserver/server_planresourcechange.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,14 @@ func (s *Server) PlanResourceChange(ctx context.Context, req *PlanResourceChange
_ = resp.PlannedState.GetAttribute(ctx, p, &plannedState)
_ = req.PriorState.GetAttribute(ctx, p, &priorState)

// Due to ignoring diagnostics, the value may not be populated.
// Prevent the panic and show the path as changed.
if plannedState == nil {
changedPaths.Append(p)

continue
}

if plannedState.Equal(priorState) {
continue
}
Expand Down
Loading