-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
helper/resource: Remove data source and resource
id
attribute requi…
…rement Reference: #84 The resource instance state identifier was a Terraform versions 0.11 and earlier concept which helped core and the then SDK determine if the resource should be removed and as an identifier value in the human readable output. This concept unfortunately carried over to the testing logic when the testing logic was mostly changed to use the public, machine-readable JSON interface with Terraform, rather than reusing prior internal logic from Terraform. Using the `id` attribute value for this identifier was the default implementation and therefore those older versions of Terraform required the attribute. This is no longer necessary after Terraform versions 0.12 and later. This testing logic only supports Terraform version 0.12.26 and later. The remaining resource instance identifier and `id` attribute usage in the testing logic was: - When the `TestStep` type `ImportStateVerify` field is enabled, the instance state identifier between prior resource state and import resource state are compared to find the correct resource. - When the `TestStep` type `ImportStateIdFunc` and `ImportStateId` fields are empty as the import ID for calling `terraform import` command. - Other various `terraform` package references For the first case, a new `TestStep` type `ImportStateVerifyIdentiferAttribute` field is added, which defaults to the prior `id` attribute. This preserves the existing behavior while enabling developers to choose a different identifier attribute if omitted from the resource so they can still use the `ImportStateVerify` functionality. For the second case, developers can use the `ImportStateId*` fields to choose/create the correct import identifier value. For the final cases, no action is needed as the `terraform` package is not intended for external usage and any usage of this identifer is mainly for equality and printing debugging information which is not accessed by the testing logic. These functions will be marked with Go documentation deprecation comments in a subsequent change.
- Loading branch information
Showing
8 changed files
with
392 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
kind: ENHANCEMENTS | ||
body: 'helper/resource: Added `TestStep` type `ImportStateVerifyIdentifierAttribute` | ||
field, which can override the default `id` attribute used for matching prior | ||
resource state with imported resource state' | ||
time: 2023-08-20T19:53:00.488517-04:00 | ||
custom: | ||
Issue: "84" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
kind: FEATURES | ||
body: 'helper/resource: Removed data resource and managed resource `id` attribute | ||
requirement' | ||
time: 2023-08-20T19:56:24.356163-04:00 | ||
custom: | ||
Issue: "84" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package resource | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-go/tfprotov6" | ||
"github.com/hashicorp/terraform-plugin-go/tftypes" | ||
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider" | ||
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/datasource" | ||
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver" | ||
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/resource" | ||
) | ||
|
||
// Reference: https://github.com/hashicorp/terraform-plugin-testing/issues/84 | ||
func TestTestCase_NoDataSourceIdRequirement(t *testing.T) { | ||
t.Parallel() | ||
|
||
UnitTest(t, TestCase{ | ||
Steps: []TestStep{ | ||
{ | ||
Check: ComposeAggregateTestCheckFunc( | ||
TestCheckNoResourceAttr("data.test_datasource.test", "id"), | ||
TestCheckResourceAttr("data.test_datasource.test", "not_id", "test"), | ||
), | ||
Config: `data "test_datasource" "test" {}`, | ||
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ | ||
"test": providerserver.NewProviderServer(testprovider.Provider{ | ||
DataSources: map[string]testprovider.DataSource{ | ||
"test_datasource": { | ||
ReadResponse: &datasource.ReadResponse{ | ||
State: tftypes.NewValue( | ||
tftypes.Object{ | ||
AttributeTypes: map[string]tftypes.Type{ | ||
"not_id": tftypes.String, | ||
}, | ||
}, | ||
map[string]tftypes.Value{ | ||
"not_id": tftypes.NewValue(tftypes.String, "test"), | ||
}, | ||
), | ||
}, | ||
SchemaResponse: &datasource.SchemaResponse{ | ||
Schema: &tfprotov6.Schema{ | ||
Block: &tfprotov6.SchemaBlock{ | ||
Attributes: []*tfprotov6.SchemaAttribute{ | ||
{ | ||
Name: "not_id", | ||
Type: tftypes.String, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}), | ||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
// Reference: https://github.com/hashicorp/terraform-plugin-testing/issues/84 | ||
func TestTestCase_NoResourceIdRequirement(t *testing.T) { | ||
t.Parallel() | ||
|
||
UnitTest(t, TestCase{ | ||
Steps: []TestStep{ | ||
{ | ||
Check: ComposeAggregateTestCheckFunc( | ||
TestCheckNoResourceAttr("test_resource.test", "id"), | ||
TestCheckResourceAttr("test_resource.test", "not_id", "test"), | ||
), | ||
Config: `resource "test_resource" "test" {}`, | ||
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ | ||
"test": providerserver.NewProviderServer(testprovider.Provider{ | ||
Resources: map[string]testprovider.Resource{ | ||
"test_resource": { | ||
CreateResponse: &resource.CreateResponse{ | ||
NewState: tftypes.NewValue( | ||
tftypes.Object{ | ||
AttributeTypes: map[string]tftypes.Type{ | ||
"not_id": tftypes.String, | ||
}, | ||
}, | ||
map[string]tftypes.Value{ | ||
"not_id": tftypes.NewValue(tftypes.String, "test"), | ||
}, | ||
), | ||
}, | ||
SchemaResponse: &resource.SchemaResponse{ | ||
Schema: &tfprotov6.Schema{ | ||
Block: &tfprotov6.SchemaBlock{ | ||
Attributes: []*tfprotov6.SchemaAttribute{ | ||
{ | ||
Name: "not_id", | ||
Type: tftypes.String, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}), | ||
}, | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.