blob: 3537e6808f162e36a196f0dc82aa538569cb5699 [file] [log] [blame]
Jamie Hannaford984e9172015-02-13 14:34:03 +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 Host string
19
20 // The databases associated with this user
21 Databases []db.Database
22}
23
24type UpdatePasswordsResult struct {
25 gophercloud.ErrResult
26}
27
28type UpdateResult struct {
29 gophercloud.ErrResult
30}
31
32type GetResult struct {
33 gophercloud.Result
34}
35
36func (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.
50type AccessPage struct {
51 pagination.LinkedPageBase
52}
53
54// IsEmpty checks to see whether the collection is empty.
55func (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.
64func (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.
80func 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
91type GrantAccessResult struct {
92 gophercloud.ErrResult
93}
94
95type RevokeAccessResult struct {
96 gophercloud.ErrResult
97}