terraform: use InstanceInfo more appropriately, pass ID to ImportState

This commit is contained in:
Mitchell Hashimoto 2016-05-04 12:40:45 -07:00
parent f6a59734ef
commit d1a81379d0
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
4 changed files with 9 additions and 5 deletions

View File

@ -10,6 +10,7 @@ import (
type EvalImportState struct { type EvalImportState struct {
Provider *ResourceProvider Provider *ResourceProvider
Info *InstanceInfo Info *InstanceInfo
Id string
Output *[]*InstanceState Output *[]*InstanceState
} }
@ -28,10 +29,10 @@ func (n *EvalImportState) Eval(ctx EvalContext) (interface{}, error) {
} }
// Import! // Import!
state, err := provider.ImportState(n.Info) state, err := provider.ImportState(n.Info, n.Id)
if err != nil { if err != nil {
return nil, fmt.Errorf( 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 { if n.Output != nil {

View File

@ -96,7 +96,7 @@ type ResourceProvider interface {
// to multiple. For example, an AWS security group may contain many rules. // to multiple. For example, an AWS security group may contain many rules.
// Each rule is represented by a separate resource in Terraform, // Each rule is represented by a separate resource in Terraform,
// therefore multiple states are returned. // therefore multiple states are returned.
ImportState(*InstanceInfo) ([]*InstanceState, error) ImportState(*InstanceInfo, string) ([]*InstanceState, error)
} }
// ResourceProviderCloser is an interface that providers that can close // ResourceProviderCloser is an interface that providers that can close

View File

@ -58,6 +58,7 @@ type MockResourceProvider struct {
ImportStateCalled bool ImportStateCalled bool
ImportStateInfo *InstanceInfo ImportStateInfo *InstanceInfo
ImportStateID string
ImportStateReturn []*InstanceState ImportStateReturn []*InstanceState
ImportStateReturnError error ImportStateReturnError error
ImportStateFn func(*InstanceInfo) ([]*InstanceState, error) ImportStateFn func(*InstanceInfo) ([]*InstanceState, error)
@ -182,12 +183,13 @@ func (p *MockResourceProvider) Resources() []ResourceType {
return p.ResourcesReturn return p.ResourcesReturn
} }
func (p *MockResourceProvider) ImportState(info *InstanceInfo) ([]*InstanceState, error) { func (p *MockResourceProvider) ImportState(info *InstanceInfo, id string) ([]*InstanceState, error) {
p.Lock() p.Lock()
defer p.Unlock() defer p.Unlock()
p.ImportStateCalled = true p.ImportStateCalled = true
p.ImportStateInfo = info p.ImportStateInfo = info
p.ImportStateID = id
if p.ImportStateFn != nil { if p.ImportStateFn != nil {
return p.ImportStateFn(info) return p.ImportStateFn(info)
} }

View File

@ -58,7 +58,7 @@ func (n *graphNodeImportState) Path() []string {
func (n *graphNodeImportState) EvalTree() EvalNode { func (n *graphNodeImportState) EvalTree() EvalNode {
var provider ResourceProvider var provider ResourceProvider
info := &InstanceInfo{ info := &InstanceInfo{
Id: n.ID, Id: fmt.Sprintf("%s.%s", n.Addr.Type, n.Addr.Name),
ModulePath: n.Path(), ModulePath: n.Path(),
Type: n.Addr.Type, Type: n.Addr.Type,
} }
@ -76,6 +76,7 @@ func (n *graphNodeImportState) EvalTree() EvalNode {
&EvalImportState{ &EvalImportState{
Provider: &provider, Provider: &provider,
Info: info, Info: info,
Id: n.ID,
Output: &n.states, Output: &n.states,
}, },
}, },