Jamie Hannaford | 2a4beaa | 2015-02-09 17:27:18 +0100 | [diff] [blame] | 1 | package instances |
| 2 | |
Jamie Hannaford | 3dbfb2d | 2015-02-10 11:06:47 +0100 | [diff] [blame] | 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
| 5 | "github.com/rackspace/gophercloud" |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 6 | os "github.com/rackspace/gophercloud/openstack/db/v1/instances" |
Jamie Hannaford | 4ec6afe | 2015-02-16 16:52:49 +0100 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud/pagination" |
Jamie Hannaford | c1c6bf8 | 2015-02-17 16:53:38 +0100 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud/rackspace/db/v1/datastores" |
Jamie Hannaford | 3dbfb2d | 2015-02-10 11:06:47 +0100 | [diff] [blame] | 9 | ) |
| 10 | |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 11 | // Instance represents a remote MySQL instance. |
Jamie Hannaford | 3dbfb2d | 2015-02-10 11:06:47 +0100 | [diff] [blame] | 12 | type Instance struct { |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 13 | // 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 Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 20 | Datastore datastores.DatastorePartial |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 21 | |
| 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 Hannaford | 4ec6afe | 2015-02-16 16:52:49 +0100 | [diff] [blame] | 47 | |
| 48 | IP []string |
| 49 | |
| 50 | ReplicaOf *Instance `mapstructure:"replica_of" json:"replica_of"` |
| 51 | |
| 52 | Replicas []Instance |
Jamie Hannaford | 3dbfb2d | 2015-02-10 11:06:47 +0100 | [diff] [blame] | 53 | } |
Jamie Hannaford | 2a4beaa | 2015-02-09 17:27:18 +0100 | [diff] [blame] | 54 | |
Jamie Hannaford | 39d4ffb | 2015-02-10 13:19:44 +0100 | [diff] [blame] | 55 | func commonExtract(err error, body interface{}) (*Instance, error) { |
| 56 | if err != nil { |
| 57 | return nil, err |
Jamie Hannaford | 3dbfb2d | 2015-02-10 11:06:47 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | var response struct { |
| 61 | Instance Instance `mapstructure:"instance"` |
| 62 | } |
| 63 | |
Jamie Hannaford | 39d4ffb | 2015-02-10 13:19:44 +0100 | [diff] [blame] | 64 | err = mapstructure.Decode(body, &response) |
Jamie Hannaford | 3dbfb2d | 2015-02-10 11:06:47 +0100 | [diff] [blame] | 65 | return &response.Instance, err |
| 66 | } |
Jamie Hannaford | 39d4ffb | 2015-02-10 13:19:44 +0100 | [diff] [blame] | 67 | |
| 68 | // CreateResult represents the result of a Create operation. |
| 69 | type CreateResult struct { |
| 70 | os.CreateResult |
| 71 | } |
| 72 | |
| 73 | func (r CreateResult) Extract() (*Instance, error) { |
| 74 | return commonExtract(r.Err, r.Body) |
| 75 | } |
| 76 | |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 77 | // GetResult represents the result of a Get operation. |
Jamie Hannaford | 39d4ffb | 2015-02-10 13:19:44 +0100 | [diff] [blame] | 78 | type GetResult struct { |
| 79 | os.GetResult |
| 80 | } |
| 81 | |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 82 | // Extract will extract an Instance from a GetResult. |
Jamie Hannaford | 39d4ffb | 2015-02-10 13:19:44 +0100 | [diff] [blame] | 83 | func (r GetResult) Extract() (*Instance, error) { |
| 84 | return commonExtract(r.Err, r.Body) |
| 85 | } |
Jamie Hannaford | 936a547 | 2015-02-10 14:38:28 +0100 | [diff] [blame] | 86 | |
| 87 | type ConfigResult struct { |
| 88 | gophercloud.Result |
| 89 | } |
| 90 | |
Jamie Hannaford | 4ec6afe | 2015-02-16 16:52:49 +0100 | [diff] [blame] | 91 | type DetachResult struct { |
| 92 | gophercloud.ErrResult |
| 93 | } |
| 94 | |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 95 | // Extract will extract the configuration information (in the form of a map) |
| 96 | // about a particular instance. |
Jamie Hannaford | 936a547 | 2015-02-10 14:38:28 +0100 | [diff] [blame] | 97 | func (r ConfigResult) Extract() (map[string]string, error) { |
Jamie Hannaford | f77fc10 | 2015-02-10 14:56:02 +0100 | [diff] [blame] | 98 | if r.Err != nil { |
| 99 | return nil, r.Err |
Jamie Hannaford | 936a547 | 2015-02-10 14:38:28 +0100 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | var response struct { |
| 103 | Instance struct { |
| 104 | Config map[string]string `mapstructure:"configuration"` |
| 105 | } `mapstructure:"instance"` |
| 106 | } |
| 107 | |
Jamie Hannaford | f77fc10 | 2015-02-10 14:56:02 +0100 | [diff] [blame] | 108 | err := mapstructure.Decode(r.Body, &response) |
| 109 | return response.Instance.Config, err |
| 110 | } |
| 111 | |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 112 | // UpdateResult represents the result of an Update operation. |
Jamie Hannaford | f77fc10 | 2015-02-10 14:56:02 +0100 | [diff] [blame] | 113 | type UpdateResult struct { |
| 114 | gophercloud.ErrResult |
Jamie Hannaford | 936a547 | 2015-02-10 14:38:28 +0100 | [diff] [blame] | 115 | } |
Jamie Hannaford | 4ec6afe | 2015-02-16 16:52:49 +0100 | [diff] [blame] | 116 | |
| 117 | func 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 | } |