terraform: import loads the context module by default

This allows it to load provider configuration and interpolate it rather
than it coming purely from the environment.
This commit is contained in:
Mitchell Hashimoto 2016-11-02 10:29:58 -07:00
parent 6d8c8cfb01
commit 2b72882405
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
3 changed files with 68 additions and 1 deletions

View File

@ -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(),
}

View File

@ -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{

View File

@ -0,0 +1,5 @@
variable "foo" {}
provider "aws" {
foo = "${var.foo}"
}