Skip to content

Commit

Permalink
backport of commit 1cc6241
Browse files Browse the repository at this point in the history
  • Loading branch information
liamcervante committed Jun 2, 2023
1 parent 0caabdf commit b618fe2
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 7 deletions.
86 changes: 86 additions & 0 deletions internal/terraform/graph_builder_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/plans"
"github.com/hashicorp/terraform/internal/providers"
"github.com/hashicorp/terraform/internal/states"
)

Expand Down Expand Up @@ -702,6 +703,91 @@ func TestApplyGraphBuilder_orphanedWithProvider(t *testing.T) {
testGraphNotContains(t, g, "provider.test")
}

func TestApplyGraphBuilder_withChecks(t *testing.T) {
awsProvider := mockProviderWithResourceTypeSchema("aws_instance", simpleTestSchema())

changes := &plans.Changes{
Resources: []*plans.ResourceInstanceChangeSrc{
{
Addr: mustResourceInstanceAddr("aws_instance.foo"),
ChangeSrc: plans.ChangeSrc{
Action: plans.Create,
},
},
{
Addr: mustResourceInstanceAddr("aws_instance.baz"),
ChangeSrc: plans.ChangeSrc{
Action: plans.Create,
},
},
{
Addr: mustResourceInstanceAddr("data.aws_data_source.bar"),
ChangeSrc: plans.ChangeSrc{
Action: plans.Read,
},
ActionReason: plans.ResourceInstanceReadBecauseCheckNested,
},
},
}

plugins := newContextPlugins(map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("aws"): providers.FactoryFixed(awsProvider),
}, nil)

b := &ApplyGraphBuilder{
Config: testModule(t, "apply-with-checks"),
Changes: changes,
Plugins: plugins,
State: states.NewState(),
Operation: walkApply,
}

g, err := b.Build(addrs.RootModuleInstance)
if err != nil {
t.Fatalf("err: %s", err)
}

if g.Path.String() != addrs.RootModuleInstance.String() {
t.Fatalf("wrong path %q", g.Path.String())
}

got := strings.TrimSpace(g.String())
// We're especially looking for the edge here, where aws_instance.bat
// has a dependency on aws_instance.boo
want := strings.TrimSpace(testPlanWithCheckGraphBuilderStr)
if diff := cmp.Diff(want, got); diff != "" {
t.Fatalf("\ngot:\n%s\n\nwant:\n%s\n\ndiff:\n%s", got, want, diff)
}

}

const testPlanWithCheckGraphBuilderStr = `
(execute checks)
aws_instance.baz
aws_instance.baz
aws_instance.baz (expand)
aws_instance.foo
aws_instance.baz (expand)
provider["registry.terraform.io/hashicorp/aws"]
aws_instance.foo
aws_instance.foo (expand)
aws_instance.foo (expand)
provider["registry.terraform.io/hashicorp/aws"]
check.my_check (expand)
data.aws_data_source.bar
data.aws_data_source.bar
(execute checks)
data.aws_data_source.bar (expand)
data.aws_data_source.bar (expand)
provider["registry.terraform.io/hashicorp/aws"]
provider["registry.terraform.io/hashicorp/aws"]
provider["registry.terraform.io/hashicorp/aws"] (close)
data.aws_data_source.bar
root
check.my_check (expand)
provider["registry.terraform.io/hashicorp/aws"] (close)
`

const testApplyGraphBuilderStr = `
module.child (close)
module.child.test_object.other
Expand Down
4 changes: 0 additions & 4 deletions internal/terraform/graph_builder_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,6 @@ func (b *PlanGraphBuilder) Steps() []GraphTransformer {

&AttachDependenciesTransformer{},

// Nested data blocks should be loaded after every other resource has
// done its thing.
&checkStartTransformer{Config: b.Config, Operation: b.Operation},

// Make sure data sources are aware of any depends_on from the
// configuration
&attachDataResourceDependsOnTransformer{},
Expand Down
20 changes: 20 additions & 0 deletions internal/terraform/testdata/apply-with-checks/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

resource "aws_instance" "foo" {
test_string = "Hello, world!"
}

resource "aws_instance" "baz" {
test_string = aws_instance.foo.test_string
}

check "my_check" {
data "aws_data_source" "bar" {
id = "UI098L"
}

assert {
condition = data.aws_data_source.bar.foo == "valid value"
error_message = "invalid value"
}

}
6 changes: 3 additions & 3 deletions internal/terraform/transform_check_starter.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ func (s *checkStartTransformer) Transform(graph *Graph) error {
// We're going to step through all the vertices and pull out the relevant
// resources and data sources.
for _, vertex := range graph.Vertices() {
if node, isResource := vertex.(GraphNodeConfigResource); isResource {
if node, isResource := vertex.(*NodeApplyableResourceInstance); isResource {
addr := node.ResourceAddr()

if addr.Resource.Mode != addrs.ManagedResourceMode {
if addr.Resource.Mode == addrs.ManagedResourceMode {
// This is a resource, so we want to make sure it executes
// before any nested data sources.

Expand All @@ -50,7 +50,7 @@ func (s *checkStartTransformer) Transform(graph *Graph) error {

leafResource := true
for _, other := range graph.UpEdges(vertex) {
if otherResource, isResource := other.(GraphNodeConfigResource); isResource {
if otherResource, isResource := other.(*NodeApplyableResourceInstance); isResource {
otherAddr := otherResource.ResourceAddr()
if otherAddr.Resource.Mode == addrs.ManagedResourceMode {
// Then this resource is referencing another one
Expand Down

0 comments on commit b618fe2

Please sign in to comment.