helper/schema: ImportState must set ID on the resource data

This commit is contained in:
Mitchell Hashimoto 2016-05-04 11:18:45 -07:00
parent 498e23162d
commit 03931bfda9
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
2 changed files with 42 additions and 0 deletions

View File

@ -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
}

View File

@ -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) {