Skip to content

Commit 9f0da17

Browse files
modular-magicianemilymye
authored andcommitted
Add more import paths for google_service_account. (#2261)
1 parent 05c0ec1 commit 9f0da17

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

google/resource_google_service_account.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func resourceGoogleServiceAccount() *schema.Resource {
1515
Delete: resourceGoogleServiceAccountDelete,
1616
Update: resourceGoogleServiceAccountUpdate,
1717
Importer: &schema.ResourceImporter{
18-
State: schema.ImportStatePassthrough,
18+
State: resourceGoogleServiceAccountImport,
1919
},
2020
Schema: map[string]*schema.Schema{
2121
"email": &schema.Schema{
@@ -194,3 +194,20 @@ func saMergeBindings(bindings []*iam.Binding) []*iam.Binding {
194194

195195
return rb
196196
}
197+
198+
func resourceGoogleServiceAccountImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
199+
config := meta.(*Config)
200+
parseImportId([]string{
201+
"projects/(?P<project>[^/]+)/serviceAccounts/(?P<email>[^/]+)",
202+
"(?P<project>[^/]+)/(?P<email>[^/]+)",
203+
"(?P<email>[^/]+)"}, d, config)
204+
205+
// Replace import id for the resource id
206+
id, err := replaceVars(d, config, "projects/{{project}}/serviceAccounts/{{email}}")
207+
if err != nil {
208+
return nil, fmt.Errorf("Error constructing id: %s", err)
209+
}
210+
d.SetId(id)
211+
212+
return []*schema.ResourceData{d}, nil
213+
}

google/resource_google_service_account_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func TestAccServiceAccount_basic(t *testing.T) {
1818
displayName := "Terraform Test"
1919
displayName2 := "Terraform Test Update"
2020
project := getTestProjectFromEnv()
21+
expectedEmail := fmt.Sprintf("%s@%s.iam.gserviceaccount.com", accountId, project)
2122
resource.Test(t, resource.TestCase{
2223
PreCheck: func() { testAccPreCheck(t) },
2324
Providers: testAccProviders,
@@ -32,6 +33,19 @@ func TestAccServiceAccount_basic(t *testing.T) {
3233
},
3334
resource.TestStep{
3435
ResourceName: "google_service_account.acceptance",
36+
ImportStateId: fmt.Sprintf("projects/%s/serviceAccounts/%s", project, expectedEmail),
37+
ImportState: true,
38+
ImportStateVerify: true,
39+
},
40+
resource.TestStep{
41+
ResourceName: "google_service_account.acceptance",
42+
ImportStateId: fmt.Sprintf("%s/%s", project, expectedEmail),
43+
ImportState: true,
44+
ImportStateVerify: true,
45+
},
46+
resource.TestStep{
47+
ResourceName: "google_service_account.acceptance",
48+
ImportStateId: expectedEmail,
3549
ImportState: true,
3650
ImportStateVerify: true,
3751
},

0 commit comments

Comments
 (0)