diff --git a/google/data_source_google_service_account_key.go b/google/data_source_google_service_account_key.go index a673fe15..70f295f7 100644 --- a/google/data_source_google_service_account_key.go +++ b/google/data_source_google_service_account_key.go @@ -15,8 +15,7 @@ func dataSourceGoogleServiceAccountKey() *schema.Resource { Schema: map[string]*schema.Schema{ "name": { Type: schema.TypeString, - Optional: true, - Computed: true, + Required: true, ValidateFunc: validateRegexp(ServiceAccountKeyNameRegex), }, "public_key_type": { @@ -38,10 +37,10 @@ func dataSourceGoogleServiceAccountKey() *schema.Resource { Computed: true, }, "service_account_id": { - Type: schema.TypeString, - Optional: true, - ConflictsWith: []string{"name"}, - Deprecated: "Please use name to specify full service account key path projects/{project}/serviceAccounts/{serviceAccount}/keys/{keyId}", + Type: schema.TypeString, + Optional: true, + Computed: true, + Removed: "Please use name to specify full service account key path projects/{project}/serviceAccounts/{serviceAccount}/keys/{keyId}", }, }, } @@ -50,9 +49,13 @@ func dataSourceGoogleServiceAccountKey() *schema.Resource { func dataSourceGoogleServiceAccountKeyRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - keyName, err := getDataSourceServiceAccountKeyName(d) - if err != nil { - return err + keyName := d.Get("name").(string) + + // Validate name since interpolated values (i.e from a key or service + // account resource) will not get validated at plan time. + r := regexp.MustCompile(ServiceAccountKeyNameRegex) + if !r.MatchString(keyName) { + return fmt.Errorf("invalid key name %q does not match regexp %q", keyName, ServiceAccountKeyNameRegex) } publicKeyType := d.Get("public_key_type").(string) @@ -71,28 +74,3 @@ func dataSourceGoogleServiceAccountKeyRead(d *schema.ResourceData, meta interfac return nil } - -func getDataSourceServiceAccountKeyName(d *schema.ResourceData) (string, error) { - keyName := d.Get("name").(string) - keyFromSAId := d.Get("service_account_id").(string) - - // Neither name nor service_account_id specified - if keyName == "" && keyFromSAId == "" { - return "", fmt.Errorf("please use name to specify service account key being added as this data source") - } - - fullKeyName := keyName - if fullKeyName == "" { - // Key name specified as incorrectly named, deprecated service account ID field - fullKeyName = keyFromSAId - } - - // Validate name since interpolated values (i.e from a key or service - // account resource) will not get validated at plan time. - r := regexp.MustCompile(ServiceAccountKeyNameRegex) - if r.MatchString(fullKeyName) { - return fullKeyName, nil - } - - return "", fmt.Errorf("invalid key name %q does not match regexp %q", fullKeyName, ServiceAccountKeyNameRegex) -} diff --git a/google/data_source_google_service_account_key_test.go b/google/data_source_google_service_account_key_test.go index b9ecb964..ec896636 100644 --- a/google/data_source_google_service_account_key_test.go +++ b/google/data_source_google_service_account_key_test.go @@ -7,7 +7,6 @@ import ( "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" - "strings" ) func TestAccDatasourceGoogleServiceAccountKey_basic(t *testing.T) { @@ -36,49 +35,6 @@ func TestAccDatasourceGoogleServiceAccountKey_basic(t *testing.T) { resource.TestCheckResourceAttrSet(resourceName, "public_key"), ), }, - { - Config: testAccDatasourceGoogleServiceAccountKey_deprecated(account), - Check: resource.ComposeTestCheckFunc( - testAccCheckGoogleServiceAccountKeyExists(resourceName), - // Check that the 'name' starts with the service account name - resource.TestMatchResourceAttr(resourceName, "name", regexp.MustCompile(serviceAccountName)), - resource.TestCheckResourceAttrSet(resourceName, "key_algorithm"), - resource.TestCheckResourceAttrSet(resourceName, "public_key"), - ), - }, - }, - }) -} - -func TestAccDatasourceGoogleServiceAccountKey_errors(t *testing.T) { - t.Parallel() - - account := acctest.RandomWithPrefix("tf-test") - serviceAccountName := fmt.Sprintf( - "projects/%s/serviceAccounts/%s@%s.iam.gserviceaccount.com", - getTestProjectFromEnv(), - account, - getTestProjectFromEnv(), - ) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: testAccDatasourceGoogleServiceAccountKey_error( - account, - `name = "${google_service_account.acceptance.name}"`), - ExpectError: regexp.MustCompile( - fmt.Sprintf("invalid key name %q", serviceAccountName)), - }, - { - Config: testAccDatasourceGoogleServiceAccountKey_error( - account, - `service_account_id = "${google_service_account.acceptance.id}"`), - ExpectError: regexp.MustCompile( - fmt.Sprintf("invalid key name %q", serviceAccountName)), - }, }, }) } @@ -98,35 +54,3 @@ data "google_service_account_key" "acceptance" { name = "${google_service_account_key.acceptance.name}" }`, account) } - -func testAccDatasourceGoogleServiceAccountKey_deprecated(account string) string { - return fmt.Sprintf(` -resource "google_service_account" "acceptance" { - account_id = "%s" -} - -resource "google_service_account_key" "acceptance" { - service_account_id = "${google_service_account.acceptance.name}" - public_key_type = "TYPE_X509_PEM_FILE" -} - -data "google_service_account_key" "acceptance" { - service_account_id = "${google_service_account_key.acceptance.name}" -}`, account) -} - -func testAccDatasourceGoogleServiceAccountKey_error(account string, incorrectDataFields ...string) string { - return fmt.Sprintf(` -resource "google_service_account" "acceptance" { - account_id = "%s" -} - -resource "google_service_account_key" "acceptance" { - service_account_id = "${google_service_account.acceptance.name}" - public_key_type = "TYPE_X509_PEM_FILE" -} - -data "google_service_account_key" "acceptance" { -%s -}`, account, strings.Join(incorrectDataFields, "\n\t")) -}