blob: 6936ecba84ee8817accd37761d4e3c418523eafa [file] [log] [blame]
Jamie Hannaford6e4d7952014-10-29 16:18:29 +01001package users
2
3import (
Jamie Hannaford14e44c92014-10-30 12:12:54 +01004 "strconv"
5
Paul Quernafdc369a2014-10-31 11:50:20 -07006 "github.com/rackspace/gophercloud"
Jamie Hannaford6e4d7952014-10-29 16:18:29 +01007 os "github.com/rackspace/gophercloud/openstack/identity/v2/users"
8
9 "github.com/mitchellh/mapstructure"
10)
11
12// User represents a user resource that exists on the API.
13type User struct {
14 // The UUID for this user.
15 ID string
16
17 // The human name for this user.
18 Name string
19
20 // The username for this user.
21 Username string
22
23 // Indicates whether the user is enabled (true) or disabled (false).
24 Enabled bool
25
26 // The email address for this user.
27 Email string
28
29 // The ID of the tenant to which this user belongs.
30 TenantID string `mapstructure:"tenant_id"`
31
32 // Specifies the default region for the user account. This value is inherited
33 // from the user administrator when the account is created.
34 DefaultRegion string `mapstructure:"RAX-AUTH:defaultRegion"`
35
36 // Identifies the domain that contains the user account. This value is
37 // inherited from the user administrator when the account is created.
38 DomainID string `mapstructure:"RAX-AUTH:domainId"`
39
40 // The password value that the user needs for authentication. If the Add user
41 // request included a password value, this attribute is not included in the
42 // response.
43 Password string `mapstructure:"OS-KSADM:password"`
44
45 // Indicates whether the user has enabled multi-factor authentication.
Jamie Hannaford14e44c92014-10-30 12:12:54 +010046 MultiFactorEnabled bool `mapstructure:"RAX-AUTH:multiFactorEnabled"`
Jamie Hannaford6e4d7952014-10-29 16:18:29 +010047}
48
49// CreateResult represents the result of a Create operation
50type CreateResult struct {
51 os.CreateResult
52}
53
54// GetResult represents the result of a Get operation
55type GetResult struct {
56 os.GetResult
57}
58
59// UpdateResult represents the result of an Update operation
60type UpdateResult struct {
61 os.UpdateResult
62}
63
64func commonExtract(resp interface{}, err error) (*User, error) {
65 if err != nil {
66 return nil, err
67 }
68
69 var respStruct struct {
70 User *User `json:"user"`
71 }
72
Jamie Hannaford14e44c92014-10-30 12:12:54 +010073 // Since the API returns a string instead of a bool, we need to hack the JSON
74 json := resp.(map[string]interface{})
75 user := json["user"].(map[string]interface{})
76 if s, ok := user["RAX-AUTH:multiFactorEnabled"].(string); ok && s != "" {
77 if b, err := strconv.ParseBool(s); err == nil {
78 user["RAX-AUTH:multiFactorEnabled"] = b
79 }
80 }
81
82 err = mapstructure.Decode(json, &respStruct)
Jamie Hannaford6e4d7952014-10-29 16:18:29 +010083
84 return respStruct.User, err
85}
86
87// Extract will get the Snapshot object out of the GetResult object.
88func (r GetResult) Extract() (*User, error) {
89 return commonExtract(r.Body, r.Err)
90}
91
92// Extract will get the Snapshot object out of the CreateResult object.
93func (r CreateResult) Extract() (*User, error) {
94 return commonExtract(r.Body, r.Err)
95}
96
97// Extract will get the Snapshot object out of the UpdateResult object.
98func (r UpdateResult) Extract() (*User, error) {
99 return commonExtract(r.Body, r.Err)
100}
Paul Quernafdc369a2014-10-31 11:50:20 -0700101
102// ResetAPIKeyResult represents the server response to the ResetAPIKey method.
103type ResetAPIKeyResult struct {
104 gophercloud.Result
105}
106
107// ResetAPIKeyValue represents an API Key that has been reset.
108type ResetAPIKeyValue struct {
109 // The Username for this API Key reset.
110 Username string `mapstructure:"username"`
111
112 // The new API Key for this user.
113 APIKey string `mapstructure:"apiKey"`
114}
115
116// Extract will get the Error or ResetAPIKeyValue object out of the ResetAPIKeyResult object.
117func (r ResetAPIKeyResult) Extract() (*ResetAPIKeyValue, error) {
118 if r.Err != nil {
119 return nil, r.Err
120 }
121
122 var response struct {
123 ResetAPIKeyValue ResetAPIKeyValue `mapstructure:"RAX-KSKEY:apiKeyCredentials"`
124 }
125
126 err := mapstructure.Decode(r.Body, &response)
127
128 return &response.ResetAPIKeyValue, err
129}