helper/schema: test ImportState

This commit is contained in:
Mitchell Hashimoto 2016-04-30 18:36:31 -05:00
parent 33bbf09ff6
commit 445194ebdf
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
2 changed files with 60 additions and 2 deletions

View File

@ -214,6 +214,34 @@ func (p *Provider) Resources() []terraform.ResourceType {
return result
}
func (p *Provider) ImportState(info *terraform.InstanceInfo) ([]*terraform.InstanceState, error) {
return nil, nil
func (p *Provider) ImportState(
info *terraform.InstanceInfo) ([]*terraform.InstanceState, error) {
// Find the resource
r, ok := p.ResourcesMap[info.Type]
if !ok {
return nil, fmt.Errorf("unknown resource type: %s", info.Type)
}
// If it doesn't support import, error
if r.Importer == nil {
return nil, fmt.Errorf("resource %s doesn't support import", info.Type)
}
// Create the data
data := r.Data(nil)
data.SetType(info.Type)
// Call the import function
results, err := r.Importer.State(data, p.meta)
if err != nil {
return nil, err
}
// Convert the results to InstanceState values and return it
states := make([]*terraform.InstanceState, len(results))
for i, r := range results {
states[i] = r.State()
}
return states, nil
}

View File

@ -201,6 +201,36 @@ func TestProviderValidateResource(t *testing.T) {
}
}
func TestProviderImportState_setsType(t *testing.T) {
var tVal string
stateFunc := func(d *ResourceData, meta interface{}) ([]*ResourceData, error) {
d.SetId("foo")
tVal = d.State().Ephemeral.Type
return []*ResourceData{d}, nil
}
p := &Provider{
ResourcesMap: map[string]*Resource{
"foo": &Resource{
Importer: &ResourceImporter{
State: stateFunc,
},
},
},
}
_, err := p.ImportState(&terraform.InstanceInfo{
Type: "foo",
})
if err != nil {
t.Fatalf("err: %s", err)
}
if tVal != "foo" {
t.Fatal("should set type")
}
}
func TestProviderMeta(t *testing.T) {
p := new(Provider)
if v := p.Meta(); v != nil {