Merge pull request #20826 from hashicorp/jbardin/validate-connection
merge connection blocks for validation
This commit is contained in:
commit
a63c1af8e0
|
@ -719,6 +719,74 @@ func TestContext2Validate_provisionerConfig_bad(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContext2Validate_badResourceConnection(t *testing.T) {
|
||||||
|
m := testModule(t, "validate-bad-resource-connection")
|
||||||
|
p := testProvider("aws")
|
||||||
|
p.GetSchemaReturn = &ProviderSchema{
|
||||||
|
ResourceTypes: map[string]*configschema.Block{
|
||||||
|
"aws_instance": {
|
||||||
|
Attributes: map[string]*configschema.Attribute{
|
||||||
|
"foo": {Type: cty.String, Optional: true},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
pr := simpleMockProvisioner()
|
||||||
|
|
||||||
|
c := testContext2(t, &ContextOpts{
|
||||||
|
Config: m,
|
||||||
|
ProviderResolver: providers.ResolverFixed(
|
||||||
|
map[string]providers.Factory{
|
||||||
|
"aws": testProviderFuncFixed(p),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
Provisioners: map[string]ProvisionerFactory{
|
||||||
|
"shell": testProvisionerFuncFixed(pr),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
diags := c.Validate()
|
||||||
|
t.Log(diags.Err())
|
||||||
|
if !diags.HasErrors() {
|
||||||
|
t.Fatalf("succeeded; want error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContext2Validate_badProvisionerConnection(t *testing.T) {
|
||||||
|
m := testModule(t, "validate-bad-prov-connection")
|
||||||
|
p := testProvider("aws")
|
||||||
|
p.GetSchemaReturn = &ProviderSchema{
|
||||||
|
ResourceTypes: map[string]*configschema.Block{
|
||||||
|
"aws_instance": {
|
||||||
|
Attributes: map[string]*configschema.Attribute{
|
||||||
|
"foo": {Type: cty.String, Optional: true},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
pr := simpleMockProvisioner()
|
||||||
|
|
||||||
|
c := testContext2(t, &ContextOpts{
|
||||||
|
Config: m,
|
||||||
|
ProviderResolver: providers.ResolverFixed(
|
||||||
|
map[string]providers.Factory{
|
||||||
|
"aws": testProviderFuncFixed(p),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
Provisioners: map[string]ProvisionerFactory{
|
||||||
|
"shell": testProvisionerFuncFixed(pr),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
diags := c.Validate()
|
||||||
|
t.Log(diags.Err())
|
||||||
|
if !diags.HasErrors() {
|
||||||
|
t.Fatalf("succeeded; want error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestContext2Validate_provisionerConfig_good(t *testing.T) {
|
func TestContext2Validate_provisionerConfig_good(t *testing.T) {
|
||||||
m := testModule(t, "validate-bad-prov-conf")
|
m := testModule(t, "validate-bad-prov-conf")
|
||||||
p := testProvider("aws")
|
p := testProvider("aws")
|
||||||
|
|
|
@ -109,13 +109,13 @@ func (n *EvalValidateProvider) Eval(ctx EvalContext) (interface{}, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// EvalValidateProvisioner is an EvalNode implementation that validates
|
// EvalValidateProvisioner is an EvalNode implementation that validates
|
||||||
// the configuration of a provisioner belonging to a resource.
|
// the configuration of a provisioner belonging to a resource. The provisioner
|
||||||
|
// config is expected to contain the merged connection configurations.
|
||||||
type EvalValidateProvisioner struct {
|
type EvalValidateProvisioner struct {
|
||||||
ResourceAddr addrs.Resource
|
ResourceAddr addrs.Resource
|
||||||
Provisioner *provisioners.Interface
|
Provisioner *provisioners.Interface
|
||||||
Schema **configschema.Block
|
Schema **configschema.Block
|
||||||
Config *configs.Provisioner
|
Config *configs.Provisioner
|
||||||
ConnConfig *configs.Connection
|
|
||||||
ResourceHasCount bool
|
ResourceHasCount bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -149,10 +149,9 @@ func (n *EvalValidateProvisioner) Eval(ctx EvalContext) (interface{}, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
// Now validate the connection config, which might either be from
|
// Now validate the connection config, which contains the merged bodies
|
||||||
// the provisioner block itself or inherited from the resource's
|
// of the resource and provisioner connection blocks.
|
||||||
// shared connection info.
|
connDiags := n.validateConnConfig(ctx, config.Connection, n.ResourceAddr)
|
||||||
connDiags := n.validateConnConfig(ctx, n.ConnConfig, n.ResourceAddr)
|
|
||||||
diags = diags.Append(connDiags)
|
diags = diags.Append(connDiags)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -372,11 +372,12 @@ func TestEvalValidateProvisioner_valid(t *testing.T) {
|
||||||
Config: &configs.Provisioner{
|
Config: &configs.Provisioner{
|
||||||
Type: "baz",
|
Type: "baz",
|
||||||
Config: hcl.EmptyBody(),
|
Config: hcl.EmptyBody(),
|
||||||
},
|
Connection: &configs.Connection{
|
||||||
ConnConfig: &configs.Connection{
|
Config: configs.SynthBody("", map[string]cty.Value{
|
||||||
Config: configs.SynthBody("", map[string]cty.Value{
|
"host": cty.StringVal("localhost"),
|
||||||
"host": cty.StringVal("foo"),
|
"type": cty.StringVal("ssh"),
|
||||||
}),
|
}),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -419,12 +420,12 @@ func TestEvalValidateProvisioner_warning(t *testing.T) {
|
||||||
Config: &configs.Provisioner{
|
Config: &configs.Provisioner{
|
||||||
Type: "baz",
|
Type: "baz",
|
||||||
Config: hcl.EmptyBody(),
|
Config: hcl.EmptyBody(),
|
||||||
},
|
Connection: &configs.Connection{
|
||||||
ConnConfig: &configs.Connection{
|
Config: configs.SynthBody("", map[string]cty.Value{
|
||||||
Config: configs.SynthBody("", map[string]cty.Value{
|
"host": cty.StringVal("localhost"),
|
||||||
"host": cty.StringVal("localhost"),
|
"type": cty.StringVal("ssh"),
|
||||||
"type": cty.StringVal("ssh"),
|
}),
|
||||||
}),
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -477,13 +478,13 @@ func TestEvalValidateProvisioner_connectionInvalid(t *testing.T) {
|
||||||
Config: &configs.Provisioner{
|
Config: &configs.Provisioner{
|
||||||
Type: "baz",
|
Type: "baz",
|
||||||
Config: hcl.EmptyBody(),
|
Config: hcl.EmptyBody(),
|
||||||
},
|
Connection: &configs.Connection{
|
||||||
ConnConfig: &configs.Connection{
|
Config: configs.SynthBody("", map[string]cty.Value{
|
||||||
Config: configs.SynthBody("", map[string]cty.Value{
|
"type": cty.StringVal("ssh"),
|
||||||
"type": cty.StringVal("ssh"),
|
"bananananananana": cty.StringVal("foo"),
|
||||||
"bananananananana": cty.StringVal("foo"),
|
"bazaz": cty.StringVal("bar"),
|
||||||
"bazaz": cty.StringVal("bar"),
|
}),
|
||||||
}),
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package terraform
|
package terraform
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/hashicorp/terraform/configs"
|
||||||
"github.com/hashicorp/terraform/configs/configschema"
|
"github.com/hashicorp/terraform/configs/configschema"
|
||||||
"github.com/hashicorp/terraform/providers"
|
"github.com/hashicorp/terraform/providers"
|
||||||
"github.com/hashicorp/terraform/provisioners"
|
"github.com/hashicorp/terraform/provisioners"
|
||||||
|
@ -58,6 +59,13 @@ func (n *NodeValidatableResource) EvalTree() EvalNode {
|
||||||
for _, p := range managed.Provisioners {
|
for _, p := range managed.Provisioners {
|
||||||
var provisioner provisioners.Interface
|
var provisioner provisioners.Interface
|
||||||
var provisionerSchema *configschema.Block
|
var provisionerSchema *configschema.Block
|
||||||
|
|
||||||
|
if p.Connection == nil {
|
||||||
|
p.Connection = config.Managed.Connection
|
||||||
|
} else if config.Managed.Connection != nil {
|
||||||
|
p.Connection.Config = configs.MergeBodies(config.Managed.Connection.Config, p.Connection.Config)
|
||||||
|
}
|
||||||
|
|
||||||
seq.Nodes = append(
|
seq.Nodes = append(
|
||||||
seq.Nodes,
|
seq.Nodes,
|
||||||
&EvalGetProvisioner{
|
&EvalGetProvisioner{
|
||||||
|
@ -71,7 +79,6 @@ func (n *NodeValidatableResource) EvalTree() EvalNode {
|
||||||
Schema: &provisionerSchema,
|
Schema: &provisionerSchema,
|
||||||
Config: p,
|
Config: p,
|
||||||
ResourceHasCount: hasCount,
|
ResourceHasCount: hasCount,
|
||||||
ConnConfig: p.Connection,
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
resource "aws_instance" "foo" {
|
||||||
|
provisioner "shell" {
|
||||||
|
test_string = "test"
|
||||||
|
connection {
|
||||||
|
user = "test"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
resource "aws_instance" "foo" {
|
||||||
|
connection {
|
||||||
|
user = "test"
|
||||||
|
}
|
||||||
|
provisioner "shell" {
|
||||||
|
test_string = "test"
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue