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 | |
| 18 | Host string |
| 19 | |
| 20 | // The databases associated with this user |
| 21 | Databases []db.Database |
| 22 | } |
| 23 | |
| 24 | type UpdatePasswordsResult struct { |
| 25 | gophercloud.ErrResult |
| 26 | } |
| 27 | |
| 28 | type UpdateResult struct { |
| 29 | gophercloud.ErrResult |
| 30 | } |
| 31 | |
| 32 | type GetResult struct { |
| 33 | gophercloud.Result |
| 34 | } |
| 35 | |
| 36 | func (r GetResult) Extract() (*User, error) { |
| 37 | if r.Err != nil { |
| 38 | return nil, r.Err |
| 39 | } |
| 40 | |
| 41 | var response struct { |
| 42 | User User `mapstructure:"user"` |
| 43 | } |
| 44 | |
| 45 | err := mapstructure.Decode(r.Body, &response) |
| 46 | return &response.User, err |
| 47 | } |
| 48 | |
| 49 | // AccessPage represents a single page of a paginated user collection. |
| 50 | type AccessPage struct { |
| 51 | pagination.LinkedPageBase |
| 52 | } |
| 53 | |
| 54 | // IsEmpty checks to see whether the collection is empty. |
| 55 | func (page AccessPage) IsEmpty() (bool, error) { |
| 56 | users, err := ExtractDBs(page) |
| 57 | if err != nil { |
| 58 | return true, err |
| 59 | } |
| 60 | return len(users) == 0, nil |
| 61 | } |
| 62 | |
| 63 | // NextPageURL will retrieve the next page URL. |
| 64 | func (page AccessPage) NextPageURL() (string, error) { |
| 65 | type resp struct { |
| 66 | Links []gophercloud.Link `mapstructure:"databases_links"` |
| 67 | } |
| 68 | |
| 69 | var r resp |
| 70 | err := mapstructure.Decode(page.Body, &r) |
| 71 | if err != nil { |
| 72 | return "", err |
| 73 | } |
| 74 | |
| 75 | return gophercloud.ExtractNextURL(r.Links) |
| 76 | } |
| 77 | |
| 78 | // ExtractDBs will convert a generic pagination struct into a more |
| 79 | // relevant slice of DB structs. |
| 80 | func ExtractDBs(page pagination.Page) ([]db.Database, error) { |
| 81 | casted := page.(AccessPage).Body |
| 82 | |
| 83 | var response struct { |
| 84 | DBs []db.Database `mapstructure:"databases"` |
| 85 | } |
| 86 | |
| 87 | err := mapstructure.Decode(casted, &response) |
| 88 | return response.DBs, err |
| 89 | } |
| 90 | |
| 91 | type GrantAccessResult struct { |
| 92 | gophercloud.ErrResult |
| 93 | } |
| 94 | |
| 95 | type RevokeAccessResult struct { |
| 96 | gophercloud.ErrResult |
| 97 | } |