blob: 7a44f1bfc953dacbc10902052c9abe4e5f77ebf6 [file] [log] [blame]
Jamie Hannaford2a4beaa2015-02-09 17:27:18 +01001package instances
2
Jamie Hannaford3dbfb2d2015-02-10 11:06:47 +01003import (
4 "github.com/mitchellh/mapstructure"
5 "github.com/rackspace/gophercloud"
Jamie Hannaford9fdda582015-02-10 12:15:43 +01006 os "github.com/rackspace/gophercloud/openstack/db/v1/instances"
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +01007 "github.com/rackspace/gophercloud/pagination"
Jamie Hannafordc1c6bf82015-02-17 16:53:38 +01008 "github.com/rackspace/gophercloud/rackspace/db/v1/datastores"
Jamie Hannaford3dbfb2d2015-02-10 11:06:47 +01009)
10
Jamie Hannaford27957b22015-02-12 12:50:55 +010011// Instance represents a remote MySQL instance.
Jamie Hannaford3dbfb2d2015-02-10 11:06:47 +010012type Instance struct {
Jamie Hannaford27957b22015-02-12 12:50:55 +010013 // Indicates the datetime that the instance was created
14 Created string //time.Time
15
16 // Indicates the most recent datetime that the instance was updated.
17 Updated string //time.Time
18
19 // Indicates how the instance stores data.
Jamie Hannaforda50d1352015-02-18 11:38:38 +010020 Datastore datastores.DatastorePartial
Jamie Hannaford27957b22015-02-12 12:50:55 +010021
22 // Indicates the hardware flavor the instance uses.
23 Flavor os.Flavor
24
25 // A DNS-resolvable hostname associated with the database instance (rather
26 // than an IPv4 address). Since the hostname always resolves to the correct
27 // IP address of the database instance, this relieves the user from the task
28 // of maintaining the mapping. Note that although the IP address may likely
29 // change on resizing, migrating, and so forth, the hostname always resolves
30 // to the correct database instance.
31 Hostname string
32
33 // Indicates the unique identifier for the instance resource.
34 ID string
35
36 // Exposes various links that reference the instance resource.
37 Links []gophercloud.Link
38
39 // The human-readable name of the instance.
40 Name string
41
42 // The build status of the instance.
43 Status string
44
45 // Information about the attached volume of the instance.
46 Volume os.Volume
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +010047
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010048 // IP indicates the various IP addresses which allow access.
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +010049 IP []string
50
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010051 // Indicates whether this instance is a replica of another source instance.
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +010052 ReplicaOf *Instance `mapstructure:"replica_of" json:"replica_of"`
53
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010054 // Indicates whether this instance is the source of other replica instances.
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +010055 Replicas []Instance
Jamie Hannaford3dbfb2d2015-02-10 11:06:47 +010056}
Jamie Hannaford2a4beaa2015-02-09 17:27:18 +010057
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +010058func commonExtract(err error, body interface{}) (*Instance, error) {
59 if err != nil {
60 return nil, err
Jamie Hannaford3dbfb2d2015-02-10 11:06:47 +010061 }
62
63 var response struct {
64 Instance Instance `mapstructure:"instance"`
65 }
66
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +010067 err = mapstructure.Decode(body, &response)
Jamie Hannaford3dbfb2d2015-02-10 11:06:47 +010068 return &response.Instance, err
69}
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +010070
71// CreateResult represents the result of a Create operation.
72type CreateResult struct {
73 os.CreateResult
74}
75
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010076// Extract will retrieve an instance from a create result.
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +010077func (r CreateResult) Extract() (*Instance, error) {
78 return commonExtract(r.Err, r.Body)
79}
80
Jamie Hannaford27957b22015-02-12 12:50:55 +010081// GetResult represents the result of a Get operation.
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +010082type GetResult struct {
83 os.GetResult
84}
85
Jamie Hannaford27957b22015-02-12 12:50:55 +010086// Extract will extract an Instance from a GetResult.
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +010087func (r GetResult) Extract() (*Instance, error) {
88 return commonExtract(r.Err, r.Body)
89}
Jamie Hannaford936a5472015-02-10 14:38:28 +010090
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010091// ConfigResult represents the result of getting default configuration for an
92// instance.
Jamie Hannaford936a5472015-02-10 14:38:28 +010093type ConfigResult struct {
94 gophercloud.Result
95}
96
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010097// DetachResult represents the result of detaching a replica from its source.
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +010098type DetachResult struct {
99 gophercloud.ErrResult
100}
101
Jamie Hannaford27957b22015-02-12 12:50:55 +0100102// Extract will extract the configuration information (in the form of a map)
103// about a particular instance.
Jamie Hannaford936a5472015-02-10 14:38:28 +0100104func (r ConfigResult) Extract() (map[string]string, error) {
Jamie Hannafordf77fc102015-02-10 14:56:02 +0100105 if r.Err != nil {
106 return nil, r.Err
Jamie Hannaford936a5472015-02-10 14:38:28 +0100107 }
108
109 var response struct {
110 Instance struct {
111 Config map[string]string `mapstructure:"configuration"`
112 } `mapstructure:"instance"`
113 }
114
Jamie Hannafordf77fc102015-02-10 14:56:02 +0100115 err := mapstructure.Decode(r.Body, &response)
116 return response.Instance.Config, err
117}
118
Jamie Hannaford27957b22015-02-12 12:50:55 +0100119// UpdateResult represents the result of an Update operation.
Jamie Hannafordf77fc102015-02-10 14:56:02 +0100120type UpdateResult struct {
121 gophercloud.ErrResult
Jamie Hannaford936a5472015-02-10 14:38:28 +0100122}
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100123
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100124// ExtractInstances retrieves a slice of instances from a paginated collection.
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100125func ExtractInstances(page pagination.Page) ([]Instance, error) {
126 casted := page.(os.InstancePage).Body
127
128 var response struct {
129 Instances []Instance `mapstructure:"instances"`
130 }
131
132 err := mapstructure.Decode(casted, &response)
133 return response.Instances, err
134}