Skip to content
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

Backport of account for noop deposed instances in json plan into v1.0 #28923

Merged
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
7 changes: 5 additions & 2 deletions internal/command/jsonplan/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/configs/configschema"
"github.com/hashicorp/terraform/internal/plans"
"github.com/hashicorp/terraform/internal/states"
"github.com/hashicorp/terraform/internal/terraform"
)

Expand Down Expand Up @@ -93,8 +94,10 @@ func marshalPlannedValues(changes *plans.Changes, schemas *terraform.Schemas) (m
seenModules := make(map[string]bool)

for _, resource := range changes.Resources {
// if the resource is being deleted, skip over it.
if resource.Action != plans.Delete {
// If the resource is being deleted, skip over it.
// Deposed instances are always conceptually a destroy, but if they
// were gone during refresh then the change becomes a noop.
if resource.Action != plans.Delete && resource.DeposedKey == states.NotDeposed {
containingModule := resource.Addr.Module.String()
moduleResourceMap[containingModule] = append(moduleResourceMap[containingModule], resource.Addr)

Expand Down
48 changes: 47 additions & 1 deletion internal/command/jsonplan/values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,26 @@ func TestMarshalPlanResources(t *testing.T) {
}},
Err: false,
},
"delete": {
"delete with null and nil": {
Action: plans.Delete,
Before: cty.NullVal(cty.EmptyObject),
After: cty.NilVal,
Want: nil,
Err: false,
},
"delete": {
Action: plans.Delete,
Before: cty.ObjectVal(map[string]cty.Value{
"woozles": cty.StringVal("foo"),
"foozles": cty.StringVal("bar"),
}),
After: cty.NullVal(cty.Object(map[string]cty.Type{
"woozles": cty.String,
"foozles": cty.String,
})),
Want: nil,
Err: false,
},
"update without unknowns": {
Action: plans.Update,
Before: cty.ObjectVal(map[string]cty.Value{
Expand Down Expand Up @@ -291,6 +304,39 @@ func TestMarshalPlanResources(t *testing.T) {
}
}

func TestMarshalPlanValuesNoopDeposed(t *testing.T) {
dynamicNull, err := plans.NewDynamicValue(cty.NullVal(cty.DynamicPseudoType), cty.DynamicPseudoType)
if err != nil {
t.Fatal(err)
}
testChange := &plans.Changes{
Resources: []*plans.ResourceInstanceChangeSrc{
{
Addr: addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_thing",
Name: "example",
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
DeposedKey: "12345678",
ProviderAddr: addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
ChangeSrc: plans.ChangeSrc{
Action: plans.NoOp,
Before: dynamicNull,
After: dynamicNull,
},
},
},
}

_, err = marshalPlannedValues(testChange, testSchemas())
if err != nil {
t.Fatal(err)
}
}

func testSchemas() *terraform.Schemas {
return &terraform.Schemas{
Providers: map[addrs.Provider]*terraform.ProviderSchema{
Expand Down