From 9f0da17a02b16abaafcd9dfc0b0d835e38628c12 Mon Sep 17 00:00:00 2001 From: The Magician Date: Tue, 16 Oct 2018 11:08:27 -0700 Subject: [PATCH] Add more import paths for google_service_account. (#2261) --- google/resource_google_service_account.go | 19 ++++++++++++++++++- .../resource_google_service_account_test.go | 14 ++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/google/resource_google_service_account.go b/google/resource_google_service_account.go index 14cb347a92c..2c6672d1c23 100644 --- a/google/resource_google_service_account.go +++ b/google/resource_google_service_account.go @@ -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{ @@ -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[^/]+)/serviceAccounts/(?P[^/]+)", + "(?P[^/]+)/(?P[^/]+)", + "(?P[^/]+)"}, 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 +} diff --git a/google/resource_google_service_account_test.go b/google/resource_google_service_account_test.go index d1279d58e50..251b261abc4 100644 --- a/google/resource_google_service_account_test.go +++ b/google/resource_google_service_account_test.go @@ -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, @@ -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, },