From 6cffceb995e4c23e225ad1c0fe0839dda1220feb Mon Sep 17 00:00:00 2001 From: The Magician Date: Fri, 22 Jul 2022 13:44:20 -0700 Subject: [PATCH] Made kms crypto key import set id correctly (#6305) (#12164) Resolved https://github.com/hashicorp/terraform-provider-google/issues/12056 Signed-off-by: Modular Magician --- .changelog/6305.txt | 3 +++ google/resource_kms_crypto_key.go | 6 ++++++ google/resource_kms_crypto_key_test.go | 7 +++++++ 3 files changed, 16 insertions(+) create mode 100644 .changelog/6305.txt diff --git a/.changelog/6305.txt b/.changelog/6305.txt new file mode 100644 index 00000000000..9511bbe770a --- /dev/null +++ b/.changelog/6305.txt @@ -0,0 +1,3 @@ +```release-note:bug +kms: fixed setting of resource id post-import for `google_kms_crypto_key` +``` diff --git a/google/resource_kms_crypto_key.go b/google/resource_kms_crypto_key.go index e58eee43bc1..a27ab0dd329 100644 --- a/google/resource_kms_crypto_key.go +++ b/google/resource_kms_crypto_key.go @@ -423,6 +423,12 @@ func resourceKMSCryptoKeyImport(d *schema.ResourceData, meta interface{}) ([]*sc return nil, fmt.Errorf("Error setting skip_initial_version_creation: %s", err) } + id, err := replaceVars(d, config, "{{key_ring}}/cryptoKeys/{{name}}") + if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + return []*schema.ResourceData{d}, nil } diff --git a/google/resource_kms_crypto_key_test.go b/google/resource_kms_crypto_key_test.go index 0657d2563ef..fc3fc28c009 100644 --- a/google/resource_kms_crypto_key_test.go +++ b/google/resource_kms_crypto_key_test.go @@ -187,6 +187,13 @@ func TestAccKmsCryptoKey_basic(t *testing.T) { ImportState: true, ImportStateVerify: true, }, + // Test importing with a short id + { + ResourceName: "google_kms_crypto_key.crypto_key", + ImportState: true, + ImportStateId: fmt.Sprintf("%s/%s/%s/%s", projectId, location, keyRingName, cryptoKeyName), + ImportStateVerify: true, + }, // Use a separate TestStep rather than a CheckDestroy because we need the project to still exist. { Config: testGoogleKmsCryptoKey_removed(projectId, projectOrg, projectBillingAccount, keyRingName),