diff --git a/helper/schema/provider.go b/helper/schema/provider.go index 54ce391f2..eca3dcd56 100644 --- a/helper/schema/provider.go +++ b/helper/schema/provider.go @@ -229,6 +229,7 @@ func (p *Provider) ImportState( // Create the data data := r.Data(nil) + data.SetId(info.Id) data.SetType(info.Type) // Call the import function @@ -243,5 +244,16 @@ func (p *Provider) ImportState( states[i] = r.State() } + // Verify that all are non-nil. If there are any nil the error + // isn't obvious so we circumvent that with a friendlier error. + for _, s := range states { + if s == nil { + return nil, fmt.Errorf( + "nil entry in ImportState results. This is always a bug with\n" + + "the resource that is being imported. Please report this as\n" + + "a bug to Terraform.") + } + } + return states, nil } diff --git a/helper/schema/provider_test.go b/helper/schema/provider_test.go index 74b96cf34..700eabc54 100644 --- a/helper/schema/provider_test.go +++ b/helper/schema/provider_test.go @@ -201,6 +201,36 @@ func TestProviderValidateResource(t *testing.T) { } } +func TestProviderImportState_setsId(t *testing.T) { + var val string + stateFunc := func(d *ResourceData, meta interface{}) ([]*ResourceData, error) { + val = d.Id() + return []*ResourceData{d}, nil + } + + p := &Provider{ + ResourcesMap: map[string]*Resource{ + "foo": &Resource{ + Importer: &ResourceImporter{ + State: stateFunc, + }, + }, + }, + } + + _, err := p.ImportState(&terraform.InstanceInfo{ + Id: "bar", + Type: "foo", + }) + if err != nil { + t.Fatalf("err: %s", err) + } + + if val != "bar" { + t.Fatal("should set id") + } +} + func TestProviderImportState_setsType(t *testing.T) { var tVal string stateFunc := func(d *ResourceData, meta interface{}) ([]*ResourceData, error) {