Skip to content

Commit

Permalink
[Terraform]: Remove service_account_id from data.google_service_accou…
Browse files Browse the repository at this point in the history
…nt_key (#676)

Merged PR #676.
  • Loading branch information
rileykarson authored and modular-magician committed Nov 5, 2018
1 parent fc3b300 commit b1782c9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 112 deletions.
2 changes: 1 addition & 1 deletion build/terraform
2 changes: 1 addition & 1 deletion build/terraform-beta
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)
}
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"))
}

0 comments on commit b1782c9

Please sign in to comment.