From d1a81379d049890ceaa4c7fcf21352e55b157ff4 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 4 May 2016 12:40:45 -0700 Subject: [PATCH] terraform: use InstanceInfo more appropriately, pass ID to ImportState --- terraform/eval_import_state.go | 5 +++-- terraform/resource_provider.go | 2 +- terraform/resource_provider_mock.go | 4 +++- terraform/transform_import_state.go | 3 ++- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/terraform/eval_import_state.go b/terraform/eval_import_state.go index afde2d3ff..f8483144d 100644 --- a/terraform/eval_import_state.go +++ b/terraform/eval_import_state.go @@ -10,6 +10,7 @@ import ( type EvalImportState struct { Provider *ResourceProvider Info *InstanceInfo + Id string Output *[]*InstanceState } @@ -28,10 +29,10 @@ func (n *EvalImportState) Eval(ctx EvalContext) (interface{}, error) { } // Import! - state, err := provider.ImportState(n.Info) + state, err := provider.ImportState(n.Info, n.Id) if err != nil { return nil, fmt.Errorf( - "import %s (id: %s): %s", n.Info.Type, n.Info.Id, err) + "import %s (id: %s): %s", n.Info.HumanId(), n.Id, err) } if n.Output != nil { diff --git a/terraform/resource_provider.go b/terraform/resource_provider.go index 94b283b9c..9957f6896 100644 --- a/terraform/resource_provider.go +++ b/terraform/resource_provider.go @@ -96,7 +96,7 @@ type ResourceProvider interface { // to multiple. For example, an AWS security group may contain many rules. // Each rule is represented by a separate resource in Terraform, // therefore multiple states are returned. - ImportState(*InstanceInfo) ([]*InstanceState, error) + ImportState(*InstanceInfo, string) ([]*InstanceState, error) } // ResourceProviderCloser is an interface that providers that can close diff --git a/terraform/resource_provider_mock.go b/terraform/resource_provider_mock.go index 529b8c4d2..4fa98b060 100644 --- a/terraform/resource_provider_mock.go +++ b/terraform/resource_provider_mock.go @@ -58,6 +58,7 @@ type MockResourceProvider struct { ImportStateCalled bool ImportStateInfo *InstanceInfo + ImportStateID string ImportStateReturn []*InstanceState ImportStateReturnError error ImportStateFn func(*InstanceInfo) ([]*InstanceState, error) @@ -182,12 +183,13 @@ func (p *MockResourceProvider) Resources() []ResourceType { return p.ResourcesReturn } -func (p *MockResourceProvider) ImportState(info *InstanceInfo) ([]*InstanceState, error) { +func (p *MockResourceProvider) ImportState(info *InstanceInfo, id string) ([]*InstanceState, error) { p.Lock() defer p.Unlock() p.ImportStateCalled = true p.ImportStateInfo = info + p.ImportStateID = id if p.ImportStateFn != nil { return p.ImportStateFn(info) } diff --git a/terraform/transform_import_state.go b/terraform/transform_import_state.go index 28a0a9021..4fd9363cd 100644 --- a/terraform/transform_import_state.go +++ b/terraform/transform_import_state.go @@ -58,7 +58,7 @@ func (n *graphNodeImportState) Path() []string { func (n *graphNodeImportState) EvalTree() EvalNode { var provider ResourceProvider info := &InstanceInfo{ - Id: n.ID, + Id: fmt.Sprintf("%s.%s", n.Addr.Type, n.Addr.Name), ModulePath: n.Path(), Type: n.Addr.Type, } @@ -76,6 +76,7 @@ func (n *graphNodeImportState) EvalTree() EvalNode { &EvalImportState{ Provider: &provider, Info: info, + Id: n.ID, Output: &n.states, }, },