Skip to content

Commit 8f83f66

Browse files
modular-magicianrileykarson
authored andcommitted
Mark the deleted field computed in google_project_iam_custom_role (hashicorp#199)
<!-- This change is generated by MagicModules. --> /cc @rileykarson
1 parent 9a5f9b3 commit 8f83f66

3 files changed

+45
-71
lines changed

google-beta/resource_google_project_iam_custom_role.go

+7-51
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ func resourceGoogleProjectIamCustomRole() *schema.Resource {
5454
},
5555
"deleted": {
5656
Type: schema.TypeBool,
57-
Optional: true,
58-
Default: false,
57+
Computed: true,
5958
},
6059
},
6160
}
@@ -69,10 +68,6 @@ func resourceGoogleProjectIamCustomRoleCreate(d *schema.ResourceData, meta inter
6968
return err
7069
}
7170

72-
if d.Get("deleted").(bool) {
73-
return fmt.Errorf("Cannot create a custom project role with a deleted state. `deleted` field should be false.")
74-
}
75-
7671
roleId := fmt.Sprintf("projects/%s/roles/%s", project, d.Get("role_id").(string))
7772
r, err := config.clientIAM.Projects.Roles.Get(roleId).Do()
7873
if err == nil {
@@ -140,50 +135,20 @@ func resourceGoogleProjectIamCustomRoleUpdate(d *schema.ResourceData, meta inter
140135

141136
d.Partial(true)
142137

143-
if d.Get("deleted").(bool) {
144-
if d.HasChange("deleted") {
145-
// If other fields were changed, we need to update those first and then delete.
146-
// If we don't update, we will get diffs from re-apply
147-
// If we delete and then try to update, we will get an error.
148-
if err := resourceGoogleProjectIamCustomRoleUpdateNonDeletedFields(d, meta); err != nil {
149-
return err
150-
}
151-
if err := resourceGoogleProjectIamCustomRoleDelete(d, meta); err != nil {
152-
return err
153-
}
154-
155-
d.SetPartial("deleted")
156-
d.Partial(false)
157-
return nil
158-
} else {
159-
return fmt.Errorf("cannot make changes to deleted custom project role %s", d.Id())
160-
}
161-
}
162-
163138
// We want to update the role to some undeleted state.
164139
// Make sure the role with given ID exists and is un-deleted before patching.
165140
r, err := config.clientIAM.Projects.Roles.Get(d.Id()).Do()
166141
if err != nil {
167142
return fmt.Errorf("unable to find custom project role %s to update: %v", d.Id(), err)
168143
}
169144
if r.Deleted {
170-
// Undelete if deleted previously
171-
if err := resourceGoogleProjectIamCustomRoleUndelete(d, meta); err != nil {
172-
return err
145+
_, err := config.clientIAM.Projects.Roles.Undelete(d.Id(), &iam.UndeleteRoleRequest{}).Do()
146+
if err != nil {
147+
return fmt.Errorf("Error undeleting the custom project role %s: %s", d.Get("title").(string), err)
173148
}
174-
d.SetPartial("deleted")
175-
}
176149

177-
if err := resourceGoogleProjectIamCustomRoleUpdateNonDeletedFields(d, meta); err != nil {
178-
return err
150+
d.SetPartial("deleted")
179151
}
180-
d.Partial(false)
181-
182-
return nil
183-
}
184-
185-
func resourceGoogleProjectIamCustomRoleUpdateNonDeletedFields(d *schema.ResourceData, meta interface{}) error {
186-
config := meta.(*Config)
187152

188153
if d.HasChange("title") || d.HasChange("description") || d.HasChange("stage") || d.HasChange("permissions") {
189154
_, err := config.clientIAM.Projects.Roles.Patch(d.Id(), &iam.Role{
@@ -201,6 +166,8 @@ func resourceGoogleProjectIamCustomRoleUpdateNonDeletedFields(d *schema.Resource
201166
d.SetPartial("stage")
202167
d.SetPartial("permissions")
203168
}
169+
170+
d.Partial(false)
204171
return nil
205172
}
206173

@@ -214,14 +181,3 @@ func resourceGoogleProjectIamCustomRoleDelete(d *schema.ResourceData, meta inter
214181

215182
return nil
216183
}
217-
218-
func resourceGoogleProjectIamCustomRoleUndelete(d *schema.ResourceData, meta interface{}) error {
219-
config := meta.(*Config)
220-
221-
_, err := config.clientIAM.Projects.Roles.Undelete(d.Id(), &iam.UndeleteRoleRequest{}).Do()
222-
if err != nil {
223-
return fmt.Errorf("Error undeleting the custom project role %s: %s", d.Get("title").(string), err)
224-
}
225-
226-
return nil
227-
}

google-beta/resource_google_project_iam_custom_role_test.go

+31-20
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func TestAccProjectIamCustomRole_undelete(t *testing.T) {
5252
Steps: []resource.TestStep{
5353
{
5454
Config: testAccCheckGoogleProjectIamCustomRole_basic(roleId),
55-
Check: resource.TestCheckResourceAttr("google_project_iam_custom_role.foo", "deleted", "false"),
55+
Check: testAccCheckGoogleProjectIamCustomRoleDeletionStatus("google_project_iam_custom_role.foo", false),
5656
},
5757
{
5858
ResourceName: "google_project_iam_custom_role.foo",
@@ -61,18 +61,15 @@ func TestAccProjectIamCustomRole_undelete(t *testing.T) {
6161
},
6262
// Soft-delete
6363
{
64-
Config: testAccCheckGoogleProjectIamCustomRole_deleted(roleId),
65-
Check: resource.TestCheckResourceAttr("google_project_iam_custom_role.foo", "deleted", "true"),
66-
},
67-
{
68-
ResourceName: "google_project_iam_custom_role.foo",
69-
ImportState: true,
70-
ImportStateVerify: true,
64+
Config: testAccCheckGoogleProjectIamCustomRole_basic(roleId),
65+
Check: testAccCheckGoogleProjectIamCustomRoleDeletionStatus("google_project_iam_custom_role.foo", true),
66+
Destroy: true,
7167
},
68+
// Terraform doesn't have a config because of Destroy: true, so an import step would fail
7269
// Undelete
7370
{
7471
Config: testAccCheckGoogleProjectIamCustomRole_basic(roleId),
75-
Check: resource.TestCheckResourceAttr("google_project_iam_custom_role.foo", "deleted", "false"),
72+
Check: testAccCheckGoogleProjectIamCustomRoleDeletionStatus("google_project_iam_custom_role.foo", false),
7673
},
7774
{
7875
ResourceName: "google_project_iam_custom_role.foo",
@@ -141,25 +138,39 @@ func testAccCheckGoogleProjectIamCustomRoleDestroy(s *terraform.State) error {
141138
return nil
142139
}
143140

144-
func testAccCheckGoogleProjectIamCustomRole_basic(roleId string) string {
145-
return fmt.Sprintf(`
146-
resource "google_project_iam_custom_role" "foo" {
147-
role_id = "%s"
148-
title = "My Custom Role"
149-
description = "foo"
150-
permissions = ["iam.roles.list"]
151-
}
152-
`, roleId)
141+
func testAccCheckGoogleProjectIamCustomRoleDeletionStatus(n string, deleted bool) resource.TestCheckFunc {
142+
return func(s *terraform.State) error {
143+
rs, ok := s.RootModule().Resources[n]
144+
if !ok {
145+
return fmt.Errorf("Not found: %s", n)
146+
}
147+
148+
if rs.Primary.ID == "" {
149+
return fmt.Errorf("No ID is set")
150+
}
151+
152+
config := testAccProvider.Meta().(*Config)
153+
role, err := config.clientIAM.Projects.Roles.Get(rs.Primary.ID).Do()
154+
155+
if err != nil {
156+
return err
157+
}
158+
159+
if deleted != role.Deleted {
160+
return fmt.Errorf("Incorrect deletion status. Expected %t, got %t", deleted, role.Deleted)
161+
}
162+
163+
return nil
164+
}
153165
}
154166

155-
func testAccCheckGoogleProjectIamCustomRole_deleted(roleId string) string {
167+
func testAccCheckGoogleProjectIamCustomRole_basic(roleId string) string {
156168
return fmt.Sprintf(`
157169
resource "google_project_iam_custom_role" "foo" {
158170
role_id = "%s"
159171
title = "My Custom Role"
160172
description = "foo"
161173
permissions = ["iam.roles.list"]
162-
deleted = true
163174
}
164175
`, roleId)
165176
}

website/docs/r/google_project_iam_custom_role.html.markdown

+7
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ The following arguments are supported:
5252

5353
* `description` - (Optional) A human-readable description for the role.
5454

55+
## Attributes Reference
56+
57+
In addition to the arguments listed above, the following computed attributes are
58+
exported:
59+
60+
* `deleted` - (Optional) The current deleted state of the role.
61+
5562
## Import
5663

5764
Customized IAM project role can be imported using their URI, e.g.

0 commit comments

Comments
 (0)