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)