blob: ce07e761ba453994a07194523ed647f53d8cbc26 [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
22type CreateResult struct {
23 gophercloud.ErrResult
24}
25
26// UserPage represents a single page of a paginated user collection.
27type UserPage struct {
28 pagination.LinkedPageBase
29}
30
31// IsEmpty checks to see whether the collection is empty.
32func (page UserPage) IsEmpty() (bool, error) {
33 users, err := ExtractUsers(page)
34 if err != nil {
35 return true, err
36 }
37 return len(users) == 0, nil
38}
39
40// NextPageURL will retrieve the next page URL.
41func (page UserPage) NextPageURL() (string, error) {
42 type resp struct {
43 Links []gophercloud.Link `mapstructure:"users_links"`
44 }
45
46 var r resp
47 err := mapstructure.Decode(page.Body, &r)
48 if err != nil {
49 return "", err
50 }
51
52 return gophercloud.ExtractNextURL(r.Links)
53}
54
55// ExtractUsers will convert a generic pagination struct into a more
56// relevant slice of User structs.
57func ExtractUsers(page pagination.Page) ([]User, error) {
58 casted := page.(UserPage).Body
59
60 var response struct {
61 Users []User `mapstructure:"users"`
62 }
63
64 err := mapstructure.Decode(casted, &response)
65
66 return response.Users, err
67}
68
69type DeleteResult struct {
70 gophercloud.ErrResult
71}