Skip to content

Commit 7c9caac

Browse files
Upstream new restore policy (#4267) (#2750)
* Adds REVERT_AND_IGNORE description and prefix on log lines with printf * Fixes typo * Adds new restore policy REVERT_AND_IGNORE_FAILURE in order to ignore errors returned on the revert during the resource destroy * Adds new test for new restore policy. This function should not check revert because the policy says to ignore. I have injected an error on the undelete function to simulate API error and it worked as expected. Worth to mention that on day to day it is just testing it overall behavior when the restore_policy is REVERT_AND_IGNORE_FAILURE * Reordering error handling code, no functional changes Co-authored-by: Thiago Carvalho <[email protected]> Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Thiago Carvalho <[email protected]>
1 parent fce58b0 commit 7c9caac

File tree

4 files changed

+55
-8
lines changed

4 files changed

+55
-8
lines changed

.changelog/4267.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
project: added new restore_policy `REVERT_AND_IGNORE_FAILURE` to `google_project_default_service_accounts`
3+
```

google-beta/resource_google_project_default_service_accounts.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package google
22

33
import (
44
"fmt"
5+
"log"
56
"strings"
67
"time"
78

@@ -48,9 +49,9 @@ func resourceGoogleProjectDefaultServiceAccounts() *schema.Resource {
4849
Type: schema.TypeString,
4950
Optional: true,
5051
Default: "REVERT",
51-
ValidateFunc: validation.StringInSlice([]string{"NONE", "REVERT"}, false),
52+
ValidateFunc: validation.StringInSlice([]string{"NONE", "REVERT", "REVERT_AND_IGNORE_FAILURE"}, false),
5253
Description: `The action to be performed in the default service accounts on the resource destroy.
53-
Valid values are NONE and REVERT. If set to REVERT it will attempt to restore all default SAs but in the DEPRIVILEGE action.`,
54+
Valid values are NONE, REVERT and REVERT_AND_IGNORE_FAILURE. It is applied for any action but in the DEPRIVILEGE.`,
5455
},
5556
"service_accounts": {
5657
Type: schema.TypeMap,
@@ -67,7 +68,7 @@ func resourceGoogleProjectDefaultServiceAccountsDoAction(d *schema.ResourceData,
6768
if err != nil {
6869
return err
6970
}
70-
71+
restorePolicy := d.Get("restore_policy").(string)
7172
serviceAccountSelfLink := fmt.Sprintf("projects/%s/serviceAccounts/%s", project, uniqueID)
7273
switch action {
7374
case "DELETE":
@@ -77,8 +78,14 @@ func resourceGoogleProjectDefaultServiceAccountsDoAction(d *schema.ResourceData,
7778
}
7879
case "UNDELETE":
7980
_, err := config.NewIamClient(userAgent).Projects.ServiceAccounts.Undelete(serviceAccountSelfLink, &iam.UndeleteServiceAccountRequest{}).Do()
80-
if err != nil {
81-
return fmt.Errorf("cannot undelete service account %s: %v", serviceAccountSelfLink, err)
81+
errExpected := restorePolicy == "REVERT_AND_IGNORE_FAILURE"
82+
errReceived := err != nil
83+
if errReceived {
84+
if !errExpected {
85+
return fmt.Errorf("cannot undelete service account %s: %v", serviceAccountSelfLink, err)
86+
}
87+
log.Printf("cannot undelete service account %s: %v", serviceAccountSelfLink, err)
88+
log.Printf("restore policy is %s... ignoring error", restorePolicy)
8289
}
8390
case "DISABLE":
8491
_, err := config.NewIamClient(userAgent).Projects.ServiceAccounts.Disable(serviceAccountSelfLink, &iam.DisableServiceAccountRequest{}).Do()
@@ -87,8 +94,14 @@ func resourceGoogleProjectDefaultServiceAccountsDoAction(d *schema.ResourceData,
8794
}
8895
case "ENABLE":
8996
_, err := config.NewIamClient(userAgent).Projects.ServiceAccounts.Enable(serviceAccountSelfLink, &iam.EnableServiceAccountRequest{}).Do()
90-
if err != nil {
91-
return fmt.Errorf("cannot enable service account %s: %v", serviceAccountSelfLink, err)
97+
errReceived := err != nil
98+
errExpected := restorePolicy == "REVERT_AND_IGNORE_FAILURE"
99+
if errReceived {
100+
if !errExpected {
101+
return fmt.Errorf("cannot enable service account %s: %v", serviceAccountSelfLink, err)
102+
}
103+
log.Printf("cannot enable service account %s: %v", serviceAccountSelfLink, err)
104+
log.Printf("restore policy is %s... ignoring error", restorePolicy)
92105
}
93106
case "DEPRIVILEGE":
94107
iamPolicy, err := config.NewResourceManagerClient(userAgent).Projects.GetIamPolicy(project, &cloudresourcemanager.GetIamPolicyRequest{}).Do()

google-beta/resource_google_project_default_service_accounts_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,34 @@ func TestAccResourceGoogleProjectDefaultServiceAccountsDelete(t *testing.T) {
109109
})
110110
}
111111

112+
func TestAccResourceGoogleProjectDefaultServiceAccountsDeleteRevertIgnoreFailure(t *testing.T) {
113+
t.Parallel()
114+
115+
org := getTestOrgFromEnv(t)
116+
project := fmt.Sprintf("tf-project-%d", randInt(t))
117+
billingAccount := getTestBillingAccountFromEnv(t)
118+
action := "DELETE"
119+
restorePolicy := "REVERT_AND_IGNORE_FAILURE"
120+
121+
vcrTest(t, resource.TestCase{
122+
PreCheck: func() { testAccPreCheck(t) },
123+
Providers: testAccProviders,
124+
Steps: []resource.TestStep{
125+
{
126+
Config: testAccCheckGoogleProjectDefaultServiceAccountsAdvanced(org, project, billingAccount, action, restorePolicy),
127+
Check: resource.ComposeTestCheckFunc(
128+
resource.TestCheckResourceAttr("google_project_default_service_accounts.acceptance", "id", "projects/"+project),
129+
resource.TestCheckResourceAttrSet("google_project_default_service_accounts.acceptance", "project"),
130+
resource.TestCheckResourceAttr("google_project_default_service_accounts.acceptance", "action", action),
131+
resource.TestCheckResourceAttrSet("google_project_default_service_accounts.acceptance", "project"),
132+
sleepInSecondsForTest(10),
133+
testAccCheckGoogleProjectDefaultServiceAccountsChanges(t, project, action),
134+
),
135+
},
136+
},
137+
})
138+
}
139+
112140
func TestAccResourceGoogleProjectDefaultServiceAccountsDeprivilege(t *testing.T) {
113141
t.Parallel()
114142

website/docs/r/google_project_default_service_accounts.html.markdown

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ The following arguments are supported:
4444

4545
- `action` - (Required) The action to be performed in the default service accounts. Valid values are: `DEPRIVILEGE`, `DELETE`, `DISABLE`. Note that `DEPRIVILEGE` action will ignore the REVERT configuration in the restore_policy
4646

47-
- `restore_policy` - (Optional) The action to be performed in the default service accounts on the resource destroy. Valid values are `NONE` and `REVERT`. If set to `REVERT` it will attempt to restore all default SAs but in the `DEPRIVILEGE` action.
47+
- `restore_policy` - (Optional) The action to be performed in the default service accounts on the resource destroy.
48+
Valid values are NONE, REVERT and REVERT_AND_IGNORE_FAILURE. It is applied for any action but in the DEPRIVILEGE.
49+
If set to REVERT it attempts to restore all default SAs but the DEPRIVILEGE action.
50+
If set to REVERT_AND_IGNORE_FAILURE it is the same behavior as REVERT but ignores errors returned by the API.
4851

4952
## Attributes Reference
5053

0 commit comments

Comments
 (0)