command/import: document -var-file and -var is available #11211
This commit is contained in:
parent
290ad37489
commit
f84fd39ef9
|
@ -144,6 +144,15 @@ Options:
|
|||
-state-out=path Path to write updated state file. By default, the
|
||||
"-state" path will be used.
|
||||
|
||||
-var 'foo=bar' Set a variable in the Terraform configuration. This
|
||||
flag can be set multiple times. This is only useful
|
||||
with the "-config" flag.
|
||||
|
||||
-var-file=foo Set variables in the Terraform configuration from
|
||||
a file. If "terraform.tfvars" is present, it will be
|
||||
automatically loaded if this flag is not specified.
|
||||
|
||||
|
||||
`
|
||||
return strings.TrimSpace(helpText)
|
||||
}
|
||||
|
|
|
@ -159,6 +159,176 @@ func TestImport_providerConfigDisable(t *testing.T) {
|
|||
testStateOutput(t, statePath, testImportStr)
|
||||
}
|
||||
|
||||
func TestImport_providerConfigWithVar(t *testing.T) {
|
||||
defer testChdir(t, testFixturePath("import-provider-var"))()
|
||||
|
||||
statePath := testTempFile(t)
|
||||
|
||||
p := testProvider()
|
||||
ui := new(cli.MockUi)
|
||||
c := &ImportCommand{
|
||||
Meta: Meta{
|
||||
ContextOpts: testCtxConfig(p),
|
||||
Ui: ui,
|
||||
},
|
||||
}
|
||||
|
||||
p.ImportStateFn = nil
|
||||
p.ImportStateReturn = []*terraform.InstanceState{
|
||||
&terraform.InstanceState{
|
||||
ID: "yay",
|
||||
Ephemeral: terraform.EphemeralState{
|
||||
Type: "test_instance",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
configured := false
|
||||
p.ConfigureFn = func(c *terraform.ResourceConfig) error {
|
||||
configured = true
|
||||
|
||||
if v, ok := c.Get("foo"); !ok || v.(string) != "bar" {
|
||||
return fmt.Errorf("bad value: %#v", v)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
args := []string{
|
||||
"-state", statePath,
|
||||
"-var", "foo=bar",
|
||||
"test_instance.foo",
|
||||
"bar",
|
||||
}
|
||||
if code := c.Run(args); code != 0 {
|
||||
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
||||
}
|
||||
|
||||
// Verify that we were called
|
||||
if !configured {
|
||||
t.Fatal("Configure should be called")
|
||||
}
|
||||
|
||||
if !p.ImportStateCalled {
|
||||
t.Fatal("ImportState should be called")
|
||||
}
|
||||
|
||||
testStateOutput(t, statePath, testImportStr)
|
||||
}
|
||||
|
||||
func TestImport_providerConfigWithVarDefault(t *testing.T) {
|
||||
defer testChdir(t, testFixturePath("import-provider-var-default"))()
|
||||
|
||||
statePath := testTempFile(t)
|
||||
|
||||
p := testProvider()
|
||||
ui := new(cli.MockUi)
|
||||
c := &ImportCommand{
|
||||
Meta: Meta{
|
||||
ContextOpts: testCtxConfig(p),
|
||||
Ui: ui,
|
||||
},
|
||||
}
|
||||
|
||||
p.ImportStateFn = nil
|
||||
p.ImportStateReturn = []*terraform.InstanceState{
|
||||
&terraform.InstanceState{
|
||||
ID: "yay",
|
||||
Ephemeral: terraform.EphemeralState{
|
||||
Type: "test_instance",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
configured := false
|
||||
p.ConfigureFn = func(c *terraform.ResourceConfig) error {
|
||||
configured = true
|
||||
|
||||
if v, ok := c.Get("foo"); !ok || v.(string) != "bar" {
|
||||
return fmt.Errorf("bad value: %#v", v)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
args := []string{
|
||||
"-state", statePath,
|
||||
"test_instance.foo",
|
||||
"bar",
|
||||
}
|
||||
if code := c.Run(args); code != 0 {
|
||||
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
||||
}
|
||||
|
||||
// Verify that we were called
|
||||
if !configured {
|
||||
t.Fatal("Configure should be called")
|
||||
}
|
||||
|
||||
if !p.ImportStateCalled {
|
||||
t.Fatal("ImportState should be called")
|
||||
}
|
||||
|
||||
testStateOutput(t, statePath, testImportStr)
|
||||
}
|
||||
|
||||
func TestImport_providerConfigWithVarFile(t *testing.T) {
|
||||
defer testChdir(t, testFixturePath("import-provider-var-file"))()
|
||||
|
||||
statePath := testTempFile(t)
|
||||
|
||||
p := testProvider()
|
||||
ui := new(cli.MockUi)
|
||||
c := &ImportCommand{
|
||||
Meta: Meta{
|
||||
ContextOpts: testCtxConfig(p),
|
||||
Ui: ui,
|
||||
},
|
||||
}
|
||||
|
||||
p.ImportStateFn = nil
|
||||
p.ImportStateReturn = []*terraform.InstanceState{
|
||||
&terraform.InstanceState{
|
||||
ID: "yay",
|
||||
Ephemeral: terraform.EphemeralState{
|
||||
Type: "test_instance",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
configured := false
|
||||
p.ConfigureFn = func(c *terraform.ResourceConfig) error {
|
||||
configured = true
|
||||
|
||||
if v, ok := c.Get("foo"); !ok || v.(string) != "bar" {
|
||||
return fmt.Errorf("bad value: %#v", v)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
args := []string{
|
||||
"-state", statePath,
|
||||
"-var-file", "blah.tfvars",
|
||||
"test_instance.foo",
|
||||
"bar",
|
||||
}
|
||||
if code := c.Run(args); code != 0 {
|
||||
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
||||
}
|
||||
|
||||
// Verify that we were called
|
||||
if !configured {
|
||||
t.Fatal("Configure should be called")
|
||||
}
|
||||
|
||||
if !p.ImportStateCalled {
|
||||
t.Fatal("ImportState should be called")
|
||||
}
|
||||
|
||||
testStateOutput(t, statePath, testImportStr)
|
||||
}
|
||||
|
||||
/*
|
||||
func TestRefresh_badState(t *testing.T) {
|
||||
p := testProvider()
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
variable "foo" {}
|
||||
|
||||
provider "test" {
|
||||
foo = "${var.foo}"
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
foo = "bar"
|
|
@ -0,0 +1 @@
|
|||
foo = "bar"
|
|
@ -0,0 +1,5 @@
|
|||
variable "foo" {}
|
||||
|
||||
provider "test" {
|
||||
foo = "${var.foo}"
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
variable "foo" {}
|
||||
|
||||
provider "test" {
|
||||
foo = "${var.foo}"
|
||||
}
|
|
@ -54,6 +54,18 @@ The command-line flags are all optional. The list of available flags are:
|
|||
provider based on the prefix of the resource being imported. You usually
|
||||
don't need to specify this.
|
||||
|
||||
* `-var 'foo=bar'` - Set a variable in the Terraform configuration. This flag
|
||||
can be set multiple times. Variable values are interpreted as
|
||||
[HCL](/docs/configuration/syntax.html#HCL), so list and map values can be
|
||||
specified via this flag. This is only useful with the `-config` flag.
|
||||
|
||||
* `-var-file=foo` - Set variables in the Terraform configuration from
|
||||
a [variable file](/docs/configuration/variables.html#variable-files). If
|
||||
"terraform.tfvars" is present, it will be automatically loaded first. Any
|
||||
files specified by `-var-file` override any values in a "terraform.tfvars".
|
||||
This flag can be used multiple times. This is only useful with the `-config`
|
||||
flag.
|
||||
|
||||
## Provider Configuration
|
||||
|
||||
Terraform will attempt to load configuration files that configure the
|
||||
|
|
Loading…
Reference in New Issue