Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 1 | package users |
| 2 | |
| 3 | import ( |
| 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 |
| 11 | type User struct { |
| 12 | // The user name |
| 13 | Name string |
| 14 | |
| 15 | // The user password |
| 16 | Password string |
| 17 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 18 | // 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 Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 21 | Host string |
| 22 | |
| 23 | // The databases associated with this user |
| 24 | Databases []db.Database |
| 25 | } |
| 26 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 27 | // UpdatePasswordsResult represents the result of changing a user password. |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 28 | type UpdatePasswordsResult struct { |
| 29 | gophercloud.ErrResult |
| 30 | } |
| 31 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 32 | // UpdateResult represents the result of updating a user. |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 33 | type UpdateResult struct { |
| 34 | gophercloud.ErrResult |
| 35 | } |
| 36 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 37 | // GetResult represents the result of getting a user. |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 38 | type GetResult struct { |
| 39 | gophercloud.Result |
| 40 | } |
| 41 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 42 | // Extract will retrieve a User struct from a getresult. |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 43 | func (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. |
| 57 | type AccessPage struct { |
| 58 | pagination.LinkedPageBase |
| 59 | } |
| 60 | |
| 61 | // IsEmpty checks to see whether the collection is empty. |
| 62 | func (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. |
| 71 | func (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. |
| 87 | func 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 Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 98 | // GrantAccessResult represents the result of granting access to a user. |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 99 | type GrantAccessResult struct { |
| 100 | gophercloud.ErrResult |
| 101 | } |
| 102 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 103 | // RevokeAccessResult represents the result of revoking access to a user. |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 104 | type RevokeAccessResult struct { |
| 105 | gophercloud.ErrResult |
| 106 | } |