diff --git a/terraform/context_import.go b/terraform/context_import.go index c79f7f78a..382fc8e00 100644 --- a/terraform/context_import.go +++ b/terraform/context_import.go @@ -43,10 +43,17 @@ func (c *Context) Import(opts *ImportOpts) (*State, error) { // Copy our own state c.state = c.state.DeepCopy() + // If no module is given, default to the module configured with + // the Context. + module := opts.Module + if module == nil { + module = c.module + } + // Initialize our graph builder builder := &ImportGraphBuilder{ ImportTargets: opts.Targets, - Module: opts.Module, + Module: module, Providers: c.components.ResourceProviders(), } diff --git a/terraform/context_import_test.go b/terraform/context_import_test.go index f344fac95..cbe88f643 100644 --- a/terraform/context_import_test.go +++ b/terraform/context_import_test.go @@ -209,6 +209,61 @@ func TestContextImport_moduleProvider(t *testing.T) { } } +// Test that import will interpolate provider configuration and use +// that configuration for import. +func TestContextImport_providerVarConfig(t *testing.T) { + p := testProvider("aws") + ctx := testContext2(t, &ContextOpts{ + Module: testModule(t, "import-provider-vars"), + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + Variables: map[string]interface{}{ + "foo": "bar", + }, + }) + + configured := false + p.ConfigureFn = func(c *ResourceConfig) error { + configured = true + + if v, ok := c.Get("foo"); !ok || v.(string) != "bar" { + return fmt.Errorf("bad value: %#v", v) + } + + return nil + } + + p.ImportStateReturn = []*InstanceState{ + &InstanceState{ + ID: "foo", + Ephemeral: EphemeralState{Type: "aws_instance"}, + }, + } + + state, err := ctx.Import(&ImportOpts{ + Targets: []*ImportTarget{ + &ImportTarget{ + Addr: "aws_instance.foo", + ID: "bar", + }, + }, + }) + if err != nil { + t.Fatalf("err: %s", err) + } + + if !configured { + t.Fatal("didn't configure provider") + } + + actual := strings.TrimSpace(state.String()) + expected := strings.TrimSpace(testImportStr) + if actual != expected { + t.Fatalf("bad: \n%s", actual) + } +} + func TestContextImport_refresh(t *testing.T) { p := testProvider("aws") ctx := testContext2(t, &ContextOpts{ diff --git a/terraform/test-fixtures/import-provider-vars/main.tf b/terraform/test-fixtures/import-provider-vars/main.tf new file mode 100644 index 000000000..f18955c78 --- /dev/null +++ b/terraform/test-fixtures/import-provider-vars/main.tf @@ -0,0 +1,5 @@ +variable "foo" {} + +provider "aws" { + foo = "${var.foo}" +}