blob: 670060e77895bd873bcfb7a5f81e6ff3c731a58c [file] [log] [blame]
Jamie Hannaford6e4d7952014-10-29 16:18:29 +01001package users
2
3import (
Jamie Hannaford14e44c92014-10-30 12:12:54 +01004 "strconv"
5
Jamie Hannaford6e4d7952014-10-29 16:18:29 +01006 os "github.com/rackspace/gophercloud/openstack/identity/v2/users"
7
8 "github.com/mitchellh/mapstructure"
9)
10
11// User represents a user resource that exists on the API.
12type User struct {
13 // The UUID for this user.
14 ID string
15
16 // The human name for this user.
17 Name string
18
19 // The username for this user.
20 Username string
21
22 // Indicates whether the user is enabled (true) or disabled (false).
23 Enabled bool
24
25 // The email address for this user.
26 Email string
27
28 // The ID of the tenant to which this user belongs.
29 TenantID string `mapstructure:"tenant_id"`
30
31 // Specifies the default region for the user account. This value is inherited
32 // from the user administrator when the account is created.
33 DefaultRegion string `mapstructure:"RAX-AUTH:defaultRegion"`
34
35 // Identifies the domain that contains the user account. This value is
36 // inherited from the user administrator when the account is created.
37 DomainID string `mapstructure:"RAX-AUTH:domainId"`
38
39 // The password value that the user needs for authentication. If the Add user
40 // request included a password value, this attribute is not included in the
41 // response.
42 Password string `mapstructure:"OS-KSADM:password"`
43
44 // Indicates whether the user has enabled multi-factor authentication.
Jamie Hannaford14e44c92014-10-30 12:12:54 +010045 MultiFactorEnabled bool `mapstructure:"RAX-AUTH:multiFactorEnabled"`
Jamie Hannaford6e4d7952014-10-29 16:18:29 +010046}
47
48// CreateResult represents the result of a Create operation
49type CreateResult struct {
50 os.CreateResult
51}
52
53// GetResult represents the result of a Get operation
54type GetResult struct {
55 os.GetResult
56}
57
58// UpdateResult represents the result of an Update operation
59type UpdateResult struct {
60 os.UpdateResult
61}
62
63func commonExtract(resp interface{}, err error) (*User, error) {
64 if err != nil {
65 return nil, err
66 }
67
68 var respStruct struct {
69 User *User `json:"user"`
70 }
71
Jamie Hannaford14e44c92014-10-30 12:12:54 +010072 // Since the API returns a string instead of a bool, we need to hack the JSON
73 json := resp.(map[string]interface{})
74 user := json["user"].(map[string]interface{})
75 if s, ok := user["RAX-AUTH:multiFactorEnabled"].(string); ok && s != "" {
76 if b, err := strconv.ParseBool(s); err == nil {
77 user["RAX-AUTH:multiFactorEnabled"] = b
78 }
79 }
80
81 err = mapstructure.Decode(json, &respStruct)
Jamie Hannaford6e4d7952014-10-29 16:18:29 +010082
83 return respStruct.User, err
84}
85
86// Extract will get the Snapshot object out of the GetResult object.
87func (r GetResult) Extract() (*User, error) {
88 return commonExtract(r.Body, r.Err)
89}
90
91// Extract will get the Snapshot object out of the CreateResult object.
92func (r CreateResult) Extract() (*User, error) {
93 return commonExtract(r.Body, r.Err)
94}
95
96// Extract will get the Snapshot object out of the UpdateResult object.
97func (r UpdateResult) Extract() (*User, error) {
98 return commonExtract(r.Body, r.Err)
99}