Jamie Hannaford | 1943b38 | 2015-02-12 11:50:02 +0100 | [diff] [blame] | 1 | package databases |
| 2 | |
| 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
| 5 | "github.com/rackspace/gophercloud" |
| 6 | "github.com/rackspace/gophercloud/pagination" |
| 7 | ) |
| 8 | |
| 9 | type Database struct { |
| 10 | // Specifies the name of the MySQL DB. |
| 11 | Name string |
| 12 | |
| 13 | // Set of symbols and encodings. The default character set is utf8. |
| 14 | CharSet string |
| 15 | |
| 16 | // Set of rules for comparing characters in a character set. The default |
| 17 | // value for collate is utf8_general_ci. |
| 18 | Collate string |
| 19 | } |
| 20 | |
| 21 | type CreateResult struct { |
| 22 | gophercloud.Result |
| 23 | } |
| 24 | |
| 25 | // DBPage represents a single page of a paginated DB collection. |
| 26 | type DBPage struct { |
| 27 | pagination.LinkedPageBase |
| 28 | } |
| 29 | |
| 30 | // IsEmpty checks to see whether the collection is empty. |
| 31 | func (page DBPage) IsEmpty() (bool, error) { |
| 32 | dbs, err := ExtractDBs(page) |
| 33 | if err != nil { |
| 34 | return true, err |
| 35 | } |
| 36 | return len(dbs) == 0, nil |
| 37 | } |
| 38 | |
| 39 | // NextPageURL will retrieve the next page URL. |
| 40 | func (page DBPage) NextPageURL() (string, error) { |
| 41 | type resp struct { |
| 42 | Links []gophercloud.Link `mapstructure:"databases_links"` |
| 43 | } |
| 44 | |
| 45 | var r resp |
| 46 | err := mapstructure.Decode(page.Body, &r) |
| 47 | if err != nil { |
| 48 | return "", err |
| 49 | } |
| 50 | |
| 51 | return gophercloud.ExtractNextURL(r.Links) |
| 52 | } |
| 53 | |
| 54 | // ExtractDBs will convert a generic pagination struct into a more |
| 55 | // relevant slice of DB structs. |
| 56 | func ExtractDBs(page pagination.Page) ([]Database, error) { |
| 57 | casted := page.(DBPage).Body |
| 58 | |
| 59 | var response struct { |
| 60 | Databases []Database `mapstructure:"databases"` |
| 61 | } |
| 62 | |
| 63 | err := mapstructure.Decode(casted, &response) |
| 64 | return response.Databases, err |
| 65 | } |
| 66 | |
| 67 | type DeleteResult struct { |
| 68 | gophercloud.ErrResult |
| 69 | } |