Merge pull request #15768 from hashicorp/jbardin/remote-import

Don't ForceLocal for the import backend
This commit is contained in:
James Bardin 2017-08-11 09:12:46 -04:00 committed by GitHub
commit ea3e87b584
5 changed files with 112 additions and 15 deletions

View File

@ -124,14 +124,17 @@ func (c *ImportCommand) Run(args []string) int {
// Load the backend
b, err := c.Backend(&BackendOpts{
Config: mod.Config(),
ForceLocal: true,
})
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to load backend: %s", err))
return 1
}
// We require a local backend
// We require a backend.Local to build a context.
// This isn't necessarily a "local.Local" backend, which provides local
// operations, however that is the only current implementation. A
// "local.Local" backend also doesn't necessarily provide local state, as
// that may be delegated to a "remotestate.Backend".
local, ok := b.(backend.Local)
if !ok {
c.Ui.Error(ErrUnsupportedLocalOp)
@ -232,11 +235,12 @@ Options:
specifying aliases, such as "aws.eu". Defaults to the
normal provider prefix of the resource being imported.
-state=path Path to read and save state (unless state-out
is specified). Defaults to "terraform.tfstate".
-state=PATH Path to the source state file. Defaults to the configured
backend, or "terraform.tfstate"
-state-out=path Path to write updated state file. By default, the
"-state" path will be used.
-state-out=PATH Path to the destination state file to write to. If this
isn't specified, the source state file will be used. This
can be a new or existing path.
-var 'foo=bar' Set a variable in the Terraform configuration. This
flag can be set multiple times. This is only useful

View File

@ -110,6 +110,88 @@ func TestImport_providerConfig(t *testing.T) {
testStateOutput(t, statePath, testImportStr)
}
// "remote" state provided by the "local" backend
func TestImport_remoteState(t *testing.T) {
td := tempDir(t)
copy.CopyDir(testFixturePath("import-provider-remote-state"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
statePath := "imported.tfstate"
// init our backend
ui := new(cli.MockUi)
m := Meta{
testingOverrides: metaOverridesForProvider(testProvider()),
Ui: ui,
}
ic := &InitCommand{
Meta: m,
providerInstaller: &mockProviderInstaller{
Providers: map[string][]string{
"test": []string{"1.2.3"},
},
Dir: m.pluginDir(),
},
}
if code := ic.Run([]string{}); code != 0 {
t.Fatalf("bad: \n%s", ui.ErrorWriter)
}
p := testProvider()
ui = new(cli.MockUi)
c := &ImportCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(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{
"test_instance.foo",
"bar",
}
if code := c.Run(args); code != 0 {
fmt.Println(ui.OutputWriter)
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_providerConfigWithVar(t *testing.T) {
defer testChdir(t, testFixturePath("import-provider-var"))()

View File

@ -87,9 +87,8 @@ Options:
will write it to the same path as the statefile with
a backup extension.
-state=statefile Path to a Terraform state file to use to look
up Terraform-managed resources. By default it will
use the state "terraform.tfstate" if it exists.
-state=PATH Path to the source state file. Defaults to the configured
backend, or "terraform.tfstate"
`
return strings.TrimSpace(helpText)

View File

@ -0,0 +1,12 @@
terraform {
backend "local" {
path = "imported.tfstate"
}
}
provider "test" {
foo = "bar"
}
resource "test_instance" "foo" {
}

View File

@ -53,12 +53,12 @@ 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.
* `-state=path` - The path to read and save state files (unless state-out is
specified). Ignored when [remote state](/docs/state/remote.html) is used.
* `-state=path` - Path to the source state file to read from. Defaults to the
configured backend, or "terraform.tfstate".
* `-state-out=path` - Path to write the final state file. By default, this is
the state path. Ignored when [remote state](/docs/state/remote.html) is
used.
* `-state-out=path` - Path to the destination state file to write to. If this
isn't specified the source state file will be used. This can be a new or
existing path.
* `-var 'foo=bar'` - Set a variable in the Terraform configuration. This flag
can be set multiple times. Variable values are interpreted as