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