From f84fd39ef9286ee0e93ecc96b31c9985ce6000bd Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 24 Jan 2017 13:01:23 -0800 Subject: [PATCH] command/import: document -var-file and -var is available #11211 --- command/import.go | 9 + command/import_test.go | 170 ++++++++++++++++++ .../import-provider-var-default/main.tf | 5 + .../terraform.tfvars | 1 + .../import-provider-var-file/blah.tfvars | 1 + .../import-provider-var-file/main.tf | 5 + .../test-fixtures/import-provider-var/main.tf | 5 + website/source/docs/commands/import.html.md | 12 ++ 8 files changed, 208 insertions(+) create mode 100644 command/test-fixtures/import-provider-var-default/main.tf create mode 100644 command/test-fixtures/import-provider-var-default/terraform.tfvars create mode 100644 command/test-fixtures/import-provider-var-file/blah.tfvars create mode 100644 command/test-fixtures/import-provider-var-file/main.tf create mode 100644 command/test-fixtures/import-provider-var/main.tf diff --git a/command/import.go b/command/import.go index c31c6e542..bcad63a6d 100644 --- a/command/import.go +++ b/command/import.go @@ -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) } diff --git a/command/import_test.go b/command/import_test.go index a6a4779c4..82d2d5be9 100644 --- a/command/import_test.go +++ b/command/import_test.go @@ -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() diff --git a/command/test-fixtures/import-provider-var-default/main.tf b/command/test-fixtures/import-provider-var-default/main.tf new file mode 100644 index 000000000..bbe1466dd --- /dev/null +++ b/command/test-fixtures/import-provider-var-default/main.tf @@ -0,0 +1,5 @@ +variable "foo" {} + +provider "test" { + foo = "${var.foo}" +} diff --git a/command/test-fixtures/import-provider-var-default/terraform.tfvars b/command/test-fixtures/import-provider-var-default/terraform.tfvars new file mode 100644 index 000000000..5abc475eb --- /dev/null +++ b/command/test-fixtures/import-provider-var-default/terraform.tfvars @@ -0,0 +1 @@ +foo = "bar" diff --git a/command/test-fixtures/import-provider-var-file/blah.tfvars b/command/test-fixtures/import-provider-var-file/blah.tfvars new file mode 100644 index 000000000..5abc475eb --- /dev/null +++ b/command/test-fixtures/import-provider-var-file/blah.tfvars @@ -0,0 +1 @@ +foo = "bar" diff --git a/command/test-fixtures/import-provider-var-file/main.tf b/command/test-fixtures/import-provider-var-file/main.tf new file mode 100644 index 000000000..bbe1466dd --- /dev/null +++ b/command/test-fixtures/import-provider-var-file/main.tf @@ -0,0 +1,5 @@ +variable "foo" {} + +provider "test" { + foo = "${var.foo}" +} diff --git a/command/test-fixtures/import-provider-var/main.tf b/command/test-fixtures/import-provider-var/main.tf new file mode 100644 index 000000000..bbe1466dd --- /dev/null +++ b/command/test-fixtures/import-provider-var/main.tf @@ -0,0 +1,5 @@ +variable "foo" {} + +provider "test" { + foo = "${var.foo}" +} diff --git a/website/source/docs/commands/import.html.md b/website/source/docs/commands/import.html.md index 171456b6a..7c43e7c39 100644 --- a/website/source/docs/commands/import.html.md +++ b/website/source/docs/commands/import.html.md @@ -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