blob: 6d60e86aeeb529ece5ed4618b281f63eec35685e [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"
9 "github.com/gophercloud/gophercloud/openstack/db/v1/flavors"
10 "github.com/gophercloud/gophercloud/openstack/db/v1/users"
11 "github.com/gophercloud/gophercloud/pagination"
Jamie Hannaford9fdda582015-02-10 12:15:43 +010012)
13
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010014// Volume represents information about an attached volume for a database instance.
Jamie Hannaford9fdda582015-02-10 12:15:43 +010015type Volume struct {
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010016 // The size in GB of the volume
Jamie Hannaford9fdda582015-02-10 12:15:43 +010017 Size int
Jamie Hannaford76e177b2015-02-16 16:53:00 +010018
19 Used float64
Jamie Hannaford9fdda582015-02-10 12:15:43 +010020}
21
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010022// Instance represents a remote MySQL instance.
Jamie Hannaford9fdda582015-02-10 12:15:43 +010023type Instance struct {
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010024 // Indicates the datetime that the instance was created
esalipe58d15022017-01-13 19:31:08 +020025 Created time.Time `json:"-"`
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010026
27 // Indicates the most recent datetime that the instance was updated.
esalipe58d15022017-01-13 19:31:08 +020028 Updated time.Time `json:"-"`
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010029
30 // Indicates the hardware flavor the instance uses.
Jamie Hannaford9793d942015-02-18 15:13:20 +010031 Flavor flavors.Flavor
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010032
33 // A DNS-resolvable hostname associated with the database instance (rather
34 // than an IPv4 address). Since the hostname always resolves to the correct
35 // IP address of the database instance, this relieves the user from the task
36 // of maintaining the mapping. Note that although the IP address may likely
37 // change on resizing, migrating, and so forth, the hostname always resolves
38 // to the correct database instance.
Jamie Hannaford9fdda582015-02-10 12:15:43 +010039 Hostname string
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010040
41 // Indicates the unique identifier for the instance resource.
42 ID string
43
44 // Exposes various links that reference the instance resource.
45 Links []gophercloud.Link
46
47 // The human-readable name of the instance.
48 Name string
49
50 // The build status of the instance.
51 Status string
52
53 // Information about the attached volume of the instance.
54 Volume Volume
Jamie Hannaford99eced52015-03-02 15:24:22 +010055
56 // Indicates how the instance stores data.
57 Datastore datastores.DatastorePartial
Jamie Hannaford9fdda582015-02-10 12:15:43 +010058}
59
esalipe58d15022017-01-13 19:31:08 +020060func (r *Instance) UnmarshalJSON(b []byte) error {
61 type tmp Instance
62 var s struct {
63 tmp
64 Created gophercloud.JSONRFC3339NoZ `json:"created"`
65 Updated gophercloud.JSONRFC3339NoZ `json:"updated"`
66 }
67 err := json.Unmarshal(b, &s)
68 if err != nil {
69 return err
70 }
71 *r = Instance(s.tmp)
72
73 r.Created = time.Time(s.Created)
74 r.Updated = time.Time(s.Updated)
75
76 return nil
77}
78
Jamie Hannaford821015f2015-02-10 12:58:36 +010079type commonResult struct {
Jamie Hannaford9fdda582015-02-10 12:15:43 +010080 gophercloud.Result
81}
82
Jamie Hannaford821015f2015-02-10 12:58:36 +010083// CreateResult represents the result of a Create operation.
84type CreateResult struct {
85 commonResult
86}
87
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010088// GetResult represents the result of a Get operation.
Jamie Hannaford821015f2015-02-10 12:58:36 +010089type GetResult struct {
90 commonResult
91}
92
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010093// DeleteResult represents the result of a Delete operation.
Jamie Hannaford5b16b632015-02-10 13:36:23 +010094type DeleteResult struct {
95 gophercloud.ErrResult
96}
97
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010098// Extract will extract an Instance from various result structs.
Jamie Hannaford821015f2015-02-10 12:58:36 +010099func (r commonResult) Extract() (*Instance, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600100 var s struct {
101 Instance *Instance `json:"instance"`
Jamie Hannaford9fdda582015-02-10 12:15:43 +0100102 }
Jon Perritt12395212016-02-24 10:41:17 -0600103 err := r.ExtractInto(&s)
104 return s.Instance, err
Jamie Hannaford9fdda582015-02-10 12:15:43 +0100105}
Jamie Hannaford90684242015-02-10 12:46:07 +0100106
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100107// InstancePage represents a single page of a paginated instance collection.
Jamie Hannaford90684242015-02-10 12:46:07 +0100108type InstancePage struct {
109 pagination.LinkedPageBase
110}
111
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100112// IsEmpty checks to see whether the collection is empty.
Jamie Hannaford90684242015-02-10 12:46:07 +0100113func (page InstancePage) IsEmpty() (bool, error) {
114 instances, err := ExtractInstances(page)
Jon Perritt12395212016-02-24 10:41:17 -0600115 return len(instances) == 0, err
Jamie Hannaford90684242015-02-10 12:46:07 +0100116}
117
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100118// NextPageURL will retrieve the next page URL.
Jamie Hannaford90684242015-02-10 12:46:07 +0100119func (page InstancePage) NextPageURL() (string, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600120 var s struct {
121 Links []gophercloud.Link `json:"instances_links"`
Jamie Hannaford90684242015-02-10 12:46:07 +0100122 }
Jon Perritt12395212016-02-24 10:41:17 -0600123 err := page.ExtractInto(&s)
Jamie Hannaford90684242015-02-10 12:46:07 +0100124 if err != nil {
125 return "", err
126 }
Jon Perritt12395212016-02-24 10:41:17 -0600127 return gophercloud.ExtractNextURL(s.Links)
Jamie Hannaford90684242015-02-10 12:46:07 +0100128}
129
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100130// ExtractInstances will convert a generic pagination struct into a more
131// relevant slice of Instance structs.
Jon Perritt31b66462016-02-25 22:25:30 -0600132func ExtractInstances(r pagination.Page) ([]Instance, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600133 var s struct {
134 Instances []Instance `json:"instances"`
Jamie Hannaford90684242015-02-10 12:46:07 +0100135 }
Jon Perritt31b66462016-02-25 22:25:30 -0600136 err := (r.(InstancePage)).ExtractInto(&s)
Jon Perritt12395212016-02-24 10:41:17 -0600137 return s.Instances, err
Jamie Hannaford90684242015-02-10 12:46:07 +0100138}
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100139
Jon Perrittdb0ae142016-03-13 00:33:41 -0600140// EnableRootUserResult represents the result of an operation to enable the root user.
141type EnableRootUserResult struct {
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100142 gophercloud.Result
143}
144
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100145// Extract will extract root user information from a UserRootResult.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600146func (r EnableRootUserResult) Extract() (*users.User, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600147 var s struct {
148 User *users.User `json:"user"`
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100149 }
Jon Perritt12395212016-02-24 10:41:17 -0600150 err := r.ExtractInto(&s)
151 return s.User, err
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100152}
Jamie Hannaford219ca592015-02-10 15:59:05 +0100153
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100154// ActionResult represents the result of action requests, such as: restarting
155// an instance service, resizing its memory allocation, and resizing its
156// attached volume size.
Jamie Hannaford219ca592015-02-10 15:59:05 +0100157type ActionResult struct {
158 gophercloud.ErrResult
159}
Jon Perrittdb0ae142016-03-13 00:33:41 -0600160
161// IsRootEnabledResult is the result of a call to IsRootEnabled. To see if
162// root is enabled, call the type's Extract method.
163type IsRootEnabledResult struct {
164 gophercloud.Result
165}
166
167// Extract is used to extract the data from a IsRootEnabledResult.
168func (r IsRootEnabledResult) Extract() (bool, error) {
169 return r.Body.(map[string]interface{})["rootEnabled"] == true, r.Err
170}