blob: b26fb2961d6067ae5898e4a83a8d11811f881ec9 [file] [log] [blame]
Jamie Hannaford9fdda582015-02-10 12:15:43 +01001package instances
2
3import (
esalipe58d15022017-01-13 19:31:08 +02004 "encoding/json"
Jamie Hannaforde65ad952015-11-16 14:05:11 +01005 "time"
6
Jon Perritt27249f42016-02-18 10:35:59 -06007 "github.com/gophercloud/gophercloud"
8 "github.com/gophercloud/gophercloud/openstack/db/v1/datastores"
Jon Perritt27249f42016-02-18 10:35:59 -06009 "github.com/gophercloud/gophercloud/openstack/db/v1/users"
10 "github.com/gophercloud/gophercloud/pagination"
Jamie Hannaford9fdda582015-02-10 12:15:43 +010011)
12
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010013// Volume represents information about an attached volume for a database instance.
Jamie Hannaford9fdda582015-02-10 12:15:43 +010014type Volume struct {
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010015 // The size in GB of the volume
Jamie Hannaford9fdda582015-02-10 12:15:43 +010016 Size int
Jamie Hannaford76e177b2015-02-16 16:53:00 +010017
18 Used float64
Jamie Hannaford9fdda582015-02-10 12:15:43 +010019}
20
esalipef7b8b062017-01-19 02:08:59 +020021// Flavor represents (virtual) hardware configurations for server resources in a region.
22type Flavor struct {
23 // The flavor's unique identifier.
24 ID string
25 // Links to access the flavor.
26 Links []gophercloud.Link
27}
28
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010029// Instance represents a remote MySQL instance.
Jamie Hannaford9fdda582015-02-10 12:15:43 +010030type Instance struct {
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010031 // Indicates the datetime that the instance was created
esalipe58d15022017-01-13 19:31:08 +020032 Created time.Time `json:"-"`
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010033
34 // Indicates the most recent datetime that the instance was updated.
esalipe58d15022017-01-13 19:31:08 +020035 Updated time.Time `json:"-"`
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010036
37 // Indicates the hardware flavor the instance uses.
esalipef7b8b062017-01-19 02:08:59 +020038 Flavor Flavor
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010039
40 // A DNS-resolvable hostname associated with the database instance (rather
41 // than an IPv4 address). Since the hostname always resolves to the correct
42 // IP address of the database instance, this relieves the user from the task
43 // of maintaining the mapping. Note that although the IP address may likely
44 // change on resizing, migrating, and so forth, the hostname always resolves
45 // to the correct database instance.
Jamie Hannaford9fdda582015-02-10 12:15:43 +010046 Hostname string
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010047
48 // Indicates the unique identifier for the instance resource.
49 ID string
50
51 // Exposes various links that reference the instance resource.
52 Links []gophercloud.Link
53
54 // The human-readable name of the instance.
55 Name string
56
57 // The build status of the instance.
58 Status string
59
60 // Information about the attached volume of the instance.
61 Volume Volume
Jamie Hannaford99eced52015-03-02 15:24:22 +010062
63 // Indicates how the instance stores data.
64 Datastore datastores.DatastorePartial
Jamie Hannaford9fdda582015-02-10 12:15:43 +010065}
66
esalipe58d15022017-01-13 19:31:08 +020067func (r *Instance) UnmarshalJSON(b []byte) error {
68 type tmp Instance
69 var s struct {
70 tmp
71 Created gophercloud.JSONRFC3339NoZ `json:"created"`
72 Updated gophercloud.JSONRFC3339NoZ `json:"updated"`
73 }
74 err := json.Unmarshal(b, &s)
75 if err != nil {
76 return err
77 }
78 *r = Instance(s.tmp)
79
80 r.Created = time.Time(s.Created)
81 r.Updated = time.Time(s.Updated)
82
83 return nil
84}
85
Jamie Hannaford821015f2015-02-10 12:58:36 +010086type commonResult struct {
Jamie Hannaford9fdda582015-02-10 12:15:43 +010087 gophercloud.Result
88}
89
Jamie Hannaford821015f2015-02-10 12:58:36 +010090// CreateResult represents the result of a Create operation.
91type CreateResult struct {
92 commonResult
93}
94
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010095// GetResult represents the result of a Get operation.
Jamie Hannaford821015f2015-02-10 12:58:36 +010096type GetResult struct {
97 commonResult
98}
99
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100100// DeleteResult represents the result of a Delete operation.
Jamie Hannaford5b16b632015-02-10 13:36:23 +0100101type DeleteResult struct {
102 gophercloud.ErrResult
103}
104
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100105// Extract will extract an Instance from various result structs.
Jamie Hannaford821015f2015-02-10 12:58:36 +0100106func (r commonResult) Extract() (*Instance, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600107 var s struct {
108 Instance *Instance `json:"instance"`
Jamie Hannaford9fdda582015-02-10 12:15:43 +0100109 }
Jon Perritt12395212016-02-24 10:41:17 -0600110 err := r.ExtractInto(&s)
111 return s.Instance, err
Jamie Hannaford9fdda582015-02-10 12:15:43 +0100112}
Jamie Hannaford90684242015-02-10 12:46:07 +0100113
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100114// InstancePage represents a single page of a paginated instance collection.
Jamie Hannaford90684242015-02-10 12:46:07 +0100115type InstancePage struct {
116 pagination.LinkedPageBase
117}
118
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100119// IsEmpty checks to see whether the collection is empty.
Jamie Hannaford90684242015-02-10 12:46:07 +0100120func (page InstancePage) IsEmpty() (bool, error) {
121 instances, err := ExtractInstances(page)
Jon Perritt12395212016-02-24 10:41:17 -0600122 return len(instances) == 0, err
Jamie Hannaford90684242015-02-10 12:46:07 +0100123}
124
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100125// NextPageURL will retrieve the next page URL.
Jamie Hannaford90684242015-02-10 12:46:07 +0100126func (page InstancePage) NextPageURL() (string, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600127 var s struct {
128 Links []gophercloud.Link `json:"instances_links"`
Jamie Hannaford90684242015-02-10 12:46:07 +0100129 }
Jon Perritt12395212016-02-24 10:41:17 -0600130 err := page.ExtractInto(&s)
Jamie Hannaford90684242015-02-10 12:46:07 +0100131 if err != nil {
132 return "", err
133 }
Jon Perritt12395212016-02-24 10:41:17 -0600134 return gophercloud.ExtractNextURL(s.Links)
Jamie Hannaford90684242015-02-10 12:46:07 +0100135}
136
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100137// ExtractInstances will convert a generic pagination struct into a more
138// relevant slice of Instance structs.
Jon Perritt31b66462016-02-25 22:25:30 -0600139func ExtractInstances(r pagination.Page) ([]Instance, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600140 var s struct {
141 Instances []Instance `json:"instances"`
Jamie Hannaford90684242015-02-10 12:46:07 +0100142 }
Jon Perritt31b66462016-02-25 22:25:30 -0600143 err := (r.(InstancePage)).ExtractInto(&s)
Jon Perritt12395212016-02-24 10:41:17 -0600144 return s.Instances, err
Jamie Hannaford90684242015-02-10 12:46:07 +0100145}
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100146
Jon Perrittdb0ae142016-03-13 00:33:41 -0600147// EnableRootUserResult represents the result of an operation to enable the root user.
148type EnableRootUserResult struct {
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100149 gophercloud.Result
150}
151
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100152// Extract will extract root user information from a UserRootResult.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600153func (r EnableRootUserResult) Extract() (*users.User, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600154 var s struct {
155 User *users.User `json:"user"`
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100156 }
Jon Perritt12395212016-02-24 10:41:17 -0600157 err := r.ExtractInto(&s)
158 return s.User, err
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100159}
Jamie Hannaford219ca592015-02-10 15:59:05 +0100160
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100161// ActionResult represents the result of action requests, such as: restarting
162// an instance service, resizing its memory allocation, and resizing its
163// attached volume size.
Jamie Hannaford219ca592015-02-10 15:59:05 +0100164type ActionResult struct {
165 gophercloud.ErrResult
166}
Jon Perrittdb0ae142016-03-13 00:33:41 -0600167
168// IsRootEnabledResult is the result of a call to IsRootEnabled. To see if
169// root is enabled, call the type's Extract method.
170type IsRootEnabledResult struct {
171 gophercloud.Result
172}
173
174// Extract is used to extract the data from a IsRootEnabledResult.
175func (r IsRootEnabledResult) Extract() (bool, error) {
176 return r.Body.(map[string]interface{})["rootEnabled"] == true, r.Err
177}