Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 1 | package users |
| 2 | |
| 3 | import ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
Krzysztof Szukiełojć | 24a29ce | 2017-05-07 14:24:02 +0200 | [diff] [blame^] | 5 | db "gerrit.mcp.mirantis.net/debian/gophercloud.git/openstack/db/v1/databases" |
| 6 | "gerrit.mcp.mirantis.net/debian/gophercloud.git/pagination" |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 7 | ) |
| 8 | |
| 9 | // User represents a database user |
| 10 | type User struct { |
| 11 | // The user name |
| 12 | Name string |
| 13 | |
| 14 | // The user password |
| 15 | Password string |
| 16 | |
| 17 | // The databases associated with this user |
| 18 | Databases []db.Database |
| 19 | } |
| 20 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 21 | // CreateResult represents the result of a create operation. |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 22 | type CreateResult struct { |
| 23 | gophercloud.ErrResult |
| 24 | } |
| 25 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 26 | // DeleteResult represents the result of a delete operation. |
| 27 | type DeleteResult struct { |
| 28 | gophercloud.ErrResult |
| 29 | } |
| 30 | |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 31 | // UserPage represents a single page of a paginated user collection. |
| 32 | type UserPage struct { |
| 33 | pagination.LinkedPageBase |
| 34 | } |
| 35 | |
| 36 | // IsEmpty checks to see whether the collection is empty. |
| 37 | func (page UserPage) IsEmpty() (bool, error) { |
| 38 | users, err := ExtractUsers(page) |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 39 | return len(users) == 0, err |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | // NextPageURL will retrieve the next page URL. |
| 43 | func (page UserPage) NextPageURL() (string, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 44 | var s struct { |
| 45 | Links []gophercloud.Link `json:"users_links"` |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 46 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 47 | err := page.ExtractInto(&s) |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 48 | if err != nil { |
| 49 | return "", err |
| 50 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 51 | return gophercloud.ExtractNextURL(s.Links) |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | // ExtractUsers will convert a generic pagination struct into a more |
| 55 | // relevant slice of User structs. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 56 | func ExtractUsers(r pagination.Page) ([]User, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 57 | var s struct { |
| 58 | Users []User `json:"users"` |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 59 | } |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 60 | err := (r.(UserPage)).ExtractInto(&s) |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 61 | return s.Users, err |
Jamie Hannaford | af4570f | 2015-02-12 13:33:25 +0100 | [diff] [blame] | 62 | } |