Jamie Hannaford | 6e4d795 | 2014-10-29 16:18:29 +0100 | [diff] [blame] | 1 | package users |
| 2 | |
| 3 | import ( |
Jamie Hannaford | 14e44c9 | 2014-10-30 12:12:54 +0100 | [diff] [blame] | 4 | "strconv" |
| 5 | |
Paul Querna | fdc369a | 2014-10-31 11:50:20 -0700 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud" |
Jamie Hannaford | 6e4d795 | 2014-10-29 16:18:29 +0100 | [diff] [blame] | 7 | 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. |
| 13 | type 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 Hannaford | 14e44c9 | 2014-10-30 12:12:54 +0100 | [diff] [blame] | 46 | MultiFactorEnabled bool `mapstructure:"RAX-AUTH:multiFactorEnabled"` |
Jamie Hannaford | 6e4d795 | 2014-10-29 16:18:29 +0100 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | // CreateResult represents the result of a Create operation |
| 50 | type CreateResult struct { |
| 51 | os.CreateResult |
| 52 | } |
| 53 | |
| 54 | // GetResult represents the result of a Get operation |
| 55 | type GetResult struct { |
| 56 | os.GetResult |
| 57 | } |
| 58 | |
| 59 | // UpdateResult represents the result of an Update operation |
| 60 | type UpdateResult struct { |
| 61 | os.UpdateResult |
| 62 | } |
| 63 | |
| 64 | func 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 Hannaford | 14e44c9 | 2014-10-30 12:12:54 +0100 | [diff] [blame] | 73 | // 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 Hannaford | 6e4d795 | 2014-10-29 16:18:29 +0100 | [diff] [blame] | 83 | |
| 84 | return respStruct.User, err |
| 85 | } |
| 86 | |
| 87 | // Extract will get the Snapshot object out of the GetResult object. |
| 88 | func (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. |
| 93 | func (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. |
| 98 | func (r UpdateResult) Extract() (*User, error) { |
| 99 | return commonExtract(r.Body, r.Err) |
| 100 | } |
Paul Querna | fdc369a | 2014-10-31 11:50:20 -0700 | [diff] [blame] | 101 | |
| 102 | // ResetAPIKeyResult represents the server response to the ResetAPIKey method. |
| 103 | type ResetAPIKeyResult struct { |
| 104 | gophercloud.Result |
| 105 | } |
| 106 | |
| 107 | // ResetAPIKeyValue represents an API Key that has been reset. |
| 108 | type 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. |
| 117 | func (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 | } |