Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow other forms of project names to be matched. #1865

Merged
merged 3 commits into from
Aug 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions google/resource_kms_crypto_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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{
Expand Down
6 changes: 6 additions & 0 deletions google/resource_kms_crypto_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions google/resource_kms_key_ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
6 changes: 6 additions & 0 deletions google/resource_kms_key_ring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down