configs/configupgrade: Upgrade the bodies of "connection" blocks

This uses the fixed "superset" schema from the main terraform package to
apply our standard expression mapping, with the exception of "type" where
interpolation sequences are not supported due to the type being evaluated
early to retrieve the schema for decoding the rest.
This commit is contained in:
Martin Atkins 2019-02-21 18:10:31 -08:00
parent e2ef51800a
commit ac6e0e42dc
4 changed files with 48 additions and 4 deletions

View File

@ -1,8 +1,18 @@
resource "test_instance" "foo" { resource "test_instance" "foo" {
connection {
type = "ssh"
host = "${self.private_ip}"
}
provisioner "test" { provisioner "test" {
commands = "${list("a", "b", "c")}" commands = "${list("a", "b", "c")}"
when = "create" when = "create"
on_failure = "fail" on_failure = "fail"
connection {
type = "winrm"
host = "${self.public_ip}"
}
} }
} }

View File

@ -1,8 +1,18 @@
resource "test_instance" "foo" { resource "test_instance" "foo" {
connection {
type = "ssh"
host = self.private_ip
}
provisioner "test" { provisioner "test" {
commands = ["a", "b", "c"] commands = ["a", "b", "c"]
when = create when = create
on_failure = fail on_failure = fail
connection {
type = "winrm"
host = self.public_ip
}
} }
} }

View File

@ -7,13 +7,13 @@ import (
"strings" "strings"
hcl1ast "github.com/hashicorp/hcl/hcl/ast" hcl1ast "github.com/hashicorp/hcl/hcl/ast"
hcl1printer "github.com/hashicorp/hcl/hcl/printer"
hcl1token "github.com/hashicorp/hcl/hcl/token" hcl1token "github.com/hashicorp/hcl/hcl/token"
hcl2 "github.com/hashicorp/hcl2/hcl" hcl2 "github.com/hashicorp/hcl2/hcl"
hcl2syntax "github.com/hashicorp/hcl2/hcl/hclsyntax" hcl2syntax "github.com/hashicorp/hcl2/hcl/hclsyntax"
"github.com/zclconf/go-cty/cty" "github.com/zclconf/go-cty/cty"
"github.com/hashicorp/terraform/configs/configschema" "github.com/hashicorp/terraform/configs/configschema"
"github.com/hashicorp/terraform/terraform"
"github.com/hashicorp/terraform/tfdiags" "github.com/hashicorp/terraform/tfdiags"
) )
@ -611,13 +611,25 @@ func connectionBlockRule(filename string, an *analysis, adhocComments *commentQu
// connection block, rather than just for its contents. Therefore it must // connection block, rather than just for its contents. Therefore it must
// also produce the block header and body delimiters. // also produce the block header and body delimiters.
return func(buf *bytes.Buffer, blockAddr string, item *hcl1ast.ObjectItem) tfdiags.Diagnostics { return func(buf *bytes.Buffer, blockAddr string, item *hcl1ast.ObjectItem) tfdiags.Diagnostics {
var diags tfdiags.Diagnostics
body := item.Val.(*hcl1ast.ObjectType)
// TODO: For the few resource types that were setting ConnInfo in // TODO: For the few resource types that were setting ConnInfo in
// state after create/update in prior versions, generate the additional // state after create/update in prior versions, generate the additional
// explicit connection settings that are now required if and only if // explicit connection settings that are now required if and only if
// there's at least one provisioner block. // there's at least one provisioner block.
// For now, we just pass this through as-is. // For now, we just pass this through as-is.
hcl1printer.Fprint(buf, item)
buf.WriteByte('\n') schema := terraform.ConnectionBlockSupersetSchema()
return nil rules := schemaDefaultBodyRules(filename, schema, an, adhocComments)
rules["type"] = noInterpAttributeRule(filename, cty.String, an) // type is processed early in the config loader, so cannot interpolate
printComments(buf, item.LeadComment)
printBlockOpen(buf, "connection", nil, item.LineComment)
bodyDiags := upgradeBlockBody(filename, fmt.Sprintf("%s.connection", blockAddr), buf, body.List.Items, body.Rbrace, rules, adhocComments)
diags = diags.Append(bodyDiags)
buf.WriteString("}\n")
return diags
} }
} }

View File

@ -313,6 +313,18 @@ var connectionBlockSupersetSchema = &configschema.Block{
}, },
} }
// connectionBlockSupersetSchema is a schema representing the superset of all
// possible arguments for "connection" blocks across all supported connection
// types.
//
// This currently lives here because we've not yet updated our communicator
// subsystem to be aware of schema itself. It's exported only for use in the
// configs/configupgrade package and should not be used from anywhere else.
// The caller may not modify any part of the returned schema data structure.
func ConnectionBlockSupersetSchema() *configschema.Block {
return connectionBlockSupersetSchema
}
// EvalValidateResource is an EvalNode implementation that validates // EvalValidateResource is an EvalNode implementation that validates
// the configuration of a resource. // the configuration of a resource.
type EvalValidateResource struct { type EvalValidateResource struct {