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

service_account_key: regression fix for v1.14 #1664

Merged
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
7 changes: 1 addition & 6 deletions google/data_source_google_service_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,11 @@ func dataSourceGoogleServiceAccount() *schema.Resource {
func dataSourceGoogleServiceAccountRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

// Get the project from the resource or fallback to the project
// in the provider configuration
project, err := getProject(d, config)
serviceAccountName, err := serviceAccountFQN(d.Get("account_id").(string), d, config)
if err != nil {
return err
}

// Get the service account as a fully qualified name
serviceAccountName := serviceAccountFQN(d.Get("account_id").(string), project)

sa, err := config.clientIAM.Projects.ServiceAccounts.Get(serviceAccountName).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Service Account %q", serviceAccountName))
Expand Down
7 changes: 1 addition & 6 deletions google/data_source_google_service_account_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,11 @@ func dataSourceGoogleServiceAccountKey() *schema.Resource {
func dataSourceGoogleServiceAccountKeyRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

// Get the project from the resource or fallback to the project
// in the provider configuration
project, err := getProject(d, config)
serviceAccountName, err := serviceAccountFQN(d.Get("service_account_id").(string), d, config)
if err != nil {
return err
}

// Get the service account as the fully qualified name
serviceAccountName := serviceAccountFQN(d.Get("service_account_id").(string), project)

publicKeyType := d.Get("public_key_type").(string)

// Confirm the service account key exists
Expand Down
6 changes: 1 addition & 5 deletions google/resource_google_service_account_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,11 @@ func resourceGoogleServiceAccountKey() *schema.Resource {
func resourceGoogleServiceAccountKeyCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

// Get the project from the resource or fallback to the project
// in the provider configuration
project, err := getProject(d, config)
serviceAccountName, err := serviceAccountFQN(d.Get("service_account_id").(string), d, config)
if err != nil {
return err
}

serviceAccountName := serviceAccountFQN(d.Get("service_account_id").(string), project)

r := &iam.CreateServiceAccountKeyRequest{
KeyAlgorithm: d.Get("key_algorithm").(string),
PrivateKeyType: d.Get("private_key_type").(string),
Expand Down
34 changes: 22 additions & 12 deletions google/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,17 +362,27 @@ func lockedCall(lockKey string, f func() error) error {
}

// serviceAccountFQN will attempt to generate the fully qualified name in the format of:
// "projects/(-|<project_name>)/serviceAccounts/<service_account_id>@<project_name>.iam.gserviceaccount.com"
func serviceAccountFQN(serviceAccount, project string) string {
// If the service account id isn't already the fully qualified name
if !strings.HasPrefix(serviceAccount, "projects/") {
// If the service account id is an email
if strings.Contains(serviceAccount, "@") {
serviceAccount = "projects/-/serviceAccounts/" + serviceAccount
} else {
// If the service account id doesn't contain the email, build it
serviceAccount = fmt.Sprintf("projects/-/serviceAccounts/%s@%s.iam.gserviceaccount.com", serviceAccount, project)
}
// "projects/(-|<project>)/serviceAccounts/<service_account_id>@<project>.iam.gserviceaccount.com"
// A project is required if we are trying to build the FQN from a service account id and
// and error will be returned in this case if no project is set in the resource or the
// provider-level config
func serviceAccountFQN(serviceAccount string, d TerraformResourceData, config *Config) (string, error) {
// If the service account id is already the fully qualified name
if strings.HasPrefix(serviceAccount, "projects/") {
return serviceAccount, nil
}

// If the service account id is an email
if strings.Contains(serviceAccount, "@") {
return "projects/-/serviceAccounts/" + serviceAccount, nil
}
return serviceAccount

// Get the project from the resource or fallback to the project
// in the provider configuration
project, err := getProject(d, config)
if err != nil {
return "", err
}

return fmt.Sprintf("projects/-/serviceAccounts/%s@%s.iam.gserviceaccount.com", serviceAccount, project), nil
}
7 changes: 6 additions & 1 deletion google/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,12 @@ func TestServiceAccountFQN(t *testing.T) {
}

for tn, tc := range cases {
serviceAccountName := serviceAccountFQN(tc.serviceAccount, tc.project)
config := &Config{Project: tc.project}
d := &schema.ResourceData{}
serviceAccountName, err := serviceAccountFQN(tc.serviceAccount, d, config)
if err != nil {
t.Fatalf("unexpected error for service account FQN: %s", err)
}
if serviceAccountName != serviceAccountExpected {
t.Errorf("bad: %s, expected '%s' but returned '%s", tn, serviceAccountExpected, serviceAccountName)
}
Expand Down