mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
Merge pull request #19707 from hashicorp/jbardin/connection
Validate provisioner connection blocks
This commit is contained in:
commit
3e5ce033b0
@ -5675,11 +5675,8 @@ func TestContext2Apply_provisionerDestroyRefInvalid(t *testing.T) {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
if _, diags := ctx.Plan(); diags.HasErrors() {
|
// this was an apply test, but this is now caught in Validation
|
||||||
t.Fatalf("plan errors: %s", diags.Err())
|
if diags := ctx.Validate(); !diags.HasErrors() {
|
||||||
}
|
|
||||||
|
|
||||||
if _, diags := ctx.Apply(); diags == nil {
|
|
||||||
t.Fatal("expected error")
|
t.Fatal("expected error")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -494,17 +494,21 @@ func (n *EvalApplyProvisioners) apply(ctx EvalContext, provs []*configs.Provisio
|
|||||||
connBody = baseConn
|
connBody = baseConn
|
||||||
case localConn != nil:
|
case localConn != nil:
|
||||||
connBody = localConn
|
connBody = localConn
|
||||||
default: // both are nil, by elimination
|
|
||||||
connBody = hcl.EmptyBody()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
connInfo, _, connInfoDiags := ctx.EvaluateBlock(connBody, connectionBlockSupersetSchema, instanceAddr, keyData)
|
// start with an empty connInfo
|
||||||
|
connInfo := cty.NullVal(connectionBlockSupersetSchema.ImpliedType())
|
||||||
|
|
||||||
|
if connBody != nil {
|
||||||
|
var connInfoDiags tfdiags.Diagnostics
|
||||||
|
connInfo, _, connInfoDiags = ctx.EvaluateBlock(connBody, connectionBlockSupersetSchema, instanceAddr, keyData)
|
||||||
diags = diags.Append(connInfoDiags)
|
diags = diags.Append(connInfoDiags)
|
||||||
if diags.HasErrors() {
|
if diags.HasErrors() {
|
||||||
// "on failure continue" setting only applies to failures of the
|
// "on failure continue" setting only applies to failures of the
|
||||||
// provisioner itself, not to invalid configuration.
|
// provisioner itself, not to invalid configuration.
|
||||||
return diags.Err()
|
return diags.Err()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
// Call pre hook
|
// Call pre hook
|
||||||
|
@ -218,6 +218,10 @@ var connectionBlockSupersetSchema = &configschema.Block{
|
|||||||
// by the config loader and stored away in a separate field.
|
// by the config loader and stored away in a separate field.
|
||||||
|
|
||||||
// Common attributes for both connection types
|
// Common attributes for both connection types
|
||||||
|
"host": {
|
||||||
|
Type: cty.String,
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
"type": {
|
"type": {
|
||||||
Type: cty.String,
|
Type: cty.String,
|
||||||
Optional: true,
|
Optional: true,
|
||||||
@ -230,10 +234,6 @@ var connectionBlockSupersetSchema = &configschema.Block{
|
|||||||
Type: cty.String,
|
Type: cty.String,
|
||||||
Optional: true,
|
Optional: true,
|
||||||
},
|
},
|
||||||
"host": {
|
|
||||||
Type: cty.String,
|
|
||||||
Optional: true,
|
|
||||||
},
|
|
||||||
"port": {
|
"port": {
|
||||||
Type: cty.String,
|
Type: cty.String,
|
||||||
Optional: true,
|
Optional: true,
|
||||||
|
@ -374,8 +374,9 @@ func TestEvalValidateProvisioner_valid(t *testing.T) {
|
|||||||
Config: hcl.EmptyBody(),
|
Config: hcl.EmptyBody(),
|
||||||
},
|
},
|
||||||
ConnConfig: &configs.Connection{
|
ConnConfig: &configs.Connection{
|
||||||
//Type: "ssh",
|
Config: configs.SynthBody("", map[string]cty.Value{
|
||||||
Config: hcl.EmptyBody(),
|
"host": cty.StringVal("foo"),
|
||||||
|
}),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -421,6 +422,7 @@ func TestEvalValidateProvisioner_warning(t *testing.T) {
|
|||||||
},
|
},
|
||||||
ConnConfig: &configs.Connection{
|
ConnConfig: &configs.Connection{
|
||||||
Config: configs.SynthBody("", map[string]cty.Value{
|
Config: configs.SynthBody("", map[string]cty.Value{
|
||||||
|
"host": cty.StringVal("localhost"),
|
||||||
"type": cty.StringVal("ssh"),
|
"type": cty.StringVal("ssh"),
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
@ -442,7 +444,7 @@ func TestEvalValidateProvisioner_warning(t *testing.T) {
|
|||||||
var diags tfdiags.Diagnostics
|
var diags tfdiags.Diagnostics
|
||||||
diags = diags.Append(err)
|
diags = diags.Append(err)
|
||||||
if len(diags) != 1 {
|
if len(diags) != 1 {
|
||||||
t.Fatalf("wrong number of diagsnostics in %#v; want one warning", diags)
|
t.Fatalf("wrong number of diagnostics in %s; want one warning", diags.ErrWithWarnings())
|
||||||
}
|
}
|
||||||
|
|
||||||
if got, want := diags[0].Description().Summary, mp.ValidateProvisionerConfigResponse.Diagnostics[0].Description().Summary; got != want {
|
if got, want := diags[0].Description().Summary, mp.ValidateProvisionerConfigResponse.Diagnostics[0].Description().Summary; got != want {
|
||||||
@ -492,7 +494,7 @@ func TestEvalValidateProvisioner_connectionInvalid(t *testing.T) {
|
|||||||
|
|
||||||
var diags tfdiags.Diagnostics
|
var diags tfdiags.Diagnostics
|
||||||
diags = diags.Append(err)
|
diags = diags.Append(err)
|
||||||
if len(diags) != 2 {
|
if len(diags) != 3 {
|
||||||
t.Fatalf("wrong number of diagnostics; want two errors\n\n%s", diags.Err())
|
t.Fatalf("wrong number of diagnostics; want two errors\n\n%s", diags.Err())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,6 +71,7 @@ func (n *NodeValidatableResource) EvalTree() EvalNode {
|
|||||||
Schema: &provisionerSchema,
|
Schema: &provisionerSchema,
|
||||||
Config: p,
|
Config: p,
|
||||||
ResourceHasCount: hasCount,
|
ResourceHasCount: hasCount,
|
||||||
|
ConnConfig: p.Connection,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package terraform
|
package terraform
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/zclconf/go-cty/cty"
|
"github.com/zclconf/go-cty/cty"
|
||||||
@ -79,10 +80,16 @@ func (p *MockProvisioner) ProvisionResource(r provisioners.ProvisionResourceRequ
|
|||||||
p.ProvisionResourceCalled = true
|
p.ProvisionResourceCalled = true
|
||||||
p.ProvisionResourceRequest = r
|
p.ProvisionResourceRequest = r
|
||||||
if p.ApplyFn != nil {
|
if p.ApplyFn != nil {
|
||||||
|
if !r.Config.IsKnown() {
|
||||||
|
panic(fmt.Sprintf("cannot provision with unknown value: %#v", r.Config))
|
||||||
|
}
|
||||||
|
|
||||||
schema := p.getSchema()
|
schema := p.getSchema()
|
||||||
rc := NewResourceConfigShimmed(r.Config, schema.Provisioner)
|
rc := NewResourceConfigShimmed(r.Config, schema.Provisioner)
|
||||||
connVal := r.Connection
|
connVal := r.Connection
|
||||||
connMap := map[string]string{}
|
connMap := map[string]string{}
|
||||||
|
|
||||||
|
if !connVal.IsNull() && connVal.IsKnown() {
|
||||||
for it := connVal.ElementIterator(); it.Next(); {
|
for it := connVal.ElementIterator(); it.Next(); {
|
||||||
ak, av := it.Element()
|
ak, av := it.Element()
|
||||||
name := ak.AsString()
|
name := ak.AsString()
|
||||||
@ -94,6 +101,8 @@ func (p *MockProvisioner) ProvisionResource(r provisioners.ProvisionResourceRequ
|
|||||||
av, _ = convert.Convert(av, cty.String)
|
av, _ = convert.Convert(av, cty.String)
|
||||||
connMap[name] = av.AsString()
|
connMap[name] = av.AsString()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// We no longer pass the full instance state to a provisioner, so we'll
|
// We no longer pass the full instance state to a provisioner, so we'll
|
||||||
// construct a partial one that should be good enough for what existing
|
// construct a partial one that should be good enough for what existing
|
||||||
// test mocks need.
|
// test mocks need.
|
||||||
|
@ -12,12 +12,14 @@ resource "aws_instance" "foo" {
|
|||||||
|
|
||||||
resource "aws_instance" "bar" {
|
resource "aws_instance" "bar" {
|
||||||
connection {
|
connection {
|
||||||
|
host = "localhost"
|
||||||
type = "telnet"
|
type = "telnet"
|
||||||
}
|
}
|
||||||
|
|
||||||
provisioner "shell" {
|
provisioner "shell" {
|
||||||
foo = "${aws_instance.foo.value}"
|
foo = "${aws_instance.foo.value}"
|
||||||
connection {
|
connection {
|
||||||
|
host = "localhost"
|
||||||
type = "telnet"
|
type = "telnet"
|
||||||
user = "superuser"
|
user = "superuser"
|
||||||
port = 2222
|
port = 2222
|
||||||
|
@ -25,6 +25,7 @@ resource "aws_load_balancer" "weblb" {
|
|||||||
provisioner "shell" {
|
provisioner "shell" {
|
||||||
cmd = "add ${aws_instance.web.id}"
|
cmd = "add ${aws_instance.web.id}"
|
||||||
connection {
|
connection {
|
||||||
|
host = "localhost"
|
||||||
type = "magic"
|
type = "magic"
|
||||||
user = "${aws_security_group.firewall.id}"
|
user = "${aws_security_group.firewall.id}"
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user