blob: 91263294c0ebe487b7bd2b539b8b1f0a2b443766 [file] [log] [blame]
Jamie Hannaford1943b382015-02-12 11:50:02 +01001package databases
2
3import (
4 "github.com/mitchellh/mapstructure"
Jon Perritt27249f42016-02-18 10:35:59 -06005 "github.com/gophercloud/gophercloud"
6 "github.com/gophercloud/gophercloud/pagination"
Jamie Hannaford1943b382015-02-12 11:50:02 +01007)
8
Jamie Hannaford9793d942015-02-18 15:13:20 +01009// Database represents a Database API resource.
Jamie Hannaford1943b382015-02-12 11:50:02 +010010type Database struct {
Jamie Hannaford9793d942015-02-18 15:13:20 +010011 // Specifies the name of the MySQL database.
Jamie Hannaford1943b382015-02-12 11:50:02 +010012 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 Hannaford9793d942015-02-18 15:13:20 +010022// CreateResult represents the result of a Create operation.
Jamie Hannaford1943b382015-02-12 11:50:02 +010023type CreateResult struct {
Jamie Hannaford11108402015-02-23 10:31:41 +010024 gophercloud.ErrResult
Jamie Hannaford1943b382015-02-12 11:50:02 +010025}
26
Jamie Hannaford9793d942015-02-18 15:13:20 +010027// DeleteResult represents the result of a Delete operation.
28type DeleteResult struct {
29 gophercloud.ErrResult
30}
31
Jamie Hannaford1943b382015-02-12 11:50:02 +010032// DBPage represents a single page of a paginated DB collection.
33type DBPage struct {
34 pagination.LinkedPageBase
35}
36
37// IsEmpty checks to see whether the collection is empty.
38func (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.
47func (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.
63func 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}