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 | 1110840 | 2015-02-23 10:31:41 +0100 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud/openstack/db/v1/flavors" |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 7 | os "github.com/rackspace/gophercloud/openstack/db/v1/instances" |
Jamie Hannaford | 4ec6afe | 2015-02-16 16:52:49 +0100 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud/pagination" |
Jamie Hannaford | c1c6bf8 | 2015-02-17 16:53:38 +0100 | [diff] [blame] | 9 | "github.com/rackspace/gophercloud/rackspace/db/v1/datastores" |
Jamie Hannaford | 3dbfb2d | 2015-02-10 11:06:47 +0100 | [diff] [blame] | 10 | ) |
| 11 | |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 12 | // Instance represents a remote MySQL instance. |
Jamie Hannaford | 3dbfb2d | 2015-02-10 11:06:47 +0100 | [diff] [blame] | 13 | type Instance struct { |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 14 | // 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 Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 21 | Datastore datastores.DatastorePartial |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 22 | |
| 23 | // Indicates the hardware flavor the instance uses. |
Jamie Hannaford | 1110840 | 2015-02-23 10:31:41 +0100 | [diff] [blame] | 24 | Flavor flavors.Flavor |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 25 | |
| 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 Hannaford | 4ec6afe | 2015-02-16 16:52:49 +0100 | [diff] [blame] | 48 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 49 | // IP indicates the various IP addresses which allow access. |
Jamie Hannaford | 4ec6afe | 2015-02-16 16:52:49 +0100 | [diff] [blame] | 50 | IP []string |
| 51 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 52 | // Indicates whether this instance is a replica of another source instance. |
Jamie Hannaford | 4ec6afe | 2015-02-16 16:52:49 +0100 | [diff] [blame] | 53 | ReplicaOf *Instance `mapstructure:"replica_of" json:"replica_of"` |
| 54 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 55 | // Indicates whether this instance is the source of other replica instances. |
Jamie Hannaford | 4ec6afe | 2015-02-16 16:52:49 +0100 | [diff] [blame] | 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 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 77 | // Extract will retrieve an instance from a create result. |
Jamie Hannaford | 39d4ffb | 2015-02-10 13:19:44 +0100 | [diff] [blame] | 78 | func (r CreateResult) Extract() (*Instance, error) { |
| 79 | return commonExtract(r.Err, r.Body) |
| 80 | } |
| 81 | |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 82 | // GetResult represents the result of a Get operation. |
Jamie Hannaford | 39d4ffb | 2015-02-10 13:19:44 +0100 | [diff] [blame] | 83 | type GetResult struct { |
| 84 | os.GetResult |
| 85 | } |
| 86 | |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 87 | // Extract will extract an Instance from a GetResult. |
Jamie Hannaford | 39d4ffb | 2015-02-10 13:19:44 +0100 | [diff] [blame] | 88 | func (r GetResult) Extract() (*Instance, error) { |
| 89 | return commonExtract(r.Err, r.Body) |
| 90 | } |
Jamie Hannaford | 936a547 | 2015-02-10 14:38:28 +0100 | [diff] [blame] | 91 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 92 | // ConfigResult represents the result of getting default configuration for an |
| 93 | // instance. |
Jamie Hannaford | 936a547 | 2015-02-10 14:38:28 +0100 | [diff] [blame] | 94 | type ConfigResult struct { |
| 95 | gophercloud.Result |
| 96 | } |
| 97 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 98 | // DetachResult represents the result of detaching a replica from its source. |
Jamie Hannaford | 4ec6afe | 2015-02-16 16:52:49 +0100 | [diff] [blame] | 99 | type DetachResult struct { |
| 100 | gophercloud.ErrResult |
| 101 | } |
| 102 | |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 103 | // Extract will extract the configuration information (in the form of a map) |
| 104 | // about a particular instance. |
Jamie Hannaford | 936a547 | 2015-02-10 14:38:28 +0100 | [diff] [blame] | 105 | func (r ConfigResult) Extract() (map[string]string, error) { |
Jamie Hannaford | f77fc10 | 2015-02-10 14:56:02 +0100 | [diff] [blame] | 106 | if r.Err != nil { |
| 107 | return nil, r.Err |
Jamie Hannaford | 936a547 | 2015-02-10 14:38:28 +0100 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | var response struct { |
| 111 | Instance struct { |
| 112 | Config map[string]string `mapstructure:"configuration"` |
| 113 | } `mapstructure:"instance"` |
| 114 | } |
| 115 | |
Jamie Hannaford | f77fc10 | 2015-02-10 14:56:02 +0100 | [diff] [blame] | 116 | err := mapstructure.Decode(r.Body, &response) |
| 117 | return response.Instance.Config, err |
| 118 | } |
| 119 | |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 120 | // UpdateResult represents the result of an Update operation. |
Jamie Hannaford | f77fc10 | 2015-02-10 14:56:02 +0100 | [diff] [blame] | 121 | type UpdateResult struct { |
| 122 | gophercloud.ErrResult |
Jamie Hannaford | 936a547 | 2015-02-10 14:38:28 +0100 | [diff] [blame] | 123 | } |
Jamie Hannaford | 4ec6afe | 2015-02-16 16:52:49 +0100 | [diff] [blame] | 124 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 125 | // ExtractInstances retrieves a slice of instances from a paginated collection. |
Jamie Hannaford | 4ec6afe | 2015-02-16 16:52:49 +0100 | [diff] [blame] | 126 | func 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 | } |