blob: 905b32cfe57aafda921fa1847fdf60a88eed6012 [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.
Jamie Hannaford90684242015-02-10 12:46:07 +0100112func ExtractInstances(page pagination.Page) ([]Instance, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600113 r := page.(InstancePage)
114 var s struct {
115 Instances []Instance `json:"instances"`
Jamie Hannaford90684242015-02-10 12:46:07 +0100116 }
Jon Perritt12395212016-02-24 10:41:17 -0600117 err := r.ExtractInto(&s)
118 return s.Instances, err
Jamie Hannaford90684242015-02-10 12:46:07 +0100119}
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100120
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100121// UserRootResult represents the result of an operation to enable the root user.
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100122type UserRootResult struct {
123 gophercloud.Result
124}
125
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100126// Extract will extract root user information from a UserRootResult.
Jamie Hannaford3aba0b12015-02-13 14:33:39 +0100127func (r UserRootResult) Extract() (*users.User, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600128 var s struct {
129 User *users.User `json:"user"`
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100130 }
Jon Perritt12395212016-02-24 10:41:17 -0600131 err := r.ExtractInto(&s)
132 return s.User, err
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100133}
Jamie Hannaford219ca592015-02-10 15:59:05 +0100134
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100135// ActionResult represents the result of action requests, such as: restarting
136// an instance service, resizing its memory allocation, and resizing its
137// attached volume size.
Jamie Hannaford219ca592015-02-10 15:59:05 +0100138type ActionResult struct {
139 gophercloud.ErrResult
140}