Skip to content

Commit

Permalink
Merge pull request #2613 from hashicorp/b-input-var-default
Browse files Browse the repository at this point in the history
core: don't prompt for variables with defaults
  • Loading branch information
phinze committed Jul 2, 2015
2 parents 3b56814 + 5c38456 commit 9de673b
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 5 deletions.
34 changes: 34 additions & 0 deletions helper/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2605,6 +2605,40 @@ func TestSchemaMap_Input(t *testing.T) {
}
}

func TestSchemaMap_InputDefault(t *testing.T) {
emptyConfig := make(map[string]interface{})
c, err := config.NewRawConfig(emptyConfig)
if err != nil {
t.Fatalf("err: %s", err)
}
rc := terraform.NewResourceConfig(c)
rc.Config = make(map[string]interface{})

input := new(terraform.MockUIInput)
input.InputFn = func(opts *terraform.InputOpts) (string, error) {
t.Fatalf("InputFn should not be called on: %#v", opts)
return "", nil
}

schema := map[string]*Schema{
"availability_zone": &Schema{
Type: TypeString,
Default: "foo",
Optional: true,
},
}
actual, err := schemaMap(schema).Input(input, rc)
if err != nil {
t.Fatalf("err: %s", err)
}

expected := map[string]interface{}{}

if !reflect.DeepEqual(expected, actual.Config) {
t.Fatalf("got: %#v\nexpected: %#v", actual.Config, expected)
}
}

func TestSchemaMap_InternalValidate(t *testing.T) {
cases := []struct {
In map[string]*Schema
Expand Down
13 changes: 8 additions & 5 deletions terraform/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (c *Context) Input(mode InputMode) error {
}
sort.Strings(names)
for _, n := range names {
// If we only care about unset variables, then if the variabel
// If we only care about unset variables, then if the variable
// is set, continue on.
if mode&InputModeVarUnset != 0 {
if _, ok := c.variables[n]; ok {
Expand All @@ -214,9 +214,13 @@ func (c *Context) Input(mode InputMode) error {
panic(fmt.Sprintf("Unknown variable type: %#v", v.Type()))
}

var defaultString string
if v.Default != nil {
defaultString = v.Default.(string)
// If the variable is not already set, and the variable defines a
// default, use that for the value.
if _, ok := c.variables[n]; !ok {
if v.Default != nil {
c.variables[n] = v.Default.(string)
continue
}
}

// Ask the user for a value for this variable
Expand All @@ -226,7 +230,6 @@ func (c *Context) Input(mode InputMode) error {
value, err = c.uiInput.Input(&InputOpts{
Id: fmt.Sprintf("var.%s", n),
Query: fmt.Sprintf("var.%s", n),
Default: defaultString,
Description: v.Description,
})
if err != nil {
Expand Down
46 changes: 46 additions & 0 deletions terraform/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3572,6 +3572,52 @@ func TestContext2Input_varOnlyUnset(t *testing.T) {
}
}

func TestContext2Input_varWithDefault(t *testing.T) {
input := new(MockUIInput)
m := testModule(t, "input-var-default")
p := testProvider("aws")
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
Variables: map[string]string{},
UIInput: input,
})

input.InputFn = func(opts *InputOpts) (string, error) {
t.Fatalf(
"Input should never be called because variable has a default: %#v", opts)
return "", nil
}

if err := ctx.Input(InputModeVar | InputModeVarUnset); err != nil {
t.Fatalf("err: %s", err)
}

if _, err := ctx.Plan(); err != nil {
t.Fatalf("err: %s", err)
}

state, err := ctx.Apply()
if err != nil {
t.Fatalf("err: %s", err)
}

actualStr := strings.TrimSpace(state.String())
expectedStr := strings.TrimSpace(`
aws_instance.foo:
ID = foo
foo = 123
type = aws_instance
`)
if actualStr != expectedStr {
t.Fatalf("expected: \n%s\ngot: \n%s\n", expectedStr, actualStr)
}
}

func TestContext2Apply(t *testing.T) {
m := testModule(t, "apply-good")
p := testProvider("aws")
Expand Down
7 changes: 7 additions & 0 deletions terraform/test-fixtures/input-var-default/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
variable "foo" {
default = 123
}

resource "aws_instance" "foo" {
foo = "${var.foo}"
}

0 comments on commit 9de673b

Please sign in to comment.