From 4b5214826288656b713e4417ddf05ba5d4949f18 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Tue, 4 Dec 2018 15:49:46 -0800 Subject: [PATCH] configs/configupgrade: Upgrade provider addresses Both resource blocks and module blocks contain references to providers that are expressed as short-form provider addresses ("aws.foo" rather than "provider.aws.foo"). These rules call for those to be unwrapped as naked identifiers during upgrade, rather than appearing as quoted strings. This also introduces some further rules for other simpler meta-arguments that are required for the test fixtures for this feature. --- .../provider-addrs/input/provider-addrs.tf | 16 ++++++++++ .../provider-addrs/want/provider-addrs.tf | 16 ++++++++++ .../valid/provider-addrs/want/versions.tf | 3 ++ configs/configupgrade/upgrade_native.go | 31 +++++++++++++++++++ 4 files changed, 66 insertions(+) create mode 100644 configs/configupgrade/test-fixtures/valid/provider-addrs/input/provider-addrs.tf create mode 100644 configs/configupgrade/test-fixtures/valid/provider-addrs/want/provider-addrs.tf create mode 100644 configs/configupgrade/test-fixtures/valid/provider-addrs/want/versions.tf diff --git a/configs/configupgrade/test-fixtures/valid/provider-addrs/input/provider-addrs.tf b/configs/configupgrade/test-fixtures/valid/provider-addrs/input/provider-addrs.tf new file mode 100644 index 0000000000..7be7dc8c7f --- /dev/null +++ b/configs/configupgrade/test-fixtures/valid/provider-addrs/input/provider-addrs.tf @@ -0,0 +1,16 @@ +provider "test" { + alias = "baz" +} + +resource "test_instance" "foo" { + provider = "test.baz" +} + +module "bar" { + source = "./baz" + + providers = { + test = "test.baz" + test.foo = "test" + } +} diff --git a/configs/configupgrade/test-fixtures/valid/provider-addrs/want/provider-addrs.tf b/configs/configupgrade/test-fixtures/valid/provider-addrs/want/provider-addrs.tf new file mode 100644 index 0000000000..729452c3cd --- /dev/null +++ b/configs/configupgrade/test-fixtures/valid/provider-addrs/want/provider-addrs.tf @@ -0,0 +1,16 @@ +provider "test" { + alias = "baz" +} + +resource "test_instance" "foo" { + provider = test.baz +} + +module "bar" { + source = "./baz" + + providers = { + test = test.baz + test.foo = test + } +} diff --git a/configs/configupgrade/test-fixtures/valid/provider-addrs/want/versions.tf b/configs/configupgrade/test-fixtures/valid/provider-addrs/want/versions.tf new file mode 100644 index 0000000000..d9b6f790b9 --- /dev/null +++ b/configs/configupgrade/test-fixtures/valid/provider-addrs/want/versions.tf @@ -0,0 +1,3 @@ +terraform { + required_version = ">= 0.12" +} diff --git a/configs/configupgrade/upgrade_native.go b/configs/configupgrade/upgrade_native.go index f2bef91443..096250fb0b 100644 --- a/configs/configupgrade/upgrade_native.go +++ b/configs/configupgrade/upgrade_native.go @@ -210,6 +210,34 @@ func (u *Upgrader) upgradeNativeSyntaxFile(filename string, src []byte, an *anal // the special lifecycle arguments below. rules := justAttributesBodyRules(filename, body, an) rules["source"] = noInterpAttributeRule(filename, cty.String, an) + rules["version"] = noInterpAttributeRule(filename, cty.String, an) + rules["providers"] = func(buf *bytes.Buffer, blockAddr string, item *hcl1ast.ObjectItem) tfdiags.Diagnostics { + var diags tfdiags.Diagnostics + subBody, ok := item.Val.(*hcl1ast.ObjectType) + if !ok { + diags = diags.Append(&hcl2.Diagnostic{ + Severity: hcl2.DiagError, + Summary: "Invalid providers argument", + Detail: `The "providers" argument must be a map from provider addresses in the child module to corresponding provider addresses in this module.`, + Subject: &declRange, + }) + return diags + } + + // We're gonna cheat here and use justAttributesBodyRules to + // find all the attribute names but then just rewrite them all + // to be our specialized traversal-style mapping instead. + subRules := justAttributesBodyRules(filename, subBody, an) + for k := range subRules { + subRules[k] = maybeBareTraversalAttributeRule(filename, an) + } + buf.WriteString("providers = {\n") + bodyDiags := upgradeBlockBody(filename, blockAddr, buf, subBody.List.Items, body.Rbrace, subRules, adhocComments) + diags = diags.Append(bodyDiags) + buf.WriteString("}\n") + + return diags + } printComments(&buf, item.LeadComment) printBlockOpen(&buf, blockType, labels, item.LineComment) @@ -298,6 +326,7 @@ func (u *Upgrader) upgradeNativeSyntaxResource(filename string, buf *bytes.Buffe rules := schemaDefaultBodyRules(filename, schema, an, adhocComments) rules["count"] = normalAttributeRule(filename, cty.Number, an) + rules["provider"] = maybeBareTraversalAttributeRule(filename, an) printComments(buf, item.LeadComment) printBlockOpen(buf, blockType, labels, item.LineComment) @@ -321,6 +350,8 @@ func (u *Upgrader) upgradeNativeSyntaxProvider(filename string, buf *bytes.Buffe } schema := providerSchema.Provider rules := schemaDefaultBodyRules(filename, schema, an, adhocComments) + rules["alias"] = noInterpAttributeRule(filename, cty.String, an) + rules["version"] = noInterpAttributeRule(filename, cty.String, an) printComments(buf, item.LeadComment) printBlockOpen(buf, "provider", []string{typeName}, item.LineComment)