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)
|
data.SetType(info.Type)
|
||||||
|
|
||||||
// Call the import function
|
// Call the import function
|
||||||
results, err := r.Importer.State(data, p.meta)
|
results := []*ResourceData{data}
|
||||||
if err != nil {
|
if r.Importer.State != nil {
|
||||||
return nil, err
|
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
|
// 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) {
|
func TestProviderImportState_setsId(t *testing.T) {
|
||||||
var val string
|
var val string
|
||||||
stateFunc := func(d *ResourceData, meta interface{}) ([]*ResourceData, error) {
|
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.
|
// The functions below must all be implemented for importing to work.
|
||||||
|
|
||||||
// State is called to convert an ID to one or more InstanceState to
|
// 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
|
State StateFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue