blob: 760e5a7c269e4cfcc89348d281826914cc0a11f4 [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 Hannaford3dbfb2d2015-02-10 11:06:47 +01008)
9
10type Datastore struct {
11 Type string
12 Version string
13}
14
Jamie Hannaford27957b22015-02-12 12:50:55 +010015// Instance represents a remote MySQL instance.
Jamie Hannaford3dbfb2d2015-02-10 11:06:47 +010016type Instance struct {
Jamie Hannaford27957b22015-02-12 12:50:55 +010017 // Indicates the datetime that the instance was created
18 Created string //time.Time
19
20 // Indicates the most recent datetime that the instance was updated.
21 Updated string //time.Time
22
23 // Indicates how the instance stores data.
Jamie Hannaford3dbfb2d2015-02-10 11:06:47 +010024 Datastore Datastore
Jamie Hannaford27957b22015-02-12 12:50:55 +010025
26 // Indicates the hardware flavor the instance uses.
27 Flavor os.Flavor
28
29 // A DNS-resolvable hostname associated with the database instance (rather
30 // than an IPv4 address). Since the hostname always resolves to the correct
31 // IP address of the database instance, this relieves the user from the task
32 // of maintaining the mapping. Note that although the IP address may likely
33 // change on resizing, migrating, and so forth, the hostname always resolves
34 // to the correct database instance.
35 Hostname string
36
37 // Indicates the unique identifier for the instance resource.
38 ID string
39
40 // Exposes various links that reference the instance resource.
41 Links []gophercloud.Link
42
43 // The human-readable name of the instance.
44 Name string
45
46 // The build status of the instance.
47 Status string
48
49 // Information about the attached volume of the instance.
50 Volume os.Volume
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +010051
52 IP []string
53
54 ReplicaOf *Instance `mapstructure:"replica_of" json:"replica_of"`
55
56 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
77func (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
91type ConfigResult struct {
92 gophercloud.Result
93}
94
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +010095type DetachResult struct {
96 gophercloud.ErrResult
97}
98
Jamie Hannaford27957b22015-02-12 12:50:55 +010099// Extract will extract the configuration information (in the form of a map)
100// about a particular instance.
Jamie Hannaford936a5472015-02-10 14:38:28 +0100101func (r ConfigResult) Extract() (map[string]string, error) {
Jamie Hannafordf77fc102015-02-10 14:56:02 +0100102 if r.Err != nil {
103 return nil, r.Err
Jamie Hannaford936a5472015-02-10 14:38:28 +0100104 }
105
106 var response struct {
107 Instance struct {
108 Config map[string]string `mapstructure:"configuration"`
109 } `mapstructure:"instance"`
110 }
111
Jamie Hannafordf77fc102015-02-10 14:56:02 +0100112 err := mapstructure.Decode(r.Body, &response)
113 return response.Instance.Config, err
114}
115
Jamie Hannaford27957b22015-02-12 12:50:55 +0100116// UpdateResult represents the result of an Update operation.
Jamie Hannafordf77fc102015-02-10 14:56:02 +0100117type UpdateResult struct {
118 gophercloud.ErrResult
Jamie Hannaford936a5472015-02-10 14:38:28 +0100119}
Jamie Hannaford4ec6afe2015-02-16 16:52:49 +0100120
121func ExtractInstances(page pagination.Page) ([]Instance, error) {
122 casted := page.(os.InstancePage).Body
123
124 var response struct {
125 Instances []Instance `mapstructure:"instances"`
126 }
127
128 err := mapstructure.Decode(casted, &response)
129 return response.Instances, err
130}