Skip to content

Commit

Permalink
Add more import paths for google_service_account. (#2261)
Browse files Browse the repository at this point in the history
  • Loading branch information
modular-magician authored and emilymye committed Oct 16, 2018
1 parent 05c0ec1 commit 9f0da17
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
19 changes: 18 additions & 1 deletion google/resource_google_service_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func resourceGoogleServiceAccount() *schema.Resource {
Delete: resourceGoogleServiceAccountDelete,
Update: resourceGoogleServiceAccountUpdate,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
State: resourceGoogleServiceAccountImport,
},
Schema: map[string]*schema.Schema{
"email": &schema.Schema{
Expand Down Expand Up @@ -194,3 +194,20 @@ func saMergeBindings(bindings []*iam.Binding) []*iam.Binding {

return rb
}

func resourceGoogleServiceAccountImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config)
parseImportId([]string{
"projects/(?P<project>[^/]+)/serviceAccounts/(?P<email>[^/]+)",
"(?P<project>[^/]+)/(?P<email>[^/]+)",
"(?P<email>[^/]+)"}, d, config)

// Replace import id for the resource id
id, err := replaceVars(d, config, "projects/{{project}}/serviceAccounts/{{email}}")
if err != nil {
return nil, fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)

return []*schema.ResourceData{d}, nil
}
14 changes: 14 additions & 0 deletions google/resource_google_service_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestAccServiceAccount_basic(t *testing.T) {
displayName := "Terraform Test"
displayName2 := "Terraform Test Update"
project := getTestProjectFromEnv()
expectedEmail := fmt.Sprintf("%s@%s.iam.gserviceaccount.com", accountId, project)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand All @@ -32,6 +33,19 @@ func TestAccServiceAccount_basic(t *testing.T) {
},
resource.TestStep{
ResourceName: "google_service_account.acceptance",
ImportStateId: fmt.Sprintf("projects/%s/serviceAccounts/%s", project, expectedEmail),
ImportState: true,
ImportStateVerify: true,
},
resource.TestStep{
ResourceName: "google_service_account.acceptance",
ImportStateId: fmt.Sprintf("%s/%s", project, expectedEmail),
ImportState: true,
ImportStateVerify: true,
},
resource.TestStep{
ResourceName: "google_service_account.acceptance",
ImportStateId: expectedEmail,
ImportState: true,
ImportStateVerify: true,
},
Expand Down

0 comments on commit 9f0da17

Please sign in to comment.