Merge pull request #26351 from hashicorp/jbardin/dependencies

we no longer need EvalRefreshDependencies
This commit is contained in:
James Bardin 2020-09-24 09:33:59 -04:00 committed by GitHub
commit c2566bff7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 56 deletions

View File

@ -1582,7 +1582,7 @@ func TestRefresh_updateDependencies(t *testing.T) {
Status: states.ObjectReady, Status: states.ObjectReady,
AttrsJSON: []byte(`{"id":"foo"}`), AttrsJSON: []byte(`{"id":"foo"}`),
Dependencies: []addrs.ConfigResource{ Dependencies: []addrs.ConfigResource{
// Existing dependencies should not be removed during refresh // Existing dependencies should be removed when overridden by the config
{ {
Module: addrs.RootModule, Module: addrs.RootModule,
Resource: addrs.Resource{ Resource: addrs.Resource{
@ -1598,6 +1598,32 @@ func TestRefresh_updateDependencies(t *testing.T) {
Module: addrs.RootModule, Module: addrs.RootModule,
}, },
) )
root.SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "aws_instance",
Name: "baz",
}.Instance(addrs.NoKey),
&states.ResourceInstanceObjectSrc{
Status: states.ObjectReady,
AttrsJSON: []byte(`{"id":"baz"}`),
Dependencies: []addrs.ConfigResource{
// Existing dependencies should not be removed from orphaned instances
{
Module: addrs.RootModule,
Resource: addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "aws_instance",
Name: "bam",
},
},
},
},
addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("aws"),
Module: addrs.RootModule,
},
)
root.SetResourceInstanceCurrent( root.SetResourceInstanceCurrent(
addrs.Resource{ addrs.Resource{
Mode: addrs.ManagedResourceMode, Mode: addrs.ManagedResourceMode,
@ -1621,7 +1647,8 @@ resource "aws_instance" "bar" {
} }
resource "aws_instance" "foo" { resource "aws_instance" "foo" {
}`, }
`,
}) })
p := testProvider("aws") p := testProvider("aws")
@ -1649,12 +1676,15 @@ aws_instance.bar:
Dependencies: Dependencies:
aws_instance.foo aws_instance.foo
aws_instance.foo: aws_instance.baz:
ID = foo ID = baz
provider = provider["registry.terraform.io/hashicorp/aws"] provider = provider["registry.terraform.io/hashicorp/aws"]
Dependencies: Dependencies:
aws_instance.baz aws_instance.bam
aws_instance.foo:
ID = foo
provider = provider["registry.terraform.io/hashicorp/aws"]
`) `)
checkStateString(t, result, expect) checkStateString(t, result, expect)

View File

@ -3,7 +3,6 @@ package terraform
import ( import (
"fmt" "fmt"
"log" "log"
"sort"
"github.com/hashicorp/terraform/addrs" "github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/configs" "github.com/hashicorp/terraform/configs"
@ -530,49 +529,3 @@ func (n *EvalWriteResourceState) Eval(ctx EvalContext) (interface{}, error) {
return nil, nil return nil, nil
} }
// EvalRefreshDependencies is an EvalNode implementation that appends any newly
// found dependencies to those saved in the state. The existing dependencies
// are retained, as they may be missing from the config, and will be required
// for the updates and destroys during the next apply.
type EvalRefreshDependencies struct {
// Prior State
State **states.ResourceInstanceObject
// Dependencies to write to the new state
Dependencies *[]addrs.ConfigResource
}
func (n *EvalRefreshDependencies) Eval(ctx EvalContext) (interface{}, error) {
state := *n.State
if state == nil {
// no existing state to append
return nil, nil
}
// We already have dependencies in state, so we need to trust those for
// refresh. We can't write out new dependencies until apply time in case
// the configuration has been changed in a manner the conflicts with the
// stored dependencies.
if len(state.Dependencies) > 0 {
*n.Dependencies = state.Dependencies
return nil, nil
}
depMap := make(map[string]addrs.ConfigResource)
for _, d := range *n.Dependencies {
depMap[d.String()] = d
}
deps := make([]addrs.ConfigResource, 0, len(depMap))
for _, d := range depMap {
deps = append(deps, d)
}
sort.Slice(deps, func(i, j int) bool {
return deps[i].String() < deps[j].String()
})
*n.Dependencies = deps
return nil, nil
}

View File

@ -141,10 +141,6 @@ func (n *NodePlannableResourceInstance) evalTreeManagedResource(addr addrs.AbsRe
ProviderSchema: &providerSchema, ProviderSchema: &providerSchema,
Output: &instanceRefreshState, Output: &instanceRefreshState,
}, },
&EvalRefreshDependencies{
State: &instanceRefreshState,
Dependencies: &n.Dependencies,
},
&EvalRefresh{ &EvalRefresh{
Addr: addr.Resource, Addr: addr.Resource,
ProviderAddr: n.ResolvedProvider, ProviderAddr: n.ResolvedProvider,