blob: 2ce302b97ccf7927799aad70124039a8d70a05a5 [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
48 IP []string
49
50 ReplicaOf *Instance `mapstructure:"replica_of" json:"replica_of"`
51
52 Replicas []Instance
Jamie Hannaford3dbfb2d2015-02-10 11:06:47 +010053}
Jamie Hannaford2a4beaa2015-02-09 17:27:18 +010054
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +010055func commonExtract(err error, body interface{}) (*Instance, error) {
56 if err != nil {
57 return nil, err
Jamie Hannaford3dbfb2d2015-02-10 11:06:47 +010058 }
59
60 var response struct {
61 Instance Instance `mapstructure:"instance"`
62 }
63
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +010064 err = mapstructure.Decode(body, &response)
Jamie Hannaford3dbfb2d2015-02-10 11:06:47 +010065 return &response.Instance, err
66}
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +010067
68// CreateResult represents the result of a Create operation.
69type CreateResult struct {
70 os.CreateResult
71}
72
73func (r CreateResult) Extract() (*Instance, error) {
74 return commonExtract(r.Err, r.Body)
75}
76
Jamie Hannaford27957b22015-02-12 12:50:55 +010077// GetResult represents the result of a Get operation.
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +010078type GetResult struct {
79 os.GetResult
80}
81
Jamie Hannaford27957b22015-02-12 12:50:55 +010082// Extract will extract an Instance from a GetResult.
Jamie Hannaford39d4ffb2015-02-10 13:19:44 +010083func (r GetResult) Extract() (*Instance, error) {
84 return commonExtract(r.Err, r.Body)
85}
Jamie Hannaford936a5472015-02-10 14:38:28 +010086
87type ConfigResult struct {
88 gophercloud.Result
89}
90
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +010091type DetachResult struct {
92 gophercloud.ErrResult
93}
94
Jamie Hannaford27957b22015-02-12 12:50:55 +010095// Extract will extract the configuration information (in the form of a map)
96// about a particular instance.
Jamie Hannaford936a5472015-02-10 14:38:28 +010097func (r ConfigResult) Extract() (map[string]string, error) {
Jamie Hannafordf77fc102015-02-10 14:56:02 +010098 if r.Err != nil {
99 return nil, r.Err
Jamie Hannaford936a5472015-02-10 14:38:28 +0100100 }
101
102 var response struct {
103 Instance struct {
104 Config map[string]string `mapstructure:"configuration"`
105 } `mapstructure:"instance"`
106 }
107
Jamie Hannafordf77fc102015-02-10 14:56:02 +0100108 err := mapstructure.Decode(r.Body, &response)
109 return response.Instance.Config, err
110}
111
Jamie Hannaford27957b22015-02-12 12:50:55 +0100112// UpdateResult represents the result of an Update operation.
Jamie Hannafordf77fc102015-02-10 14:56:02 +0100113type UpdateResult struct {
114 gophercloud.ErrResult
Jamie Hannaford936a5472015-02-10 14:38:28 +0100115}
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100116
117func ExtractInstances(page pagination.Page) ([]Instance, error) {
118 casted := page.(os.InstancePage).Body
119
120 var response struct {
121 Instances []Instance `mapstructure:"instances"`
122 }
123
124 err := mapstructure.Decode(casted, &response)
125 return response.Instances, err
126}