Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 1 | package users |
| 2 | |
| 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
| 5 | "github.com/rackspace/gophercloud" |
| 6 | db "github.com/rackspace/gophercloud/openstack/db/v1/databases" |
| 7 | "github.com/rackspace/gophercloud/pagination" |
| 8 | ) |
| 9 | |
| 10 | // User represents a database user |
| 11 | type User struct { |
| 12 | // The user name |
| 13 | Name string |
| 14 | |
| 15 | // The user password |
| 16 | Password string |
| 17 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 18 | // Specifies the host from which a user is allowed to connect to the database. |
| 19 | // Possible values are a string containing an IPv4 address or "%" to allow |
| 20 | // connecting from any host. |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 21 | Host string |
| 22 | |
| 23 | // The databases associated with this user |
| 24 | Databases []db.Database |
| 25 | } |
| 26 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 27 | // UpdatePasswordsResult represents the result of changing a user password. |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 28 | type UpdatePasswordsResult struct { |
| 29 | gophercloud.ErrResult |
| 30 | } |
| 31 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 32 | // UpdateResult represents the result of updating a user. |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 33 | type UpdateResult struct { |
| 34 | gophercloud.ErrResult |
| 35 | } |
| 36 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 37 | // GetResult represents the result of getting a user. |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 38 | type GetResult struct { |
| 39 | gophercloud.Result |
| 40 | } |
| 41 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 42 | // Extract will retrieve a User struct from a getresult. |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 43 | func (r GetResult) Extract() (*User, error) { |
| 44 | if r.Err != nil { |
| 45 | return nil, r.Err |
| 46 | } |
| 47 | |
| 48 | var response struct { |
| 49 | User User `mapstructure:"user"` |
| 50 | } |
| 51 | |
| 52 | err := mapstructure.Decode(r.Body, &response) |
| 53 | return &response.User, err |
| 54 | } |
| 55 | |
| 56 | // AccessPage represents a single page of a paginated user collection. |
| 57 | type AccessPage struct { |
| 58 | pagination.LinkedPageBase |
| 59 | } |
| 60 | |
| 61 | // IsEmpty checks to see whether the collection is empty. |
| 62 | func (page AccessPage) IsEmpty() (bool, error) { |
| 63 | users, err := ExtractDBs(page) |
| 64 | if err != nil { |
| 65 | return true, err |
| 66 | } |
| 67 | return len(users) == 0, nil |
| 68 | } |
| 69 | |
| 70 | // NextPageURL will retrieve the next page URL. |
| 71 | func (page AccessPage) NextPageURL() (string, error) { |
| 72 | type resp struct { |
| 73 | Links []gophercloud.Link `mapstructure:"databases_links"` |
| 74 | } |
| 75 | |
| 76 | var r resp |
| 77 | err := mapstructure.Decode(page.Body, &r) |
| 78 | if err != nil { |
| 79 | return "", err |
| 80 | } |
| 81 | |
| 82 | return gophercloud.ExtractNextURL(r.Links) |
| 83 | } |
| 84 | |
| 85 | // ExtractDBs will convert a generic pagination struct into a more |
| 86 | // relevant slice of DB structs. |
| 87 | func ExtractDBs(page pagination.Page) ([]db.Database, error) { |
| 88 | casted := page.(AccessPage).Body |
| 89 | |
| 90 | var response struct { |
| 91 | DBs []db.Database `mapstructure:"databases"` |
| 92 | } |
| 93 | |
| 94 | err := mapstructure.Decode(casted, &response) |
| 95 | return response.DBs, err |
| 96 | } |
| 97 | |
Jamie Hannaford | 1b2f8cb | 2015-10-16 12:25:07 +0200 | [diff] [blame] | 98 | // UserPage represents a single page of a paginated user collection. |
| 99 | type UserPage struct { |
| 100 | pagination.LinkedPageBase |
| 101 | } |
| 102 | |
| 103 | // IsEmpty checks to see whether the collection is empty. |
| 104 | func (page UserPage) IsEmpty() (bool, error) { |
| 105 | users, err := ExtractUsers(page) |
| 106 | if err != nil { |
| 107 | return true, err |
| 108 | } |
| 109 | return len(users) == 0, nil |
| 110 | } |
| 111 | |
| 112 | // NextPageURL will retrieve the next page URL. |
| 113 | func (page UserPage) NextPageURL() (string, error) { |
| 114 | type resp struct { |
| 115 | Links []gophercloud.Link `mapstructure:"users_links"` |
| 116 | } |
| 117 | |
| 118 | var r resp |
| 119 | err := mapstructure.Decode(page.Body, &r) |
| 120 | if err != nil { |
| 121 | return "", err |
| 122 | } |
| 123 | |
| 124 | return gophercloud.ExtractNextURL(r.Links) |
| 125 | } |
| 126 | |
| 127 | // ExtractUsers will convert a generic pagination struct into a more |
| 128 | // relevant slice of User structs. |
| 129 | func ExtractUsers(page pagination.Page) ([]User, error) { |
| 130 | casted := page.(UserPage).Body |
| 131 | |
| 132 | var response struct { |
| 133 | Users []User `mapstructure:"users"` |
| 134 | } |
| 135 | |
| 136 | err := mapstructure.Decode(casted, &response) |
| 137 | |
| 138 | return response.Users, err |
| 139 | } |
| 140 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 141 | // GrantAccessResult represents the result of granting access to a user. |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 142 | type GrantAccessResult struct { |
| 143 | gophercloud.ErrResult |
| 144 | } |
| 145 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 146 | // RevokeAccessResult represents the result of revoking access to a user. |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 147 | type RevokeAccessResult struct { |
| 148 | gophercloud.ErrResult |
| 149 | } |