blob: 4b1317eb8b0f8221938a5729b2a5e3faab83ed53 [file] [log] [blame]
Jamie Hannaford2a4beaa2015-02-09 17:27:18 +01001package instances
2
Jamie Hannaford3dbfb2d2015-02-10 11:06:47 +01003import (
Jamie Hannaforde65ad952015-11-16 14:05:11 +01004 "time"
5
Jamie Hannaford3dbfb2d2015-02-10 11:06:47 +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 Hannaford11108402015-02-23 10:31:41 +01009 "github.com/rackspace/gophercloud/openstack/db/v1/flavors"
Jamie Hannaford9fdda582015-02-10 12:15:43 +010010 os "github.com/rackspace/gophercloud/openstack/db/v1/instances"
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +010011 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford3dbfb2d2015-02-10 11:06:47 +010012)
13
Jamie Hannaford27957b22015-02-12 12:50:55 +010014// Instance represents a remote MySQL instance.
Jamie Hannaford3dbfb2d2015-02-10 11:06:47 +010015type Instance struct {
Jamie Hannaford27957b22015-02-12 12:50:55 +010016 // Indicates the datetime that the instance was created
Jamie Hannaforde65ad952015-11-16 14:05:11 +010017 Created time.Time `mapstructure:"-"`
Jamie Hannaford27957b22015-02-12 12:50:55 +010018
19 // Indicates the most recent datetime that the instance was updated.
Jamie Hannaforde65ad952015-11-16 14:05:11 +010020 Updated time.Time `mapstructure:"-"`
Jamie Hannaford27957b22015-02-12 12:50:55 +010021
22 // Indicates how the instance stores data.
Jamie Hannaforda50d1352015-02-18 11:38:38 +010023 Datastore datastores.DatastorePartial
Jamie Hannaford27957b22015-02-12 12:50:55 +010024
25 // Indicates the hardware flavor the instance uses.
Jamie Hannaford11108402015-02-23 10:31:41 +010026 Flavor flavors.Flavor
Jamie Hannaford27957b22015-02-12 12:50:55 +010027
28 // A DNS-resolvable hostname associated with the database instance (rather
29 // than an IPv4 address). Since the hostname always resolves to the correct
30 // IP address of the database instance, this relieves the user from the task
31 // of maintaining the mapping. Note that although the IP address may likely
32 // change on resizing, migrating, and so forth, the hostname always resolves
33 // to the correct database instance.
34 Hostname string
35
36 // Indicates the unique identifier for the instance resource.
37 ID string
38
39 // Exposes various links that reference the instance resource.
40 Links []gophercloud.Link
41
42 // The human-readable name of the instance.
43 Name string
44
45 // The build status of the instance.
46 Status string
47
48 // Information about the attached volume of the instance.
49 Volume os.Volume
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +010050
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010051 // IP indicates the various IP addresses which allow access.
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +010052 IP []string
53
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010054 // Indicates whether this instance is a replica of another source instance.
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +010055 ReplicaOf *Instance `mapstructure:"replica_of" json:"replica_of"`
56
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010057 // Indicates whether this instance is the source of other replica instances.
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +010058 Replicas []Instance
Jamie Hannaford3dbfb2d2015-02-10 11:06:47 +010059}
Jamie Hannaford2a4beaa2015-02-09 17:27:18 +010060
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +010061func commonExtract(err error, body interface{}) (*Instance, error) {
62 if err != nil {
63 return nil, err
Jamie Hannaford3dbfb2d2015-02-10 11:06:47 +010064 }
65
66 var response struct {
67 Instance Instance `mapstructure:"instance"`
68 }
69
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +010070 err = mapstructure.Decode(body, &response)
Jamie Hannaforde65ad952015-11-16 14:05:11 +010071
72 val := body.(map[string]interface{})["instance"].(map[string]interface{})
73
74 if t, ok := val["created"].(string); ok && t != "" {
75 creationTime, err := time.Parse(time.RFC3339, t)
76 if err != nil {
77 return &response.Instance, err
78 }
79 response.Instance.Created = creationTime
80 }
81
82 if t, ok := val["updated"].(string); ok && t != "" {
83 updatedTime, err := time.Parse(time.RFC3339, t)
84 if err != nil {
85 return &response.Instance, err
86 }
87 response.Instance.Updated = updatedTime
88 }
89
Jamie Hannaford3dbfb2d2015-02-10 11:06:47 +010090 return &response.Instance, err
91}
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +010092
93// CreateResult represents the result of a Create operation.
94type CreateResult struct {
95 os.CreateResult
96}
97
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010098// Extract will retrieve an instance from a create result.
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +010099func (r CreateResult) Extract() (*Instance, error) {
100 return commonExtract(r.Err, r.Body)
101}
102
Jamie Hannaford27957b22015-02-12 12:50:55 +0100103// GetResult represents the result of a Get operation.
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +0100104type GetResult struct {
105 os.GetResult
106}
107
Jamie Hannaford27957b22015-02-12 12:50:55 +0100108// Extract will extract an Instance from a GetResult.
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +0100109func (r GetResult) Extract() (*Instance, error) {
110 return commonExtract(r.Err, r.Body)
111}
Jamie Hannaford936a5472015-02-10 14:38:28 +0100112
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100113// ConfigResult represents the result of getting default configuration for an
114// instance.
Jamie Hannaford936a5472015-02-10 14:38:28 +0100115type ConfigResult struct {
116 gophercloud.Result
117}
118
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100119// DetachResult represents the result of detaching a replica from its source.
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100120type DetachResult struct {
121 gophercloud.ErrResult
122}
123
Jamie Hannaford27957b22015-02-12 12:50:55 +0100124// Extract will extract the configuration information (in the form of a map)
125// about a particular instance.
Jamie Hannaford936a5472015-02-10 14:38:28 +0100126func (r ConfigResult) Extract() (map[string]string, error) {
Jamie Hannafordf77fc102015-02-10 14:56:02 +0100127 if r.Err != nil {
128 return nil, r.Err
Jamie Hannaford936a5472015-02-10 14:38:28 +0100129 }
130
131 var response struct {
132 Instance struct {
133 Config map[string]string `mapstructure:"configuration"`
134 } `mapstructure:"instance"`
135 }
136
Jamie Hannafordf77fc102015-02-10 14:56:02 +0100137 err := mapstructure.Decode(r.Body, &response)
138 return response.Instance.Config, err
139}
140
Jamie Hannaford27957b22015-02-12 12:50:55 +0100141// UpdateResult represents the result of an Update operation.
Jamie Hannafordf77fc102015-02-10 14:56:02 +0100142type UpdateResult struct {
143 gophercloud.ErrResult
Jamie Hannaford936a5472015-02-10 14:38:28 +0100144}
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100145
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100146// ExtractInstances retrieves a slice of instances from a paginated collection.
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100147func ExtractInstances(page pagination.Page) ([]Instance, error) {
148 casted := page.(os.InstancePage).Body
149
150 var response struct {
151 Instances []Instance `mapstructure:"instances"`
152 }
153
154 err := mapstructure.Decode(casted, &response)
Jamie Hannaforde65ad952015-11-16 14:05:11 +0100155
156 var vals []interface{}
157 switch (casted).(type) {
158 case interface{}:
159 vals = casted.(map[string]interface{})["instances"].([]interface{})
160 }
161
162 for i, v := range vals {
163 val := v.(map[string]interface{})
164
165 if t, ok := val["created"].(string); ok && t != "" {
166 creationTime, err := time.Parse(time.RFC3339, t)
167 if err != nil {
168 return response.Instances, err
169 }
170 response.Instances[i].Created = creationTime
171 }
172
173 if t, ok := val["updated"].(string); ok && t != "" {
174 updatedTime, err := time.Parse(time.RFC3339, t)
175 if err != nil {
176 return response.Instances, err
177 }
178 response.Instances[i].Updated = updatedTime
179 }
180 }
181
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100182 return response.Instances, err
183}