blob: 217ddd8daf8b079b6af1d1f1e12843c83b05d34d [file] [log] [blame]
Jamie Hannafordaf4570f2015-02-12 13:33:25 +01001package users
2
3import (
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
11type User struct {
12 // The user name
13 Name string
14
15 // The user password
16 Password string
17
18 // The databases associated with this user
19 Databases []db.Database
20}
21
Jamie Hannaford9793d942015-02-18 15:13:20 +010022// CreateResult represents the result of a create operation.
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010023type CreateResult struct {
24 gophercloud.ErrResult
25}
26
Jamie Hannaford9793d942015-02-18 15:13:20 +010027// DeleteResult represents the result of a delete operation.
28type DeleteResult struct {
29 gophercloud.ErrResult
30}
31
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010032// UserPage represents a single page of a paginated user collection.
33type UserPage struct {
34 pagination.LinkedPageBase
35}
36
37// IsEmpty checks to see whether the collection is empty.
38func (page UserPage) IsEmpty() (bool, error) {
39 users, err := ExtractUsers(page)
40 if err != nil {
41 return true, err
42 }
43 return len(users) == 0, nil
44}
45
46// NextPageURL will retrieve the next page URL.
47func (page UserPage) NextPageURL() (string, error) {
48 type resp struct {
49 Links []gophercloud.Link `mapstructure:"users_links"`
50 }
51
52 var r resp
53 err := mapstructure.Decode(page.Body, &r)
54 if err != nil {
55 return "", err
56 }
57
58 return gophercloud.ExtractNextURL(r.Links)
59}
60
61// ExtractUsers will convert a generic pagination struct into a more
62// relevant slice of User structs.
63func ExtractUsers(page pagination.Page) ([]User, error) {
64 casted := page.(UserPage).Body
65
66 var response struct {
67 Users []User `mapstructure:"users"`
68 }
69
70 err := mapstructure.Decode(casted, &response)
71
72 return response.Users, err
73}