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 | |
Jamie Hannaford | 6e4d795 | 2014-10-29 16:18:29 +0100 | [diff] [blame] | 6 | 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. |
| 12 | type 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 Hannaford | 14e44c9 | 2014-10-30 12:12:54 +0100 | [diff] [blame] | 45 | MultiFactorEnabled bool `mapstructure:"RAX-AUTH:multiFactorEnabled"` |
Jamie Hannaford | 6e4d795 | 2014-10-29 16:18:29 +0100 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | // CreateResult represents the result of a Create operation |
| 49 | type CreateResult struct { |
| 50 | os.CreateResult |
| 51 | } |
| 52 | |
| 53 | // GetResult represents the result of a Get operation |
| 54 | type GetResult struct { |
| 55 | os.GetResult |
| 56 | } |
| 57 | |
| 58 | // UpdateResult represents the result of an Update operation |
| 59 | type UpdateResult struct { |
| 60 | os.UpdateResult |
| 61 | } |
| 62 | |
| 63 | func 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 Hannaford | 14e44c9 | 2014-10-30 12:12:54 +0100 | [diff] [blame] | 72 | // 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 Hannaford | 6e4d795 | 2014-10-29 16:18:29 +0100 | [diff] [blame] | 82 | |
| 83 | return respStruct.User, err |
| 84 | } |
| 85 | |
| 86 | // Extract will get the Snapshot object out of the GetResult object. |
| 87 | func (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. |
| 92 | func (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. |
| 97 | func (r UpdateResult) Extract() (*User, error) { |
| 98 | return commonExtract(r.Body, r.Err) |
| 99 | } |