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

Fix bug that prevented setting 'create_ignore_already_exists' on existing resources #10394

Merged
merged 2 commits into from
Apr 15, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -266,21 +266,16 @@ func resourceGoogleServiceAccountUpdate(d *schema.ResourceData, meta interface{}
if err != nil {
return err
}

if len(updateMask) == 0 {
return nil
}

} else if d.HasChange("disabled") && d.Get("disabled").(bool) {
_, err = config.NewIamClient(userAgent).Projects.ServiceAccounts.Disable(d.Id(),
&iam.DisableServiceAccountRequest{}).Do()
if err != nil {
return err
}
}

if len(updateMask) == 0 {
return nil
}
if len(updateMask) == 0 {
return nil
}

_, err = config.NewIamClient(userAgent).Projects.ServiceAccounts.Patch(d.Id(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func TestAccServiceAccount_createIgnoreAlreadyExists(t *testing.T) {
},
// The second step creates a new resource that duplicates with the existing service account.
{
Config: testAccServiceAccountCreateIgnoreAlreadyExists(accountId, displayName, desc),
Config: testAccServiceAccountDuplicateIgnoreAlreadyExists(accountId, displayName, desc),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"google_service_account.duplicate", "member", "serviceAccount:"+expectedEmail),
Expand All @@ -129,6 +129,50 @@ func TestAccServiceAccount_createIgnoreAlreadyExists(t *testing.T) {
})
}

// Test setting create_ignore_already_exists on an existing resource
func TestAccServiceAccount_existingResourceCreateIgnoreAlreadyExists(t *testing.T) {
t.Parallel()

project := envvar.GetTestProjectFromEnv()
accountId := "a" + acctest.RandString(t, 10)
displayName := "Terraform Test"
desc := "test description"

expectedEmail := fmt.Sprintf("%s@%s.iam.gserviceaccount.com", accountId, project)
acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Steps: []resource.TestStep{
// The first step creates a new resource with create_ignore_already_exists=false
{
Config: testAccServiceAccountCreateIgnoreAlreadyExists(accountId, displayName, desc, false),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"google_service_account.acceptance", "project", project),
resource.TestCheckResourceAttr(
"google_service_account.acceptance", "member", "serviceAccount:"+expectedEmail),
),
},
{
ResourceName: "google_service_account.acceptance",
ImportStateId: fmt.Sprintf("projects/%s/serviceAccounts/%s", project, expectedEmail),
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"create_ignore_already_exists"}, // Import leaves this field out when false
},
// The second step updates the resource to have create_ignore_already_exists=true
{
Config: testAccServiceAccountCreateIgnoreAlreadyExists(accountId, displayName, desc, true),
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttr(
"google_service_account.acceptance", "project", project),
resource.TestCheckResourceAttr(
"google_service_account.acceptance", "member", "serviceAccount:"+expectedEmail),
),
},
},
})
}

func TestAccServiceAccount_Disabled(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -207,7 +251,18 @@ resource "google_service_account" "acceptance" {
`, account, name, desc)
}

func testAccServiceAccountCreateIgnoreAlreadyExists(account, name, desc string) string {
func testAccServiceAccountCreateIgnoreAlreadyExists(account, name, desc string, ignore_already_exists bool) string {
return fmt.Sprintf(`
resource "google_service_account" "acceptance" {
account_id = "%v"
display_name = "%v"
description = "%v"
create_ignore_already_exists = %t
}
`, account, name, desc, ignore_already_exists)
}

func testAccServiceAccountDuplicateIgnoreAlreadyExists(account, name, desc string) string {
return fmt.Sprintf(`
resource "google_service_account" "acceptance" {
account_id = "%v"
Expand Down
Loading