blob: 6bfde15030e4a44dae4f15494f4e470819e4e40e [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
esalipe84e358b2017-01-19 02:19:23 +020048 // The IP addresses associated with the database instance
49 // Is empty if the instance has a hostname
50 IP []string
51
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010052 // Indicates the unique identifier for the instance resource.
53 ID string
54
55 // Exposes various links that reference the instance resource.
56 Links []gophercloud.Link
57
58 // The human-readable name of the instance.
59 Name string
60
61 // The build status of the instance.
62 Status string
63
64 // Information about the attached volume of the instance.
65 Volume Volume
Jamie Hannaford99eced52015-03-02 15:24:22 +010066
67 // Indicates how the instance stores data.
68 Datastore datastores.DatastorePartial
Jamie Hannaford9fdda582015-02-10 12:15:43 +010069}
70
esalipe58d15022017-01-13 19:31:08 +020071func (r *Instance) UnmarshalJSON(b []byte) error {
72 type tmp Instance
73 var s struct {
74 tmp
75 Created gophercloud.JSONRFC3339NoZ `json:"created"`
76 Updated gophercloud.JSONRFC3339NoZ `json:"updated"`
77 }
78 err := json.Unmarshal(b, &s)
79 if err != nil {
80 return err
81 }
82 *r = Instance(s.tmp)
83
84 r.Created = time.Time(s.Created)
85 r.Updated = time.Time(s.Updated)
86
87 return nil
88}
89
Jamie Hannaford821015f2015-02-10 12:58:36 +010090type commonResult struct {
Jamie Hannaford9fdda582015-02-10 12:15:43 +010091 gophercloud.Result
92}
93
Jamie Hannaford821015f2015-02-10 12:58:36 +010094// CreateResult represents the result of a Create operation.
95type CreateResult struct {
96 commonResult
97}
98
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010099// GetResult represents the result of a Get operation.
Jamie Hannaford821015f2015-02-10 12:58:36 +0100100type GetResult struct {
101 commonResult
102}
103
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100104// DeleteResult represents the result of a Delete operation.
Jamie Hannaford5b16b632015-02-10 13:36:23 +0100105type DeleteResult struct {
106 gophercloud.ErrResult
107}
108
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100109// Extract will extract an Instance from various result structs.
Jamie Hannaford821015f2015-02-10 12:58:36 +0100110func (r commonResult) Extract() (*Instance, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600111 var s struct {
112 Instance *Instance `json:"instance"`
Jamie Hannaford9fdda582015-02-10 12:15:43 +0100113 }
Jon Perritt12395212016-02-24 10:41:17 -0600114 err := r.ExtractInto(&s)
115 return s.Instance, err
Jamie Hannaford9fdda582015-02-10 12:15:43 +0100116}
Jamie Hannaford90684242015-02-10 12:46:07 +0100117
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100118// InstancePage represents a single page of a paginated instance collection.
Jamie Hannaford90684242015-02-10 12:46:07 +0100119type InstancePage struct {
120 pagination.LinkedPageBase
121}
122
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100123// IsEmpty checks to see whether the collection is empty.
Jamie Hannaford90684242015-02-10 12:46:07 +0100124func (page InstancePage) IsEmpty() (bool, error) {
125 instances, err := ExtractInstances(page)
Jon Perritt12395212016-02-24 10:41:17 -0600126 return len(instances) == 0, err
Jamie Hannaford90684242015-02-10 12:46:07 +0100127}
128
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100129// NextPageURL will retrieve the next page URL.
Jamie Hannaford90684242015-02-10 12:46:07 +0100130func (page InstancePage) NextPageURL() (string, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600131 var s struct {
132 Links []gophercloud.Link `json:"instances_links"`
Jamie Hannaford90684242015-02-10 12:46:07 +0100133 }
Jon Perritt12395212016-02-24 10:41:17 -0600134 err := page.ExtractInto(&s)
Jamie Hannaford90684242015-02-10 12:46:07 +0100135 if err != nil {
136 return "", err
137 }
Jon Perritt12395212016-02-24 10:41:17 -0600138 return gophercloud.ExtractNextURL(s.Links)
Jamie Hannaford90684242015-02-10 12:46:07 +0100139}
140
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100141// ExtractInstances will convert a generic pagination struct into a more
142// relevant slice of Instance structs.
Jon Perritt31b66462016-02-25 22:25:30 -0600143func ExtractInstances(r pagination.Page) ([]Instance, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600144 var s struct {
145 Instances []Instance `json:"instances"`
Jamie Hannaford90684242015-02-10 12:46:07 +0100146 }
Jon Perritt31b66462016-02-25 22:25:30 -0600147 err := (r.(InstancePage)).ExtractInto(&s)
Jon Perritt12395212016-02-24 10:41:17 -0600148 return s.Instances, err
Jamie Hannaford90684242015-02-10 12:46:07 +0100149}
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100150
Jon Perrittdb0ae142016-03-13 00:33:41 -0600151// EnableRootUserResult represents the result of an operation to enable the root user.
152type EnableRootUserResult struct {
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100153 gophercloud.Result
154}
155
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100156// Extract will extract root user information from a UserRootResult.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600157func (r EnableRootUserResult) Extract() (*users.User, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600158 var s struct {
159 User *users.User `json:"user"`
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100160 }
Jon Perritt12395212016-02-24 10:41:17 -0600161 err := r.ExtractInto(&s)
162 return s.User, err
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100163}
Jamie Hannaford219ca592015-02-10 15:59:05 +0100164
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100165// ActionResult represents the result of action requests, such as: restarting
166// an instance service, resizing its memory allocation, and resizing its
167// attached volume size.
Jamie Hannaford219ca592015-02-10 15:59:05 +0100168type ActionResult struct {
169 gophercloud.ErrResult
170}
Jon Perrittdb0ae142016-03-13 00:33:41 -0600171
172// IsRootEnabledResult is the result of a call to IsRootEnabled. To see if
173// root is enabled, call the type's Extract method.
174type IsRootEnabledResult struct {
175 gophercloud.Result
176}
177
178// Extract is used to extract the data from a IsRootEnabledResult.
179func (r IsRootEnabledResult) Extract() (bool, error) {
180 return r.Body.(map[string]interface{})["rootEnabled"] == true, r.Err
181}