mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-26 00:41:27 -06:00
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:
parent
6d8c8cfb01
commit
2b72882405
@ -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(),
|
||||
}
|
||||
|
||||
|
@ -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{
|
||||
|
5
terraform/test-fixtures/import-provider-vars/main.tf
Normal file
5
terraform/test-fixtures/import-provider-vars/main.tf
Normal file
@ -0,0 +1,5 @@
|
||||
variable "foo" {}
|
||||
|
||||
provider "aws" {
|
||||
foo = "${var.foo}"
|
||||
}
|
Loading…
Reference in New Issue
Block a user