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

Remove service_account_id from data.google_service_account_key #2397

Merged
merged 1 commit into from
Nov 5, 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
46 changes: 12 additions & 34 deletions google/data_source_google_service_account_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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}",
},
},
}
Expand All @@ -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)
Expand All @@ -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)
}
76 changes: 0 additions & 76 deletions google/data_source_google_service_account_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"strings"
)

func TestAccDatasourceGoogleServiceAccountKey_basic(t *testing.T) {
Expand Down Expand Up @@ -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)),
},
},
})
}
Expand All @@ -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"))
}