terraform-plugin-sdk
terraform-plugin-sdk copied to clipboard
Match Resources Using Key During Import State Acceptance Tests
SDK version
github.com/hashicorp/terraform-plugin-sdk/v2 v2.14.0
Use-cases
Allow multiple resources to be used in a single configuration within an acceptance test and have the import state test complete successfully. For instance, in the random provider, the testing of import state has been separated out into its own test. The reason for this is that testing import using a config that contains multiple random_password resources produces unpredictable results because of the way in which equality matching of resources is implemented within the SDK:
if r2.Primary != nil && r2.Primary.ID == r.Primary.ID && r2.Type == r.Type && r2.Provider == r.Provider {
oldR = r2
break
}
Because the random_password resources all have the same r.Primary.ID and there is no matching on the "key" (e.g., random_password.bar) then, the first resource from the slice is considered a match and is returned. The ordering of the resources is non-deterministic so the results of a test using multiple resources in the same config would be unpredictable.
Attempted Solutions
Proposal
if r2.Primary != nil && r2.Primary.ID == r.Primary.ID && r2.Type == r.Type && r2.Provider == r.Provider && r2Key == rKey{
oldR = r2
break
}
References
https://github.com/hashicorp/terraform-plugin-sdk/issues/367