This repository has been archived by the owner on Nov 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
resource_openstack_identity_user_v3.go
287 lines (236 loc) · 6.97 KB
/
resource_openstack_identity_user_v3.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package openstack
import (
"fmt"
"log"
"github.com/gophercloud/gophercloud/openstack/identity/v3/users"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceIdentityUserV3() *schema.Resource {
return &schema.Resource{
Create: resourceIdentityUserV3Create,
Read: resourceIdentityUserV3Read,
Update: resourceIdentityUserV3Update,
Delete: resourceIdentityUserV3Delete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"default_project_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"domain_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"extra": {
Type: schema.TypeMap,
Optional: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"password": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
},
// The following are all specific options that must
// be bundled into user.Options
"ignore_change_password_upon_first_use": {
Type: schema.TypeBool,
Optional: true,
},
"ignore_password_expiry": {
Type: schema.TypeBool,
Optional: true,
},
"ignore_lockout_failure_attempts": {
Type: schema.TypeBool,
Optional: true,
},
"multi_factor_auth_enabled": {
Type: schema.TypeBool,
Optional: true,
},
"multi_factor_auth_rule": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"rule": {
Type: schema.TypeList,
MinItems: 1,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
},
}
}
func resourceIdentityUserV3Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
identityClient, err := config.identityV3Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack identity client: %s", err)
}
enabled := d.Get("enabled").(bool)
createOpts := users.CreateOpts{
DefaultProjectID: d.Get("default_project_id").(string),
Description: d.Get("description").(string),
DomainID: d.Get("domain_id").(string),
Enabled: &enabled,
Extra: d.Get("extra").(map[string]interface{}),
Name: d.Get("name").(string),
}
// Build the user options
options := map[users.Option]interface{}{}
for optionType, option := range userOptions {
if v, ok := d.GetOk(option); ok {
options[optionType] = v.(bool)
}
}
// Build the MFA rules
mfaRules := expandIdentityUserV3MFARules(d.Get("multi_factor_auth_rule").([]interface{}))
if len(mfaRules) > 0 {
options[users.MultiFactorAuthRules] = mfaRules
}
createOpts.Options = options
log.Printf("[DEBUG] openstack_identity_user_v3 create options: %#v", createOpts)
// Add password here so it wouldn't go in the above log entry
createOpts.Password = d.Get("password").(string)
user, err := users.Create(identityClient, createOpts).Extract()
if err != nil {
return fmt.Errorf("Error creating openstack_identity_user_v3: %s", err)
}
d.SetId(user.ID)
return resourceIdentityUserV3Read(d, meta)
}
func resourceIdentityUserV3Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
identityClient, err := config.identityV3Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack identity client: %s", err)
}
user, err := users.Get(identityClient, d.Id()).Extract()
if err != nil {
return CheckDeleted(d, err, "Error retrieving openstack_identity_user_v3")
}
log.Printf("[DEBUG] Retrieved openstack_identity_user_v3 %s: %#v", d.Id(), user)
d.Set("default_project_id", user.DefaultProjectID)
d.Set("description", user.Description)
d.Set("domain_id", user.DomainID)
d.Set("enabled", user.Enabled)
d.Set("extra", user.Extra)
d.Set("name", user.Name)
d.Set("region", GetRegion(d, config))
// Check and see if any options match those defined in the schema.
options := user.Options
for _, option := range userOptions {
if v, ok := options[option]; ok {
d.Set(option, v.(bool))
}
}
if v, ok := options["multi_factor_auth_rules"].([]interface{}); ok {
mfaRules := flattenIdentityUserV3MFARules(v)
d.Set("multi_factor_auth_rule", mfaRules)
}
return nil
}
func resourceIdentityUserV3Update(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
identityClient, err := config.identityV3Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack identity client: %s", err)
}
var hasChange bool
var updateOpts users.UpdateOpts
if d.HasChange("default_project_id") {
hasChange = true
updateOpts.DefaultProjectID = d.Get("default_project_id").(string)
}
if d.HasChange("description") {
hasChange = true
description := d.Get("description").(string)
updateOpts.Description = &description
}
if d.HasChange("domain_id") {
hasChange = true
updateOpts.DomainID = d.Get("domain_id").(string)
}
if d.HasChange("enabled") {
hasChange = true
enabled := d.Get("enabled").(bool)
updateOpts.Enabled = &enabled
}
if d.HasChange("extra") {
hasChange = true
updateOpts.Extra = d.Get("extra").(map[string]interface{})
}
if d.HasChange("name") {
hasChange = true
updateOpts.Name = d.Get("name").(string)
}
// Determine if the options have changed
options := map[users.Option]interface{}{}
for optionType, option := range userOptions {
if d.HasChange(option) {
hasChange = true
options[optionType] = d.Get(option).(bool)
}
}
// Build the MFA rules
if d.HasChange("multi_factor_auth_rule") {
mfaRules := expandIdentityUserV3MFARules(d.Get("multi_factor_auth_rule").([]interface{}))
if len(mfaRules) > 0 {
options[users.MultiFactorAuthRules] = mfaRules
}
}
updateOpts.Options = options
if hasChange {
log.Printf("[DEBUG] openstack_identity_user_v3 %s update options: %#v", d.Id(), updateOpts)
}
if d.HasChange("password") {
hasChange = true
updateOpts.Password = d.Get("password").(string)
}
if hasChange {
_, err := users.Update(identityClient, d.Id(), updateOpts).Extract()
if err != nil {
return fmt.Errorf("Error updating openstack_identity_user_v3 %s: %s", d.Id(), err)
}
}
return resourceIdentityUserV3Read(d, meta)
}
func resourceIdentityUserV3Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
identityClient, err := config.identityV3Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack identity client: %s", err)
}
err = users.Delete(identityClient, d.Id()).ExtractErr()
if err != nil {
return CheckDeleted(d, err, "Error deleting openstack_identity_user_v3")
}
return nil
}