Merge pull request #2613 from hashicorp/b-input-var-default
core: don't prompt for variables with defaults
This commit is contained in:
commit
9de673b7e2
|
@ -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
|
||||
|
|
|
@ -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 {
|
||||
|
@ -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
|
||||
|
@ -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 {
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
variable "foo" {
|
||||
default = 123
|
||||
}
|
||||
|
||||
resource "aws_instance" "foo" {
|
||||
foo = "${var.foo}"
|
||||
}
|
Loading…
Reference in New Issue