Skip to content

Commit

Permalink
Merge pull request #8482 from hashicorp/b-output-dup
Browse files Browse the repository at this point in the history
config: variable names and outputs must be unique
  • Loading branch information
mitchellh authored Aug 26, 2016
2 parents 475d875 + 0fceeaa commit 706b2e2
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 33 deletions.
76 changes: 47 additions & 29 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@ func (c *Config) Validate() error {
vars := c.InterpolatedVariables()
varMap := make(map[string]*Variable)
for _, v := range c.Variables {
if _, ok := varMap[v.Name]; ok {
errs = append(errs, fmt.Errorf(
"Variable '%s': duplicate found. Variable names must be unique.",
v.Name))
}

varMap[v.Name] = v
}

Expand Down Expand Up @@ -586,43 +592,55 @@ func (c *Config) Validate() error {
}

// Check that all outputs are valid
for _, o := range c.Outputs {
var invalidKeys []string
valueKeyFound := false
for k := range o.RawConfig.Raw {
if k == "value" {
valueKeyFound = true
{
found := make(map[string]struct{})
for _, o := range c.Outputs {
// Verify the output is new
if _, ok := found[o.Name]; ok {
errs = append(errs, fmt.Errorf(
"%s: duplicate output. output names must be unique.",
o.Name))
continue
}
if k == "sensitive" {
if sensitive, ok := o.RawConfig.config[k].(bool); ok {
if sensitive {
o.Sensitive = true
}
found[o.Name] = struct{}{}

var invalidKeys []string
valueKeyFound := false
for k := range o.RawConfig.Raw {
if k == "value" {
valueKeyFound = true
continue
}
if k == "sensitive" {
if sensitive, ok := o.RawConfig.config[k].(bool); ok {
if sensitive {
o.Sensitive = true
}
continue
}

errs = append(errs, fmt.Errorf(
"%s: value for 'sensitive' must be boolean",
o.Name))
continue
}
invalidKeys = append(invalidKeys, k)
}
if len(invalidKeys) > 0 {
errs = append(errs, fmt.Errorf(
"%s: value for 'sensitive' must be boolean",
o.Name))
continue
"%s: output has invalid keys: %s",
o.Name, strings.Join(invalidKeys, ", ")))
}
invalidKeys = append(invalidKeys, k)
}
if len(invalidKeys) > 0 {
errs = append(errs, fmt.Errorf(
"%s: output has invalid keys: %s",
o.Name, strings.Join(invalidKeys, ", ")))
}
if !valueKeyFound {
errs = append(errs, fmt.Errorf(
"%s: output is missing required 'value' key", o.Name))
}

for _, v := range o.RawConfig.Variables {
if _, ok := v.(*CountVariable); ok {
if !valueKeyFound {
errs = append(errs, fmt.Errorf(
"%s: count variables are only valid within resources", o.Name))
"%s: output is missing required 'value' key", o.Name))
}

for _, v := range o.RawConfig.Variables {
if _, ok := v.(*CountVariable); ok {
errs = append(errs, fmt.Errorf(
"%s: count variables are only valid within resources", o.Name))
}
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,13 @@ func TestConfigValidate_outputBadField(t *testing.T) {
}
}

func TestConfigValidate_outputDuplicate(t *testing.T) {
c := testConfig(t, "validate-output-dup")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
}
}

func TestConfigValidate_pathVar(t *testing.T) {
c := testConfig(t, "validate-path-var")
if err := c.Validate(); err != nil {
Expand Down Expand Up @@ -440,6 +447,13 @@ func TestConfigValidate_varDefaultInterpolate(t *testing.T) {
}
}

func TestConfigValidate_varDup(t *testing.T) {
c := testConfig(t, "validate-var-dup")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
}
}

func TestConfigValidate_varMultiExactNonSlice(t *testing.T) {
c := testConfig(t, "validate-var-multi-exact-non-slice")
if err := c.Validate(); err != nil {
Expand Down
7 changes: 4 additions & 3 deletions config/loader_hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ func (t *hclConfigurable) Config() (*Config, error) {
}

type hclVariable struct {
Name string `hcl:",key"`
Default interface{}
Description string
DeclaredType string `hcl:"type"`
Fields []string `hcl:",decodedFields"`
}

var rawConfig struct {
Variable map[string]*hclVariable
Variable []*hclVariable
}

// Top-level item should be the object list
Expand All @@ -56,7 +57,7 @@ func (t *hclConfigurable) Config() (*Config, error) {
config := new(Config)
if len(rawConfig.Variable) > 0 {
config.Variables = make([]*Variable, 0, len(rawConfig.Variable))
for k, v := range rawConfig.Variable {
for _, v := range rawConfig.Variable {
// Defaults turn into a slice of map[string]interface{} and
// we need to make sure to convert that down into the
// proper type for Config.
Expand All @@ -72,7 +73,7 @@ func (t *hclConfigurable) Config() (*Config, error) {
}

newVar := &Variable{
Name: k,
Name: v.Name,
DeclaredType: v.DeclaredType,
Default: v.Default,
Description: v.Description,
Expand Down
22 changes: 22 additions & 0 deletions config/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,22 @@ func TestLoadDir_override(t *testing.T) {
}
}

func TestLoadDir_overrideVar(t *testing.T) {
c, err := LoadDir(filepath.Join(fixtureDir, "dir-override-var"))
if err != nil {
t.Fatalf("err: %s", err)
}

if c == nil {
t.Fatal("config should not be nil")
}

actual := variablesStr(c.Variables)
if actual != strings.TrimSpace(dirOverrideVarsVariablesStr) {
t.Fatalf("bad:\n%s", actual)
}
}

func TestLoadFile_mismatchedVariableTypes(t *testing.T) {
_, err := LoadFile(filepath.Join(fixtureDir, "variable-mismatched-type.tf"))
if err == nil {
Expand Down Expand Up @@ -943,6 +959,12 @@ foo
bar
`

const dirOverrideVarsVariablesStr = `
foo
baz
bar
`

const importProvidersStr = `
aws
bar
Expand Down
4 changes: 4 additions & 0 deletions config/test-fixtures/dir-override-var/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
variable "foo" {
default = "bar"
description = "bar"
}
3 changes: 3 additions & 0 deletions config/test-fixtures/dir-override-var/main_override.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
variable "foo" {
default = "baz"
}
10 changes: 10 additions & 0 deletions config/test-fixtures/validate-output-dup/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
resource "aws_instance" "web" {
}

output "ip" {
value = "foo"
}

output "ip" {
value = "bar"
}
2 changes: 1 addition & 1 deletion config/test-fixtures/validate-var-default/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ variable "foo" {
default = "bar"
}

variable "foo" {
variable "foo2" {
default = {
foo = "bar"
}
Expand Down
2 changes: 2 additions & 0 deletions config/test-fixtures/validate-var-dup/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
variable "foo" {}
variable "foo" {}

0 comments on commit 706b2e2

Please sign in to comment.