From e2a3f78c0828ed7745fc9b445b870cc26b1f933f Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 7 Feb 2015 16:29:04 -0800 Subject: [PATCH] terraform: expand modules in our builder --- terraform/graph_builder.go | 7 +++++ terraform/graph_builder_test.go | 30 +++++++++++++++++++ .../graph-builder-modules/consul/main.tf | 2 ++ .../graph-builder-modules/main.tf | 16 ++++++++++ 4 files changed, 55 insertions(+) create mode 100644 terraform/test-fixtures/graph-builder-modules/consul/main.tf create mode 100644 terraform/test-fixtures/graph-builder-modules/main.tf diff --git a/terraform/graph_builder.go b/terraform/graph_builder.go index e946e152e..75e3afbe2 100644 --- a/terraform/graph_builder.go +++ b/terraform/graph_builder.go @@ -74,6 +74,13 @@ func (b *BuiltinGraphBuilder) Steps() []GraphTransformer { &MissingProviderTransformer{Providers: b.Providers}, &ProviderTransformer{}, &PruneProviderTransformer{}, + &VertexTransformer{ + Transforms: []GraphVertexTransformer{ + &ExpandTransform{ + Builder: b, + }, + }, + }, &RootTransformer{}, } } diff --git a/terraform/graph_builder_test.go b/terraform/graph_builder_test.go index a353fa0b3..f9da79e28 100644 --- a/terraform/graph_builder_test.go +++ b/terraform/graph_builder_test.go @@ -29,6 +29,24 @@ func TestBuiltinGraphBuilder(t *testing.T) { } } +// This test tests that the graph builder properly expands modules. +func TestBuiltinGraphBuilder_modules(t *testing.T) { + b := &BuiltinGraphBuilder{ + Root: testModule(t, "graph-builder-modules"), + } + + g, err := b.Build(RootModulePath) + if err != nil { + t.Fatalf("err: %s", err) + } + + actual := strings.TrimSpace(g.String()) + expected := strings.TrimSpace(testBuiltinGraphBuilderModuleStr) + if actual != expected { + t.Fatalf("bad: %s", actual) + } +} + const testBuiltinGraphBuilderBasicStr = ` aws_instance.db provider.aws @@ -37,3 +55,15 @@ aws_instance.web provider.aws provider.aws ` + +const testBuiltinGraphBuilderModuleStr = ` +aws_instance.web + aws_security_group.firewall + module.consul (expanded) + provider.aws +aws_security_group.firewall + provider.aws +module.consul (expanded) + aws_security_group.firewall +provider.aws +` diff --git a/terraform/test-fixtures/graph-builder-modules/consul/main.tf b/terraform/test-fixtures/graph-builder-modules/consul/main.tf new file mode 100644 index 000000000..8b0469b8c --- /dev/null +++ b/terraform/test-fixtures/graph-builder-modules/consul/main.tf @@ -0,0 +1,2 @@ +provider "aws" {} +resource "aws_instance" "server" {} diff --git a/terraform/test-fixtures/graph-builder-modules/main.tf b/terraform/test-fixtures/graph-builder-modules/main.tf new file mode 100644 index 000000000..8e8b532dd --- /dev/null +++ b/terraform/test-fixtures/graph-builder-modules/main.tf @@ -0,0 +1,16 @@ +module "consul" { + foo = "${aws_security_group.firewall.foo}" + source = "./consul" +} + +provider "aws" {} + +resource "aws_security_group" "firewall" {} + +resource "aws_instance" "web" { + security_groups = [ + "foo", + "${aws_security_group.firewall.foo}", + "${module.consul.security_group}" + ] +}