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 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame^] | 48 | // IP indicates the various IP addresses which allow access. |
Jamie Hannaford | 4ec6afe | 2015-02-16 16:52:49 +0100 | [diff] [blame] | 49 | IP []string |
| 50 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame^] | 51 | // Indicates whether this instance is a replica of another source instance. |
Jamie Hannaford | 4ec6afe | 2015-02-16 16:52:49 +0100 | [diff] [blame] | 52 | ReplicaOf *Instance `mapstructure:"replica_of" json:"replica_of"` |
| 53 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame^] | 54 | // Indicates whether this instance is the source of other replica instances. |
Jamie Hannaford | 4ec6afe | 2015-02-16 16:52:49 +0100 | [diff] [blame] | 55 | Replicas []Instance |
Jamie Hannaford | 3dbfb2d | 2015-02-10 11:06:47 +0100 | [diff] [blame] | 56 | } |
Jamie Hannaford | 2a4beaa | 2015-02-09 17:27:18 +0100 | [diff] [blame] | 57 | |
Jamie Hannaford | 39d4ffb | 2015-02-10 13:19:44 +0100 | [diff] [blame] | 58 | func commonExtract(err error, body interface{}) (*Instance, error) { |
| 59 | if err != nil { |
| 60 | return nil, err |
Jamie Hannaford | 3dbfb2d | 2015-02-10 11:06:47 +0100 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | var response struct { |
| 64 | Instance Instance `mapstructure:"instance"` |
| 65 | } |
| 66 | |
Jamie Hannaford | 39d4ffb | 2015-02-10 13:19:44 +0100 | [diff] [blame] | 67 | err = mapstructure.Decode(body, &response) |
Jamie Hannaford | 3dbfb2d | 2015-02-10 11:06:47 +0100 | [diff] [blame] | 68 | return &response.Instance, err |
| 69 | } |
Jamie Hannaford | 39d4ffb | 2015-02-10 13:19:44 +0100 | [diff] [blame] | 70 | |
| 71 | // CreateResult represents the result of a Create operation. |
| 72 | type CreateResult struct { |
| 73 | os.CreateResult |
| 74 | } |
| 75 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame^] | 76 | // Extract will retrieve an instance from a create result. |
Jamie Hannaford | 39d4ffb | 2015-02-10 13:19:44 +0100 | [diff] [blame] | 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 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame^] | 91 | // ConfigResult represents the result of getting default configuration for an |
| 92 | // instance. |
Jamie Hannaford | 936a547 | 2015-02-10 14:38:28 +0100 | [diff] [blame] | 93 | type ConfigResult struct { |
| 94 | gophercloud.Result |
| 95 | } |
| 96 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame^] | 97 | // DetachResult represents the result of detaching a replica from its source. |
Jamie Hannaford | 4ec6afe | 2015-02-16 16:52:49 +0100 | [diff] [blame] | 98 | type DetachResult struct { |
| 99 | gophercloud.ErrResult |
| 100 | } |
| 101 | |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 102 | // Extract will extract the configuration information (in the form of a map) |
| 103 | // about a particular instance. |
Jamie Hannaford | 936a547 | 2015-02-10 14:38:28 +0100 | [diff] [blame] | 104 | func (r ConfigResult) Extract() (map[string]string, error) { |
Jamie Hannaford | f77fc10 | 2015-02-10 14:56:02 +0100 | [diff] [blame] | 105 | if r.Err != nil { |
| 106 | return nil, r.Err |
Jamie Hannaford | 936a547 | 2015-02-10 14:38:28 +0100 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | var response struct { |
| 110 | Instance struct { |
| 111 | Config map[string]string `mapstructure:"configuration"` |
| 112 | } `mapstructure:"instance"` |
| 113 | } |
| 114 | |
Jamie Hannaford | f77fc10 | 2015-02-10 14:56:02 +0100 | [diff] [blame] | 115 | err := mapstructure.Decode(r.Body, &response) |
| 116 | return response.Instance.Config, err |
| 117 | } |
| 118 | |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 119 | // UpdateResult represents the result of an Update operation. |
Jamie Hannaford | f77fc10 | 2015-02-10 14:56:02 +0100 | [diff] [blame] | 120 | type UpdateResult struct { |
| 121 | gophercloud.ErrResult |
Jamie Hannaford | 936a547 | 2015-02-10 14:38:28 +0100 | [diff] [blame] | 122 | } |
Jamie Hannaford | 4ec6afe | 2015-02-16 16:52:49 +0100 | [diff] [blame] | 123 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame^] | 124 | // ExtractInstances retrieves a slice of instances from a paginated collection. |
Jamie Hannaford | 4ec6afe | 2015-02-16 16:52:49 +0100 | [diff] [blame] | 125 | func ExtractInstances(page pagination.Page) ([]Instance, error) { |
| 126 | casted := page.(os.InstancePage).Body |
| 127 | |
| 128 | var response struct { |
| 129 | Instances []Instance `mapstructure:"instances"` |
| 130 | } |
| 131 | |
| 132 | err := mapstructure.Decode(casted, &response) |
| 133 | return response.Instances, err |
| 134 | } |