blob: a516cb4e3f13911597c7a5ba88c83cc406a44262 [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 Hannaford11108402015-02-23 10:31:41 +01006 "github.com/rackspace/gophercloud/openstack/db/v1/flavors"
Jamie Hannaford9fdda582015-02-10 12:15:43 +01007 os "github.com/rackspace/gophercloud/openstack/db/v1/instances"
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +01008 "github.com/rackspace/gophercloud/pagination"
Jamie Hannafordc1c6bf82015-02-17 16:53:38 +01009 "github.com/rackspace/gophercloud/rackspace/db/v1/datastores"
Jamie Hannaford3dbfb2d2015-02-10 11:06:47 +010010)
11
Jamie Hannaford27957b22015-02-12 12:50:55 +010012// Instance represents a remote MySQL instance.
Jamie Hannaford3dbfb2d2015-02-10 11:06:47 +010013type Instance struct {
Jamie Hannaford27957b22015-02-12 12:50:55 +010014 // Indicates the datetime that the instance was created
15 Created string //time.Time
16
17 // Indicates the most recent datetime that the instance was updated.
18 Updated string //time.Time
19
20 // Indicates how the instance stores data.
Jamie Hannaforda50d1352015-02-18 11:38:38 +010021 Datastore datastores.DatastorePartial
Jamie Hannaford27957b22015-02-12 12:50:55 +010022
23 // Indicates the hardware flavor the instance uses.
Jamie Hannaford11108402015-02-23 10:31:41 +010024 Flavor flavors.Flavor
Jamie Hannaford27957b22015-02-12 12:50:55 +010025
26 // A DNS-resolvable hostname associated with the database instance (rather
27 // than an IPv4 address). Since the hostname always resolves to the correct
28 // IP address of the database instance, this relieves the user from the task
29 // of maintaining the mapping. Note that although the IP address may likely
30 // change on resizing, migrating, and so forth, the hostname always resolves
31 // to the correct database instance.
32 Hostname string
33
34 // Indicates the unique identifier for the instance resource.
35 ID string
36
37 // Exposes various links that reference the instance resource.
38 Links []gophercloud.Link
39
40 // The human-readable name of the instance.
41 Name string
42
43 // The build status of the instance.
44 Status string
45
46 // Information about the attached volume of the instance.
47 Volume os.Volume
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +010048
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010049 // IP indicates the various IP addresses which allow access.
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +010050 IP []string
51
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010052 // Indicates whether this instance is a replica of another source instance.
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +010053 ReplicaOf *Instance `mapstructure:"replica_of" json:"replica_of"`
54
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010055 // Indicates whether this instance is the source of other replica instances.
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +010056 Replicas []Instance
Jamie Hannaford3dbfb2d2015-02-10 11:06:47 +010057}
Jamie Hannaford2a4beaa2015-02-09 17:27:18 +010058
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +010059func commonExtract(err error, body interface{}) (*Instance, error) {
60 if err != nil {
61 return nil, err
Jamie Hannaford3dbfb2d2015-02-10 11:06:47 +010062 }
63
64 var response struct {
65 Instance Instance `mapstructure:"instance"`
66 }
67
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +010068 err = mapstructure.Decode(body, &response)
Jamie Hannaford3dbfb2d2015-02-10 11:06:47 +010069 return &response.Instance, err
70}
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +010071
72// CreateResult represents the result of a Create operation.
73type CreateResult struct {
74 os.CreateResult
75}
76
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010077// Extract will retrieve an instance from a create result.
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +010078func (r CreateResult) Extract() (*Instance, error) {
79 return commonExtract(r.Err, r.Body)
80}
81
Jamie Hannaford27957b22015-02-12 12:50:55 +010082// GetResult represents the result of a Get operation.
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +010083type GetResult struct {
84 os.GetResult
85}
86
Jamie Hannaford27957b22015-02-12 12:50:55 +010087// Extract will extract an Instance from a GetResult.
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +010088func (r GetResult) Extract() (*Instance, error) {
89 return commonExtract(r.Err, r.Body)
90}
Jamie Hannaford936a5472015-02-10 14:38:28 +010091
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010092// ConfigResult represents the result of getting default configuration for an
93// instance.
Jamie Hannaford936a5472015-02-10 14:38:28 +010094type ConfigResult struct {
95 gophercloud.Result
96}
97
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010098// DetachResult represents the result of detaching a replica from its source.
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +010099type DetachResult struct {
100 gophercloud.ErrResult
101}
102
Jamie Hannaford27957b22015-02-12 12:50:55 +0100103// Extract will extract the configuration information (in the form of a map)
104// about a particular instance.
Jamie Hannaford936a5472015-02-10 14:38:28 +0100105func (r ConfigResult) Extract() (map[string]string, error) {
Jamie Hannafordf77fc102015-02-10 14:56:02 +0100106 if r.Err != nil {
107 return nil, r.Err
Jamie Hannaford936a5472015-02-10 14:38:28 +0100108 }
109
110 var response struct {
111 Instance struct {
112 Config map[string]string `mapstructure:"configuration"`
113 } `mapstructure:"instance"`
114 }
115
Jamie Hannafordf77fc102015-02-10 14:56:02 +0100116 err := mapstructure.Decode(r.Body, &response)
117 return response.Instance.Config, err
118}
119
Jamie Hannaford27957b22015-02-12 12:50:55 +0100120// UpdateResult represents the result of an Update operation.
Jamie Hannafordf77fc102015-02-10 14:56:02 +0100121type UpdateResult struct {
122 gophercloud.ErrResult
Jamie Hannaford936a5472015-02-10 14:38:28 +0100123}
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100124
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100125// ExtractInstances retrieves a slice of instances from a paginated collection.
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100126func ExtractInstances(page pagination.Page) ([]Instance, error) {
127 casted := page.(os.InstancePage).Body
128
129 var response struct {
130 Instances []Instance `mapstructure:"instances"`
131 }
132
133 err := mapstructure.Decode(casted, &response)
134 return response.Instances, err
135}