mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-30 10:47:14 -06:00
helper/schema: ImportState must set ID on the resource data
This commit is contained in:
parent
498e23162d
commit
03931bfda9
@ -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
|
||||
}
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user