From 2c4afd3aecf9b412206cb0953c1f98a41a4ae69a Mon Sep 17 00:00:00 2001 From: Michael Haro Date: Tue, 14 Aug 2018 11:48:08 -0700 Subject: [PATCH] Allow other forms of project names to be matched. (#1865) --- google/resource_kms_crypto_key.go | 8 ++++---- google/resource_kms_crypto_key_test.go | 6 ++++++ google/resource_kms_key_ring.go | 4 ++-- google/resource_kms_key_ring_test.go | 6 ++++++ 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/google/resource_kms_crypto_key.go b/google/resource_kms_crypto_key.go index a66ff91c36f..08dcbff6cb3 100644 --- a/google/resource_kms_crypto_key.go +++ b/google/resource_kms_crypto_key.go @@ -44,8 +44,8 @@ func resourceKmsCryptoKey() *schema.Resource { } func kmsCryptoKeyRingsEquivalent(k, old, new string, d *schema.ResourceData) bool { - keyRingIdWithSpecifiersRegex := regexp.MustCompile("^projects/([a-z0-9-]+)/locations/([a-z0-9-])+/keyRings/([a-zA-Z0-9_-]{1,63})$") - normalizedKeyRingIdRegex := regexp.MustCompile("^([a-z0-9-]+)/([a-z0-9-])+/([a-zA-Z0-9_-]{1,63})$") + keyRingIdWithSpecifiersRegex := regexp.MustCompile("^projects/(" + ProjectRegex + ")/locations/([a-z0-9-])+/keyRings/([a-zA-Z0-9_-]{1,63})$") + normalizedKeyRingIdRegex := regexp.MustCompile("^(" + ProjectRegex + ")/([a-z0-9-])+/([a-zA-Z0-9_-]{1,63})$") if matches := keyRingIdWithSpecifiersRegex.FindStringSubmatch(new); matches != nil { normMatches := normalizedKeyRingIdRegex.FindStringSubmatch(old) return normMatches != nil && normMatches[1] == matches[1] && normMatches[2] == matches[2] && normMatches[3] == matches[3] @@ -264,9 +264,9 @@ func kmsCryptoKeyNextRotation(now time.Time, period string) (result string, err func parseKmsCryptoKeyId(id string, config *Config) (*kmsCryptoKeyId, error) { parts := strings.Split(id, "/") - cryptoKeyIdRegex := regexp.MustCompile("^([a-z0-9-]+)/([a-z0-9-])+/([a-zA-Z0-9_-]{1,63})/([a-zA-Z0-9_-]{1,63})$") + cryptoKeyIdRegex := regexp.MustCompile("^(" + ProjectRegex + ")/([a-z0-9-])+/([a-zA-Z0-9_-]{1,63})/([a-zA-Z0-9_-]{1,63})$") cryptoKeyIdWithoutProjectRegex := regexp.MustCompile("^([a-z0-9-])+/([a-zA-Z0-9_-]{1,63})/([a-zA-Z0-9_-]{1,63})$") - cryptoKeyRelativeLinkRegex := regexp.MustCompile("^projects/([a-z0-9-]+)/locations/([a-z0-9-]+)/keyRings/([a-zA-Z0-9_-]{1,63})/cryptoKeys/([a-zA-Z0-9_-]{1,63})$") + cryptoKeyRelativeLinkRegex := regexp.MustCompile("^projects/(" + ProjectRegex + ")/locations/([a-z0-9-]+)/keyRings/([a-zA-Z0-9_-]{1,63})/cryptoKeys/([a-zA-Z0-9_-]{1,63})$") if cryptoKeyIdRegex.MatchString(id) { return &kmsCryptoKeyId{ diff --git a/google/resource_kms_crypto_key_test.go b/google/resource_kms_crypto_key_test.go index 7fdde73b1ad..8d2fa268c9f 100644 --- a/google/resource_kms_crypto_key_test.go +++ b/google/resource_kms_crypto_key_test.go @@ -27,6 +27,12 @@ func TestCryptoKeyIdParsing(t *testing.T) { ExpectedTerraformId: "test-project/us-central1/test-key-ring/test-key-name", ExpectedCryptoKeyId: "projects/test-project/locations/us-central1/keyRings/test-key-ring/cryptoKeys/test-key-name", }, + "id is in domain:project/location/keyRingName/cryptoKeyName format": { + ImportId: "example.com:test-project/us-central1/test-key-ring/test-key-name", + ExpectedError: false, + ExpectedTerraformId: "example.com:test-project/us-central1/test-key-ring/test-key-name", + ExpectedCryptoKeyId: "projects/example.com:test-project/locations/us-central1/keyRings/test-key-ring/cryptoKeys/test-key-name", + }, "id contains name that is longer than 63 characters": { ImportId: "test-project/us-central1/test-key-ring/can-you-believe-that-this-cryptokey-name-is-this-extravagantly-long", ExpectedError: true, diff --git a/google/resource_kms_key_ring.go b/google/resource_kms_key_ring.go index aa817ccce0c..1d905162050 100644 --- a/google/resource_kms_key_ring.go +++ b/google/resource_kms_key_ring.go @@ -134,9 +134,9 @@ func resourceKmsKeyRingDelete(d *schema.ResourceData, meta interface{}) error { func parseKmsKeyRingId(id string, config *Config) (*kmsKeyRingId, error) { parts := strings.Split(id, "/") - keyRingIdRegex := regexp.MustCompile("^([a-z0-9-]+)/([a-z0-9-])+/([a-zA-Z0-9_-]{1,63})$") + keyRingIdRegex := regexp.MustCompile("^(" + ProjectRegex + ")/([a-z0-9-])+/([a-zA-Z0-9_-]{1,63})$") keyRingIdWithoutProjectRegex := regexp.MustCompile("^([a-z0-9-])+/([a-zA-Z0-9_-]{1,63})$") - keyRingRelativeLinkRegex := regexp.MustCompile("^projects/([a-z0-9-]+)/locations/([a-z0-9-]+)/keyRings/([a-zA-Z0-9_-]{1,63})$") + keyRingRelativeLinkRegex := regexp.MustCompile("^projects/(" + ProjectRegex + ")/locations/([a-z0-9-]+)/keyRings/([a-zA-Z0-9_-]{1,63})$") if keyRingIdRegex.MatchString(id) { return &kmsKeyRingId{ diff --git a/google/resource_kms_key_ring_test.go b/google/resource_kms_key_ring_test.go index 78116e7ef56..6f82ec9f763 100644 --- a/google/resource_kms_key_ring_test.go +++ b/google/resource_kms_key_ring_test.go @@ -24,6 +24,12 @@ func TestKeyRingIdParsing(t *testing.T) { ExpectedTerraformId: "test-project/us-central1/test-key-ring", ExpectedKeyRingId: "projects/test-project/locations/us-central1/keyRings/test-key-ring", }, + "id is in domain:project/location/keyRingName format": { + ImportId: "example.com:test-project/us-central1/test-key-ring", + ExpectedError: false, + ExpectedTerraformId: "example.com:test-project/us-central1/test-key-ring", + ExpectedKeyRingId: "projects/example.com:test-project/locations/us-central1/keyRings/test-key-ring", + }, "id contains name that is longer than 63 characters": { ImportId: "test-project/us-central1/can-you-believe-that-this-key-ring-name-is-exactly-64-characters", ExpectedError: true,