diff --git a/helper/schema/provider.go b/helper/schema/provider.go index 6c6daff3d..a3edf82f7 100644 --- a/helper/schema/provider.go +++ b/helper/schema/provider.go @@ -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 diff --git a/helper/schema/provider_test.go b/helper/schema/provider_test.go index ba339f699..3c8a93940 100644 --- a/helper/schema/provider_test.go +++ b/helper/schema/provider_test.go @@ -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) { diff --git a/helper/schema/resource_importer.go b/helper/schema/resource_importer.go index b086adf9b..69d66afd9 100644 --- a/helper/schema/resource_importer.go +++ b/helper/schema/resource_importer.go @@ -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 }