From 0699cde1d47ac4ba0ae33c3d9f026c5af9faf2c3 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 22 Jul 2014 17:10:17 -0700 Subject: [PATCH] config: depends_on meta-parameter --- config/config.go | 1 + config/loader_libucl.go | 28 +++++++++++++++++++--------- config/loader_test.go | 9 +++++++++ config/test-fixtures/basic.tf | 2 ++ config/test-fixtures/basic.tf.json | 3 ++- 5 files changed, 33 insertions(+), 10 deletions(-) diff --git a/config/config.go b/config/config.go index cab5f728a..f2c8b41b5 100644 --- a/config/config.go +++ b/config/config.go @@ -42,6 +42,7 @@ type Resource struct { Count int RawConfig *RawConfig Provisioners []*Provisioner + DependsOn []string } // Provisioner is a configured provisioner step on a resource. diff --git a/config/loader_libucl.go b/config/loader_libucl.go index cd14774fd..2599a6d16 100644 --- a/config/loader_libucl.go +++ b/config/loader_libucl.go @@ -352,16 +352,11 @@ func loadResourcesLibucl(o *libucl.Object) ([]*Resource, error) { err) } - // Remove the "count" from the config, since we treat that special - delete(config, "count") - - // Delete the "provisioner" section from the config since - // that is treated specially. - delete(config, "provisioner") - - // Delete the "connection" section since we handle that - // seperately + // Remove the fields we handle specially delete(config, "connection") + delete(config, "count") + delete(config, "depends_on") + delete(config, "provisioner") rawConfig, err := NewRawConfig(config) if err != nil { @@ -401,6 +396,20 @@ func loadResourcesLibucl(o *libucl.Object) ([]*Resource, error) { } } + // If we have depends fields, then add those in + var dependsOn []string + if deps := r.Get("depends_on"); deps != nil { + err := deps.Decode(&dependsOn) + deps.Close() + if err != nil { + return nil, fmt.Errorf( + "Error reading depends_on for %s[%s]: %s", + t.Key(), + r.Key(), + err) + } + } + // If we have provisioners, then parse those out var provisioners []*Provisioner if po := r.Get("provisioner"); po != nil { @@ -422,6 +431,7 @@ func loadResourcesLibucl(o *libucl.Object) ([]*Resource, error) { Count: count, RawConfig: rawConfig, Provisioners: provisioners, + DependsOn: dependsOn, }) } } diff --git a/config/loader_test.go b/config/loader_test.go index 0cb8183cf..168f4e931 100644 --- a/config/loader_test.go +++ b/config/loader_test.go @@ -393,6 +393,13 @@ func resourcesStr(rs []*Resource) string { } } + if len(r.DependsOn) > 0 { + result += fmt.Sprintf(" dependsOn\n") + for _, d := range r.DependsOn { + result += fmt.Sprintf(" %s\n", d) + } + } + if len(r.RawConfig.Variables) > 0 { result += fmt.Sprintf(" vars\n") @@ -479,6 +486,8 @@ do const basicResourcesStr = ` aws_instance[db] (x1) security_groups + dependsOn + aws_instance.web vars resource: aws_security_group.firewall.*.id aws_instance[web] (x1) diff --git a/config/test-fixtures/basic.tf b/config/test-fixtures/basic.tf index 84cbd8b31..a8938c018 100644 --- a/config/test-fixtures/basic.tf +++ b/config/test-fixtures/basic.tf @@ -31,6 +31,8 @@ resource aws_instance "web" { resource "aws_instance" "db" { security_groups = "${aws_security_group.firewall.*.id}" + + depends_on = ["aws_instance.web"] } output "web_ip" { diff --git a/config/test-fixtures/basic.tf.json b/config/test-fixtures/basic.tf.json index a7d3b308c..7c43b5b30 100644 --- a/config/test-fixtures/basic.tf.json +++ b/config/test-fixtures/basic.tf.json @@ -20,7 +20,8 @@ "resource": { "aws_instance": { "db": { - "security_groups": ["${aws_security_group.firewall.*.id}"] + "security_groups": ["${aws_security_group.firewall.*.id}"], + "depends_on": ["aws_instance.web"] }, "web": {