blob: 21900d7aa3c5710c9ca587c9ba6fd92541bda14b [file] [log] [blame]
Jamie Hannaford9fdda582015-02-10 12:15:43 +01001package instances
2
3import (
Jamie Hannaforde65ad952015-11-16 14:05:11 +01004 "time"
5
Jon Perritt27249f42016-02-18 10:35:59 -06006 "github.com/gophercloud/gophercloud"
7 "github.com/gophercloud/gophercloud/openstack/db/v1/datastores"
8 "github.com/gophercloud/gophercloud/openstack/db/v1/flavors"
9 "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
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010021// Instance represents a remote MySQL instance.
Jamie Hannaford9fdda582015-02-10 12:15:43 +010022type Instance struct {
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010023 // Indicates the datetime that the instance was created
Jon Perritt12395212016-02-24 10:41:17 -060024 Created time.Time `json:"created"`
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010025
26 // Indicates the most recent datetime that the instance was updated.
Jon Perritt12395212016-02-24 10:41:17 -060027 Updated time.Time `json:"updated"`
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010028
29 // Indicates the hardware flavor the instance uses.
Jamie Hannaford9793d942015-02-18 15:13:20 +010030 Flavor flavors.Flavor
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010031
32 // A DNS-resolvable hostname associated with the database instance (rather
33 // than an IPv4 address). Since the hostname always resolves to the correct
34 // IP address of the database instance, this relieves the user from the task
35 // of maintaining the mapping. Note that although the IP address may likely
36 // change on resizing, migrating, and so forth, the hostname always resolves
37 // to the correct database instance.
Jamie Hannaford9fdda582015-02-10 12:15:43 +010038 Hostname string
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010039
40 // Indicates the unique identifier for the instance resource.
41 ID string
42
43 // Exposes various links that reference the instance resource.
44 Links []gophercloud.Link
45
46 // The human-readable name of the instance.
47 Name string
48
49 // The build status of the instance.
50 Status string
51
52 // Information about the attached volume of the instance.
53 Volume Volume
Jamie Hannaford99eced52015-03-02 15:24:22 +010054
55 // Indicates how the instance stores data.
56 Datastore datastores.DatastorePartial
Jamie Hannaford9fdda582015-02-10 12:15:43 +010057}
58
Jamie Hannaford821015f2015-02-10 12:58:36 +010059type commonResult struct {
Jamie Hannaford9fdda582015-02-10 12:15:43 +010060 gophercloud.Result
61}
62
Jamie Hannaford821015f2015-02-10 12:58:36 +010063// CreateResult represents the result of a Create operation.
64type CreateResult struct {
65 commonResult
66}
67
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010068// GetResult represents the result of a Get operation.
Jamie Hannaford821015f2015-02-10 12:58:36 +010069type GetResult struct {
70 commonResult
71}
72
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010073// DeleteResult represents the result of a Delete operation.
Jamie Hannaford5b16b632015-02-10 13:36:23 +010074type DeleteResult struct {
75 gophercloud.ErrResult
76}
77
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010078// Extract will extract an Instance from various result structs.
Jamie Hannaford821015f2015-02-10 12:58:36 +010079func (r commonResult) Extract() (*Instance, error) {
Jon Perritt12395212016-02-24 10:41:17 -060080 var s struct {
81 Instance *Instance `json:"instance"`
Jamie Hannaford9fdda582015-02-10 12:15:43 +010082 }
Jon Perritt12395212016-02-24 10:41:17 -060083 err := r.ExtractInto(&s)
84 return s.Instance, err
Jamie Hannaford9fdda582015-02-10 12:15:43 +010085}
Jamie Hannaford90684242015-02-10 12:46:07 +010086
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010087// InstancePage represents a single page of a paginated instance collection.
Jamie Hannaford90684242015-02-10 12:46:07 +010088type InstancePage struct {
89 pagination.LinkedPageBase
90}
91
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010092// IsEmpty checks to see whether the collection is empty.
Jamie Hannaford90684242015-02-10 12:46:07 +010093func (page InstancePage) IsEmpty() (bool, error) {
94 instances, err := ExtractInstances(page)
Jon Perritt12395212016-02-24 10:41:17 -060095 return len(instances) == 0, err
Jamie Hannaford90684242015-02-10 12:46:07 +010096}
97
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010098// NextPageURL will retrieve the next page URL.
Jamie Hannaford90684242015-02-10 12:46:07 +010099func (page InstancePage) NextPageURL() (string, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600100 var s struct {
101 Links []gophercloud.Link `json:"instances_links"`
Jamie Hannaford90684242015-02-10 12:46:07 +0100102 }
Jon Perritt12395212016-02-24 10:41:17 -0600103 err := page.ExtractInto(&s)
Jamie Hannaford90684242015-02-10 12:46:07 +0100104 if err != nil {
105 return "", err
106 }
Jon Perritt12395212016-02-24 10:41:17 -0600107 return gophercloud.ExtractNextURL(s.Links)
Jamie Hannaford90684242015-02-10 12:46:07 +0100108}
109
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100110// ExtractInstances will convert a generic pagination struct into a more
111// relevant slice of Instance structs.
Jon Perritt31b66462016-02-25 22:25:30 -0600112func ExtractInstances(r pagination.Page) ([]Instance, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600113 var s struct {
114 Instances []Instance `json:"instances"`
Jamie Hannaford90684242015-02-10 12:46:07 +0100115 }
Jon Perritt31b66462016-02-25 22:25:30 -0600116 err := (r.(InstancePage)).ExtractInto(&s)
Jon Perritt12395212016-02-24 10:41:17 -0600117 return s.Instances, err
Jamie Hannaford90684242015-02-10 12:46:07 +0100118}
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100119
Jon Perrittdb0ae142016-03-13 00:33:41 -0600120// EnableRootUserResult represents the result of an operation to enable the root user.
121type EnableRootUserResult struct {
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100122 gophercloud.Result
123}
124
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100125// Extract will extract root user information from a UserRootResult.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600126func (r EnableRootUserResult) Extract() (*users.User, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600127 var s struct {
128 User *users.User `json:"user"`
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100129 }
Jon Perritt12395212016-02-24 10:41:17 -0600130 err := r.ExtractInto(&s)
131 return s.User, err
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100132}
Jamie Hannaford219ca592015-02-10 15:59:05 +0100133
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100134// ActionResult represents the result of action requests, such as: restarting
135// an instance service, resizing its memory allocation, and resizing its
136// attached volume size.
Jamie Hannaford219ca592015-02-10 15:59:05 +0100137type ActionResult struct {
138 gophercloud.ErrResult
139}
Jon Perrittdb0ae142016-03-13 00:33:41 -0600140
141// IsRootEnabledResult is the result of a call to IsRootEnabled. To see if
142// root is enabled, call the type's Extract method.
143type IsRootEnabledResult struct {
144 gophercloud.Result
145}
146
147// Extract is used to extract the data from a IsRootEnabledResult.
148func (r IsRootEnabledResult) Extract() (bool, error) {
149 return r.Body.(map[string]interface{})["rootEnabled"] == true, r.Err
150}