helper/schema: default state func for import

This commit is contained in:
Mitchell Hashimoto 2016-05-04 12:56:45 -07:00
parent ba84485c87
commit c02c6c3f9c
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
3 changed files with 33 additions and 4 deletions

View File

@ -234,9 +234,13 @@ func (p *Provider) ImportState(
data.SetType(info.Type)
// Call the import function
results, err := r.Importer.State(data, p.meta)
if err != nil {
return nil, err
results := []*ResourceData{data}
if r.Importer.State != nil {
var err error
results, err = r.Importer.State(data, p.meta)
if err != nil {
return nil, err
}
}
// Convert the results to InstanceState values and return it

View File

@ -201,6 +201,30 @@ func TestProviderValidateResource(t *testing.T) {
}
}
func TestProviderImportState_default(t *testing.T) {
p := &Provider{
ResourcesMap: map[string]*Resource{
"foo": &Resource{
Importer: &ResourceImporter{},
},
},
}
states, err := p.ImportState(&terraform.InstanceInfo{
Type: "foo",
}, "bar")
if err != nil {
t.Fatalf("err: %s", err)
}
if len(states) != 1 {
t.Fatalf("bad: %#v", states)
}
if states[0].ID != "bar" {
t.Fatalf("bad: %#v", states)
}
}
func TestProviderImportState_setsId(t *testing.T) {
var val string
stateFunc := func(d *ResourceData, meta interface{}) ([]*ResourceData, error) {

View File

@ -12,7 +12,8 @@ type ResourceImporter struct {
// The functions below must all be implemented for importing to work.
// State is called to convert an ID to one or more InstanceState to
// insert into the Terraform state.
// insert into the Terraform state. If this isn't specified, then
// the ID is passed straight through.
State StateFunc
}