blob: 3d4acb436d8575c11bddb408e4994b466616bc05 [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
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010018 // Specifies the host from which a user is allowed to connect to the database.
19 // Possible values are a string containing an IPv4 address or "%" to allow
20 // connecting from any host.
Jamie Hannaford984e9172015-02-13 14:34:03 +010021 Host string
22
23 // The databases associated with this user
24 Databases []db.Database
25}
26
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010027// UpdatePasswordsResult represents the result of changing a user password.
Jamie Hannaford984e9172015-02-13 14:34:03 +010028type UpdatePasswordsResult struct {
29 gophercloud.ErrResult
30}
31
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010032// UpdateResult represents the result of updating a user.
Jamie Hannaford984e9172015-02-13 14:34:03 +010033type UpdateResult struct {
34 gophercloud.ErrResult
35}
36
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010037// GetResult represents the result of getting a user.
Jamie Hannaford984e9172015-02-13 14:34:03 +010038type GetResult struct {
39 gophercloud.Result
40}
41
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010042// Extract will retrieve a User struct from a getresult.
Jamie Hannaford984e9172015-02-13 14:34:03 +010043func (r GetResult) Extract() (*User, error) {
44 if r.Err != nil {
45 return nil, r.Err
46 }
47
48 var response struct {
49 User User `mapstructure:"user"`
50 }
51
52 err := mapstructure.Decode(r.Body, &response)
53 return &response.User, err
54}
55
56// AccessPage represents a single page of a paginated user collection.
57type AccessPage struct {
58 pagination.LinkedPageBase
59}
60
61// IsEmpty checks to see whether the collection is empty.
62func (page AccessPage) IsEmpty() (bool, error) {
63 users, err := ExtractDBs(page)
64 if err != nil {
65 return true, err
66 }
67 return len(users) == 0, nil
68}
69
70// NextPageURL will retrieve the next page URL.
71func (page AccessPage) NextPageURL() (string, error) {
72 type resp struct {
73 Links []gophercloud.Link `mapstructure:"databases_links"`
74 }
75
76 var r resp
77 err := mapstructure.Decode(page.Body, &r)
78 if err != nil {
79 return "", err
80 }
81
82 return gophercloud.ExtractNextURL(r.Links)
83}
84
85// ExtractDBs will convert a generic pagination struct into a more
86// relevant slice of DB structs.
87func ExtractDBs(page pagination.Page) ([]db.Database, error) {
88 casted := page.(AccessPage).Body
89
90 var response struct {
91 DBs []db.Database `mapstructure:"databases"`
92 }
93
94 err := mapstructure.Decode(casted, &response)
95 return response.DBs, err
96}
97
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010098// GrantAccessResult represents the result of granting access to a user.
Jamie Hannaford984e9172015-02-13 14:34:03 +010099type GrantAccessResult struct {
100 gophercloud.ErrResult
101}
102
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100103// RevokeAccessResult represents the result of revoking access to a user.
Jamie Hannaford984e9172015-02-13 14:34:03 +0100104type RevokeAccessResult struct {
105 gophercloud.ErrResult
106}