config: self var validation
This commit is contained in:
parent
9c612964d8
commit
965fe45b9e
|
@ -477,6 +477,22 @@ func (c *Config) Validate() error {
|
|||
}
|
||||
}
|
||||
|
||||
// Validate the self variable
|
||||
for source, rc := range c.rawConfigs() {
|
||||
// Ignore provisioners. This is a pretty brittle way to do this,
|
||||
// but better than also repeating all the resources.
|
||||
if strings.Contains(source, "provision") {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, v := range rc.Variables {
|
||||
if _, ok := v.(*SelfVariable); ok {
|
||||
errs = append(errs, fmt.Errorf(
|
||||
"%s: cannot contain self-reference %s", source, v.FullKey()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(errs) > 0 {
|
||||
return &multierror.Error{Errors: errs}
|
||||
}
|
||||
|
|
|
@ -214,6 +214,20 @@ func TestConfigValidate_provSplatSelf(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestConfigValidate_resourceProvVarSelf(t *testing.T) {
|
||||
c := testConfig(t, "validate-resource-prov-self")
|
||||
if err := c.Validate(); err != nil {
|
||||
t.Fatalf("should be valid: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigValidate_resourceVarSelf(t *testing.T) {
|
||||
c := testConfig(t, "validate-resource-self")
|
||||
if err := c.Validate(); err == nil {
|
||||
t.Fatal("should not be valid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigValidate_unknownThing(t *testing.T) {
|
||||
c := testConfig(t, "validate-unknownthing")
|
||||
if err := c.Validate(); err == nil {
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
resource "aws_instance" "foo" {
|
||||
foo = "bar"
|
||||
|
||||
connection {
|
||||
host = "${self.foo}"
|
||||
}
|
||||
|
||||
provisioner "shell" {
|
||||
value = "${self.foo}"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
resource "aws_instance" "foo" {
|
||||
foo = "${self.bar}"
|
||||
}
|
Loading…
Reference in New Issue