blob: f85db81dfd21a33bcc99ce085f9045d115a65a02 [file] [log] [blame]
Jamie Hannaford9fdda582015-02-10 12:15:43 +01001package instances
2
3import (
4 "github.com/mitchellh/mapstructure"
5 "github.com/rackspace/gophercloud"
Jamie Hannaford9793d942015-02-18 15:13:20 +01006 "github.com/rackspace/gophercloud/openstack/db/v1/flavors"
Jamie Hannaford2ca55d82015-02-12 14:21:55 +01007 "github.com/rackspace/gophercloud/openstack/db/v1/users"
Jamie Hannaford90684242015-02-10 12:46:07 +01008 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford9fdda582015-02-10 12:15:43 +01009)
10
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010011// Volume represents information about an attached volume for a database instance.
Jamie Hannaford9fdda582015-02-10 12:15:43 +010012type Volume struct {
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010013 // The size in GB of the volume
Jamie Hannaford9fdda582015-02-10 12:15:43 +010014 Size int
Jamie Hannaford76e177b2015-02-16 16:53:00 +010015
16 Used float64
Jamie Hannaford9fdda582015-02-10 12:15:43 +010017}
18
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010019// Instance represents a remote MySQL instance.
Jamie Hannaford9fdda582015-02-10 12:15:43 +010020type Instance struct {
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010021 // Indicates the datetime that the instance was created
22 Created string //time.Time
23
24 // Indicates the most recent datetime that the instance was updated.
25 Updated string //time.Time
26
27 // Indicates the hardware flavor the instance uses.
Jamie Hannaford9793d942015-02-18 15:13:20 +010028 Flavor flavors.Flavor
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010029
30 // A DNS-resolvable hostname associated with the database instance (rather
31 // than an IPv4 address). Since the hostname always resolves to the correct
32 // IP address of the database instance, this relieves the user from the task
33 // of maintaining the mapping. Note that although the IP address may likely
34 // change on resizing, migrating, and so forth, the hostname always resolves
35 // to the correct database instance.
Jamie Hannaford9fdda582015-02-10 12:15:43 +010036 Hostname string
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010037
38 // Indicates the unique identifier for the instance resource.
39 ID string
40
41 // Exposes various links that reference the instance resource.
42 Links []gophercloud.Link
43
44 // The human-readable name of the instance.
45 Name string
46
47 // The build status of the instance.
48 Status string
49
50 // Information about the attached volume of the instance.
51 Volume Volume
Jamie Hannaford9fdda582015-02-10 12:15:43 +010052}
53
Jamie Hannaford821015f2015-02-10 12:58:36 +010054type commonResult struct {
Jamie Hannaford9fdda582015-02-10 12:15:43 +010055 gophercloud.Result
56}
57
Jamie Hannaford821015f2015-02-10 12:58:36 +010058// CreateResult represents the result of a Create operation.
59type CreateResult struct {
60 commonResult
61}
62
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010063// GetResult represents the result of a Get operation.
Jamie Hannaford821015f2015-02-10 12:58:36 +010064type GetResult struct {
65 commonResult
66}
67
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010068// DeleteResult represents the result of a Delete operation.
Jamie Hannaford5b16b632015-02-10 13:36:23 +010069type DeleteResult struct {
70 gophercloud.ErrResult
71}
72
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010073// Extract will extract an Instance from various result structs.
Jamie Hannaford821015f2015-02-10 12:58:36 +010074func (r commonResult) Extract() (*Instance, error) {
Jamie Hannaford9fdda582015-02-10 12:15:43 +010075 if r.Err != nil {
76 return nil, r.Err
77 }
78
79 var response struct {
80 Instance Instance `mapstructure:"instance"`
81 }
82
83 err := mapstructure.Decode(r.Body, &response)
84
85 return &response.Instance, err
86}
Jamie Hannaford90684242015-02-10 12:46:07 +010087
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010088// InstancePage represents a single page of a paginated instance collection.
Jamie Hannaford90684242015-02-10 12:46:07 +010089type InstancePage struct {
90 pagination.LinkedPageBase
91}
92
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010093// IsEmpty checks to see whether the collection is empty.
Jamie Hannaford90684242015-02-10 12:46:07 +010094func (page InstancePage) IsEmpty() (bool, error) {
95 instances, err := ExtractInstances(page)
96 if err != nil {
97 return true, err
98 }
99 return len(instances) == 0, nil
100}
101
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100102// NextPageURL will retrieve the next page URL.
Jamie Hannaford90684242015-02-10 12:46:07 +0100103func (page InstancePage) NextPageURL() (string, error) {
104 type resp struct {
105 Links []gophercloud.Link `mapstructure:"instances_links"`
106 }
107
108 var r resp
109 err := mapstructure.Decode(page.Body, &r)
110 if err != nil {
111 return "", err
112 }
113
114 return gophercloud.ExtractNextURL(r.Links)
115}
116
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100117// ExtractInstances will convert a generic pagination struct into a more
118// relevant slice of Instance structs.
Jamie Hannaford90684242015-02-10 12:46:07 +0100119func ExtractInstances(page pagination.Page) ([]Instance, error) {
120 casted := page.(InstancePage).Body
121
122 var response struct {
123 Instances []Instance `mapstructure:"instances"`
124 }
125
126 err := mapstructure.Decode(casted, &response)
127
128 return response.Instances, err
129}
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100130
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100131// UserRootResult represents the result of an operation to enable the root user.
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100132type UserRootResult struct {
133 gophercloud.Result
134}
135
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100136// Extract will extract root user information from a UserRootResult.
Jamie Hannaford3aba0b12015-02-13 14:33:39 +0100137func (r UserRootResult) Extract() (*users.User, error) {
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100138 if r.Err != nil {
139 return nil, r.Err
140 }
141
142 var response struct {
Jamie Hannaford2ca55d82015-02-12 14:21:55 +0100143 User users.User `mapstructure:"user"`
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100144 }
145
146 err := mapstructure.Decode(r.Body, &response)
147
148 return &response.User, err
149}
Jamie Hannaford219ca592015-02-10 15:59:05 +0100150
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100151// ActionResult represents the result of action requests, such as: restarting
152// an instance service, resizing its memory allocation, and resizing its
153// attached volume size.
Jamie Hannaford219ca592015-02-10 15:59:05 +0100154type ActionResult struct {
155 gophercloud.ErrResult
156}