blob: ae3b9e0cdb9d4343ad5d69fcd41fd3a7d30fb667 [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 Hannaford2ca55d82015-02-12 14:21:55 +01006 "github.com/rackspace/gophercloud/openstack/db/v1/users"
Jamie Hannaford90684242015-02-10 12:46:07 +01007 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford9fdda582015-02-10 12:15:43 +01008)
9
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010010// Flavor represents information about a hardware flavor for a database instance.
Jamie Hannaford9fdda582015-02-10 12:15:43 +010011type Flavor struct {
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010012 // The unique identifier for a flavor.
13 ID string
14
15 // Various links which allow a user to reference the flavor.
Jamie Hannaford9fdda582015-02-10 12:15:43 +010016 Links []gophercloud.Link
17}
18
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010019// Volume represents information about an attached volume for a database instance.
Jamie Hannaford9fdda582015-02-10 12:15:43 +010020type Volume struct {
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010021 // The size in GB of the volume
Jamie Hannaford9fdda582015-02-10 12:15:43 +010022 Size int
Jamie Hannaford76e177b2015-02-16 16:53:00 +010023
24 Used float64
Jamie Hannaford9fdda582015-02-10 12:15:43 +010025}
26
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010027// Instance represents a remote MySQL instance.
Jamie Hannaford9fdda582015-02-10 12:15:43 +010028type Instance struct {
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010029 // Indicates the datetime that the instance was created
30 Created string //time.Time
31
32 // Indicates the most recent datetime that the instance was updated.
33 Updated string //time.Time
34
35 // Indicates the hardware flavor the instance uses.
36 Flavor Flavor
37
38 // A DNS-resolvable hostname associated with the database instance (rather
39 // than an IPv4 address). Since the hostname always resolves to the correct
40 // IP address of the database instance, this relieves the user from the task
41 // of maintaining the mapping. Note that although the IP address may likely
42 // change on resizing, migrating, and so forth, the hostname always resolves
43 // to the correct database instance.
Jamie Hannaford9fdda582015-02-10 12:15:43 +010044 Hostname string
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010045
46 // Indicates the unique identifier for the instance resource.
47 ID string
48
49 // Exposes various links that reference the instance resource.
50 Links []gophercloud.Link
51
52 // The human-readable name of the instance.
53 Name string
54
55 // The build status of the instance.
56 Status string
57
58 // Information about the attached volume of the instance.
59 Volume Volume
Jamie Hannaford9fdda582015-02-10 12:15:43 +010060}
61
Jamie Hannaford821015f2015-02-10 12:58:36 +010062type commonResult struct {
Jamie Hannaford9fdda582015-02-10 12:15:43 +010063 gophercloud.Result
64}
65
Jamie Hannaford821015f2015-02-10 12:58:36 +010066// CreateResult represents the result of a Create operation.
67type CreateResult struct {
68 commonResult
69}
70
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010071// GetResult represents the result of a Get operation.
Jamie Hannaford821015f2015-02-10 12:58:36 +010072type GetResult struct {
73 commonResult
74}
75
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010076// DeleteResult represents the result of a Delete operation.
Jamie Hannaford5b16b632015-02-10 13:36:23 +010077type DeleteResult struct {
78 gophercloud.ErrResult
79}
80
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010081// Extract will extract an Instance from various result structs.
Jamie Hannaford821015f2015-02-10 12:58:36 +010082func (r commonResult) Extract() (*Instance, error) {
Jamie Hannaford9fdda582015-02-10 12:15:43 +010083 if r.Err != nil {
84 return nil, r.Err
85 }
86
87 var response struct {
88 Instance Instance `mapstructure:"instance"`
89 }
90
91 err := mapstructure.Decode(r.Body, &response)
92
93 return &response.Instance, err
94}
Jamie Hannaford90684242015-02-10 12:46:07 +010095
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010096// InstancePage represents a single page of a paginated instance collection.
Jamie Hannaford90684242015-02-10 12:46:07 +010097type InstancePage struct {
98 pagination.LinkedPageBase
99}
100
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100101// IsEmpty checks to see whether the collection is empty.
Jamie Hannaford90684242015-02-10 12:46:07 +0100102func (page InstancePage) IsEmpty() (bool, error) {
103 instances, err := ExtractInstances(page)
104 if err != nil {
105 return true, err
106 }
107 return len(instances) == 0, nil
108}
109
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100110// NextPageURL will retrieve the next page URL.
Jamie Hannaford90684242015-02-10 12:46:07 +0100111func (page InstancePage) NextPageURL() (string, error) {
112 type resp struct {
113 Links []gophercloud.Link `mapstructure:"instances_links"`
114 }
115
116 var r resp
117 err := mapstructure.Decode(page.Body, &r)
118 if err != nil {
119 return "", err
120 }
121
122 return gophercloud.ExtractNextURL(r.Links)
123}
124
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100125// ExtractInstances will convert a generic pagination struct into a more
126// relevant slice of Instance structs.
Jamie Hannaford90684242015-02-10 12:46:07 +0100127func ExtractInstances(page pagination.Page) ([]Instance, error) {
128 casted := page.(InstancePage).Body
129
130 var response struct {
131 Instances []Instance `mapstructure:"instances"`
132 }
133
134 err := mapstructure.Decode(casted, &response)
135
136 return response.Instances, err
137}
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100138
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100139// UserRootResult represents the result of an operation to enable the root user.
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100140type UserRootResult struct {
141 gophercloud.Result
142}
143
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100144// Extract will extract root user information from a UserRootResult.
Jamie Hannaford3aba0b12015-02-13 14:33:39 +0100145func (r UserRootResult) Extract() (*users.User, error) {
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100146 if r.Err != nil {
147 return nil, r.Err
148 }
149
150 var response struct {
Jamie Hannaford2ca55d82015-02-12 14:21:55 +0100151 User users.User `mapstructure:"user"`
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100152 }
153
154 err := mapstructure.Decode(r.Body, &response)
155
156 return &response.User, err
157}
Jamie Hannaford219ca592015-02-10 15:59:05 +0100158
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100159// ActionResult represents the result of action requests, such as: restarting
160// an instance service, resizing its memory allocation, and resizing its
161// attached volume size.
Jamie Hannaford219ca592015-02-10 15:59:05 +0100162type ActionResult struct {
163 gophercloud.ErrResult
164}