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