blob: 9a495100011f74e6ef258b165ef8dfd6fb9360e5 [file] [log] [blame]
Jamie Hannaford9fdda582015-02-10 12:15:43 +01001package instances
2
3import (
Jamie Hannaforde65ad952015-11-16 14:05:11 +01004 "time"
5
Jamie Hannaford9fdda582015-02-10 12:15:43 +01006 "github.com/mitchellh/mapstructure"
7 "github.com/rackspace/gophercloud"
Jamie Hannaford52dbcee2015-10-06 16:09:56 +02008 "github.com/rackspace/gophercloud/openstack/db/v1/datastores"
Jamie Hannaford9793d942015-02-18 15:13:20 +01009 "github.com/rackspace/gophercloud/openstack/db/v1/flavors"
Jamie Hannaford2ca55d82015-02-12 14:21:55 +010010 "github.com/rackspace/gophercloud/openstack/db/v1/users"
Jamie Hannaford90684242015-02-10 12:46:07 +010011 "github.com/rackspace/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
Jamie Hannaforde65ad952015-11-16 14:05:11 +010025 Created time.Time `mapstructure:"-"`
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010026
27 // Indicates the most recent datetime that the instance was updated.
Jamie Hannaforde65ad952015-11-16 14:05:11 +010028 Updated time.Time `mapstructure:"-"`
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
Jamie Hannaford821015f2015-02-10 12:58:36 +010060type commonResult struct {
Jamie Hannaford9fdda582015-02-10 12:15:43 +010061 gophercloud.Result
62}
63
Jamie Hannaford821015f2015-02-10 12:58:36 +010064// CreateResult represents the result of a Create operation.
65type CreateResult struct {
66 commonResult
67}
68
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010069// GetResult represents the result of a Get operation.
Jamie Hannaford821015f2015-02-10 12:58:36 +010070type GetResult struct {
71 commonResult
72}
73
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010074// DeleteResult represents the result of a Delete operation.
Jamie Hannaford5b16b632015-02-10 13:36:23 +010075type DeleteResult struct {
76 gophercloud.ErrResult
77}
78
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010079// Extract will extract an Instance from various result structs.
Jamie Hannaford821015f2015-02-10 12:58:36 +010080func (r commonResult) Extract() (*Instance, error) {
Jamie Hannaford9fdda582015-02-10 12:15:43 +010081 if r.Err != nil {
82 return nil, r.Err
83 }
84
85 var response struct {
86 Instance Instance `mapstructure:"instance"`
87 }
88
89 err := mapstructure.Decode(r.Body, &response)
Jamie Hannaforde65ad952015-11-16 14:05:11 +010090 val := r.Body.(map[string]interface{})["instance"].(map[string]interface{})
91
92 if t, ok := val["created"].(string); ok && t != "" {
93 creationTime, err := time.Parse(time.RFC3339, t)
94 if err != nil {
95 return &response.Instance, err
96 }
97 response.Instance.Created = creationTime
98 }
99
100 if t, ok := val["updated"].(string); ok && t != "" {
101 updatedTime, err := time.Parse(time.RFC3339, t)
102 if err != nil {
103 return &response.Instance, err
104 }
105 response.Instance.Updated = updatedTime
106 }
Jamie Hannaford9fdda582015-02-10 12:15:43 +0100107
108 return &response.Instance, err
109}
Jamie Hannaford90684242015-02-10 12:46:07 +0100110
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100111// InstancePage represents a single page of a paginated instance collection.
Jamie Hannaford90684242015-02-10 12:46:07 +0100112type InstancePage struct {
113 pagination.LinkedPageBase
114}
115
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100116// IsEmpty checks to see whether the collection is empty.
Jamie Hannaford90684242015-02-10 12:46:07 +0100117func (page InstancePage) IsEmpty() (bool, error) {
118 instances, err := ExtractInstances(page)
119 if err != nil {
120 return true, err
121 }
122 return len(instances) == 0, nil
123}
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) {
127 type resp struct {
128 Links []gophercloud.Link `mapstructure:"instances_links"`
129 }
130
131 var r resp
132 err := mapstructure.Decode(page.Body, &r)
133 if err != nil {
134 return "", err
135 }
136
137 return gophercloud.ExtractNextURL(r.Links)
138}
139
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100140// ExtractInstances will convert a generic pagination struct into a more
141// relevant slice of Instance structs.
Jamie Hannaford90684242015-02-10 12:46:07 +0100142func ExtractInstances(page pagination.Page) ([]Instance, error) {
143 casted := page.(InstancePage).Body
144
145 var response struct {
146 Instances []Instance `mapstructure:"instances"`
147 }
148
149 err := mapstructure.Decode(casted, &response)
150
Jamie Hannaforde65ad952015-11-16 14:05:11 +0100151 var vals []interface{}
152 switch (casted).(type) {
153 case interface{}:
154 vals = casted.(map[string]interface{})["instances"].([]interface{})
155 }
156
157 for i, v := range vals {
158 val := v.(map[string]interface{})
159
160 if t, ok := val["created"].(string); ok && t != "" {
161 creationTime, err := time.Parse(time.RFC3339, t)
162 if err != nil {
163 return response.Instances, err
164 }
165 response.Instances[i].Created = creationTime
166 }
167
168 if t, ok := val["updated"].(string); ok && t != "" {
169 updatedTime, err := time.Parse(time.RFC3339, t)
170 if err != nil {
171 return response.Instances, err
172 }
173 response.Instances[i].Updated = updatedTime
174 }
175 }
176
Jamie Hannaford90684242015-02-10 12:46:07 +0100177 return response.Instances, err
178}
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100179
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100180// UserRootResult represents the result of an operation to enable the root user.
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100181type UserRootResult struct {
182 gophercloud.Result
183}
184
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100185// Extract will extract root user information from a UserRootResult.
Jamie Hannaford3aba0b12015-02-13 14:33:39 +0100186func (r UserRootResult) Extract() (*users.User, error) {
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100187 if r.Err != nil {
188 return nil, r.Err
189 }
190
191 var response struct {
Jamie Hannaford2ca55d82015-02-12 14:21:55 +0100192 User users.User `mapstructure:"user"`
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100193 }
194
195 err := mapstructure.Decode(r.Body, &response)
196
197 return &response.User, err
198}
Jamie Hannaford219ca592015-02-10 15:59:05 +0100199
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100200// ActionResult represents the result of action requests, such as: restarting
201// an instance service, resizing its memory allocation, and resizing its
202// attached volume size.
Jamie Hannaford219ca592015-02-10 15:59:05 +0100203type ActionResult struct {
204 gophercloud.ErrResult
205}