mirror of https://github.com/hashicorp/terraform
Due to the lossiness of our legacy models for diff and state, shimming a diff and then creating a state from it produces a different result than shimming a state directly. That means that ImportStateVerify no longer works as expected if there are any Computed attributes in the schema where d.Set isn't called during Read. Fixing that for every case would require some risky changes to the shim behavior, so we're instead going to ask provider developers to address it by adding `d.Set` calls where needed, since that is the contract for "Computed" anyway -- a default value should be produced during Create, and thus by extension during Import. However, since a common situation where this occurs is attributes marked as "Removed", since all of the code that deals with them has generally been deleted, we'll avoid problems in that case here by treating Removed attributes as ignored for the purposes of ImportStateVerify. This required exporting some functionality that was formerly unexported in helper/schema, but it's a relatively harmless schema introspection function so shouldn't be a big deal to export it.pull/21031/head
parent
c94f8f9067
commit
bd1a215580
@ -0,0 +1,60 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
)
|
||||
|
||||
func testResourceImportRemoved() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Create: testResourceImportRemovedCreate,
|
||||
Read: testResourceImportRemovedRead,
|
||||
Delete: testResourceImportRemovedDelete,
|
||||
Update: testResourceImportRemovedUpdate,
|
||||
|
||||
Importer: &schema.ResourceImporter{
|
||||
State: testResourceImportRemovedImportState,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
"removed": {
|
||||
Type: schema.TypeInt,
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
Removed: "do not use",
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func testResourceImportRemovedImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
|
||||
var results []*schema.ResourceData
|
||||
|
||||
results = append(results, d)
|
||||
|
||||
{
|
||||
other := testResourceDefaults()
|
||||
od := other.Data(nil)
|
||||
od.SetType("test_resource_import_removed")
|
||||
od.SetId("foo")
|
||||
results = append(results, od)
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func testResourceImportRemovedCreate(d *schema.ResourceData, meta interface{}) error {
|
||||
d.SetId("foo")
|
||||
return testResourceImportRemovedRead(d, meta)
|
||||
}
|
||||
|
||||
func testResourceImportRemovedUpdate(d *schema.ResourceData, meta interface{}) error {
|
||||
return testResourceImportRemovedRead(d, meta)
|
||||
}
|
||||
|
||||
func testResourceImportRemovedRead(d *schema.ResourceData, meta interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func testResourceImportRemovedDelete(d *schema.ResourceData, meta interface{}) error {
|
||||
return nil
|
||||
}
|
||||
@ -0,0 +1,41 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
)
|
||||
|
||||
func TestResourceImportRemoved(t *testing.T) {
|
||||
resource.UnitTest(t, resource.TestCase{
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckResourceDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: strings.TrimSpace(`
|
||||
resource "test_resource_import_removed" "foo" {
|
||||
}
|
||||
`),
|
||||
},
|
||||
{
|
||||
ImportState: true,
|
||||
ResourceName: "test_resource_import_removed.foo",
|
||||
|
||||
// This is attempting to guard against regressions of:
|
||||
// https://github.com/hashicorp/terraform/issues/20985
|
||||
//
|
||||
// Removed attributes are generally not populated during Create,
|
||||
// Update, Read, or Import by provider code but due to our
|
||||
// legacy diff format being lossy they end up getting populated
|
||||
// with zero values during shimming in all cases except Import,
|
||||
// which doesn't go through a diff.
|
||||
//
|
||||
// This is testing that the shimming inconsistency won't cause
|
||||
// ImportStateVerify failures for these, since we now ignore
|
||||
// attributes marked as Removed when comparing.
|
||||
ImportStateVerify: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
Loading…
Reference in new issue