helper/schema: default state func for import
This commit is contained in:
parent
ba84485c87
commit
c02c6c3f9c
|
@ -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
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue