forked from hashicorp/terraform-provider-google
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_google_organization_iam_custom_role.go
171 lines (143 loc) · 4.72 KB
/
resource_google_organization_iam_custom_role.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package google
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"google.golang.org/api/iam/v1"
)
func resourceGoogleOrganizationIamCustomRole() *schema.Resource {
return &schema.Resource{
Create: resourceGoogleOrganizationIamCustomRoleCreate,
Read: resourceGoogleOrganizationIamCustomRoleRead,
Update: resourceGoogleOrganizationIamCustomRoleUpdate,
Delete: resourceGoogleOrganizationIamCustomRoleDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"role_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"org_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"title": {
Type: schema.TypeString,
Required: true,
},
"permissions": {
Type: schema.TypeSet,
Required: true,
MinItems: 1,
Elem: &schema.Schema{Type: schema.TypeString},
},
"stage": {
Type: schema.TypeString,
Optional: true,
Default: "GA",
ValidateFunc: validation.StringInSlice([]string{"ALPHA", "BETA", "GA", "DEPRECATED", "DISABLED", "EAP"}, false),
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"deleted": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
}
}
func resourceGoogleOrganizationIamCustomRoleCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
if d.Get("deleted").(bool) {
return fmt.Errorf("Cannot create a custom organization role with a deleted state. `deleted` field should be false.")
}
role, err := config.clientIAM.Organizations.Roles.Create("organizations/"+d.Get("org_id").(string), &iam.CreateRoleRequest{
RoleId: d.Get("role_id").(string),
Role: &iam.Role{
Title: d.Get("title").(string),
Description: d.Get("description").(string),
Stage: d.Get("stage").(string),
IncludedPermissions: convertStringSet(d.Get("permissions").(*schema.Set)),
},
}).Do()
if err != nil {
return fmt.Errorf("Error creating the custom organization role %s: %s", d.Get("title").(string), err)
}
d.SetId(role.Name)
return resourceGoogleOrganizationIamCustomRoleRead(d, meta)
}
func resourceGoogleOrganizationIamCustomRoleRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
role, err := config.clientIAM.Organizations.Roles.Get(d.Id()).Do()
if err != nil {
return handleNotFoundError(err, d, d.Id())
}
parsedRoleName, err := ParseOrganizationCustomRoleName(role.Name)
if err != nil {
return err
}
d.Set("role_id", parsedRoleName.Name)
d.Set("org_id", parsedRoleName.OrgId)
d.Set("title", role.Title)
d.Set("description", role.Description)
d.Set("permissions", role.IncludedPermissions)
d.Set("stage", role.Stage)
d.Set("deleted", role.Deleted)
return nil
}
func resourceGoogleOrganizationIamCustomRoleUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
d.Partial(true)
if d.HasChange("deleted") {
if d.Get("deleted").(bool) {
if err := resourceGoogleOrganizationIamCustomRoleDelete(d, meta); err != nil {
return err
}
} else {
if err := resourceGoogleOrganizationIamCustomRoleUndelete(d, meta); err != nil {
return err
}
}
d.SetPartial("deleted")
}
if d.HasChange("title") || d.HasChange("description") || d.HasChange("stage") || d.HasChange("permissions") {
_, err := config.clientIAM.Organizations.Roles.Patch(d.Id(), &iam.Role{
Title: d.Get("title").(string),
Description: d.Get("description").(string),
Stage: d.Get("stage").(string),
IncludedPermissions: convertStringSet(d.Get("permissions").(*schema.Set)),
}).Do()
if err != nil {
return fmt.Errorf("Error updating the custom organization role %s: %s", d.Get("title").(string), err)
}
d.SetPartial("title")
d.SetPartial("description")
d.SetPartial("stage")
d.SetPartial("permissions")
}
d.Partial(false)
return nil
}
func resourceGoogleOrganizationIamCustomRoleDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
_, err := config.clientIAM.Organizations.Roles.Delete(d.Id()).Do()
if err != nil {
return fmt.Errorf("Error deleting the custom organization role %s: %s", d.Get("title").(string), err)
}
return nil
}
func resourceGoogleOrganizationIamCustomRoleUndelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
_, err := config.clientIAM.Organizations.Roles.Undelete(d.Id(), &iam.UndeleteRoleRequest{}).Do()
if err != nil {
return fmt.Errorf("Error undeleting the custom organization role %s: %s", d.Get("title").(string), err)
}
return nil
}