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 ( |
Jamie Hannaford | e65ad95 | 2015-11-16 14:05:11 +0100 | [diff] [blame] | 4 | "time" |
| 5 | |
Jamie Hannaford | 3dbfb2d | 2015-02-10 11:06:47 +0100 | [diff] [blame] | 6 | "github.com/mitchellh/mapstructure" |
| 7 | "github.com/rackspace/gophercloud" |
Jamie Hannaford | 52dbcee | 2015-10-06 16:09:56 +0200 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud/openstack/db/v1/datastores" |
Jamie Hannaford | 1110840 | 2015-02-23 10:31:41 +0100 | [diff] [blame] | 9 | "github.com/rackspace/gophercloud/openstack/db/v1/flavors" |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 10 | os "github.com/rackspace/gophercloud/openstack/db/v1/instances" |
Jamie Hannaford | 4ec6afe | 2015-02-16 16:52:49 +0100 | [diff] [blame] | 11 | "github.com/rackspace/gophercloud/pagination" |
Jamie Hannaford | 3dbfb2d | 2015-02-10 11:06:47 +0100 | [diff] [blame] | 12 | ) |
| 13 | |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 14 | // Instance represents a remote MySQL instance. |
Jamie Hannaford | 3dbfb2d | 2015-02-10 11:06:47 +0100 | [diff] [blame] | 15 | type Instance struct { |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 16 | // Indicates the datetime that the instance was created |
Jamie Hannaford | e65ad95 | 2015-11-16 14:05:11 +0100 | [diff] [blame] | 17 | Created time.Time `mapstructure:"-"` |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 18 | |
| 19 | // Indicates the most recent datetime that the instance was updated. |
Jamie Hannaford | e65ad95 | 2015-11-16 14:05:11 +0100 | [diff] [blame] | 20 | Updated time.Time `mapstructure:"-"` |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 21 | |
| 22 | // Indicates how the instance stores data. |
Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 23 | Datastore datastores.DatastorePartial |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 24 | |
| 25 | // Indicates the hardware flavor the instance uses. |
Jamie Hannaford | 1110840 | 2015-02-23 10:31:41 +0100 | [diff] [blame] | 26 | Flavor flavors.Flavor |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 27 | |
| 28 | // A DNS-resolvable hostname associated with the database instance (rather |
| 29 | // than an IPv4 address). Since the hostname always resolves to the correct |
| 30 | // IP address of the database instance, this relieves the user from the task |
| 31 | // of maintaining the mapping. Note that although the IP address may likely |
| 32 | // change on resizing, migrating, and so forth, the hostname always resolves |
| 33 | // to the correct database instance. |
| 34 | Hostname string |
| 35 | |
| 36 | // Indicates the unique identifier for the instance resource. |
| 37 | ID string |
| 38 | |
| 39 | // Exposes various links that reference the instance resource. |
| 40 | Links []gophercloud.Link |
| 41 | |
| 42 | // The human-readable name of the instance. |
| 43 | Name string |
| 44 | |
| 45 | // The build status of the instance. |
| 46 | Status string |
| 47 | |
| 48 | // Information about the attached volume of the instance. |
| 49 | Volume os.Volume |
Jamie Hannaford | 4ec6afe | 2015-02-16 16:52:49 +0100 | [diff] [blame] | 50 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 51 | // IP indicates the various IP addresses which allow access. |
Jamie Hannaford | 4ec6afe | 2015-02-16 16:52:49 +0100 | [diff] [blame] | 52 | IP []string |
| 53 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 54 | // Indicates whether this instance is a replica of another source instance. |
Jamie Hannaford | 4ec6afe | 2015-02-16 16:52:49 +0100 | [diff] [blame] | 55 | ReplicaOf *Instance `mapstructure:"replica_of" json:"replica_of"` |
| 56 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 57 | // Indicates whether this instance is the source of other replica instances. |
Jamie Hannaford | 4ec6afe | 2015-02-16 16:52:49 +0100 | [diff] [blame] | 58 | Replicas []Instance |
Jamie Hannaford | 3dbfb2d | 2015-02-10 11:06:47 +0100 | [diff] [blame] | 59 | } |
Jamie Hannaford | 2a4beaa | 2015-02-09 17:27:18 +0100 | [diff] [blame] | 60 | |
Jamie Hannaford | 39d4ffb | 2015-02-10 13:19:44 +0100 | [diff] [blame] | 61 | func commonExtract(err error, body interface{}) (*Instance, error) { |
| 62 | if err != nil { |
| 63 | return nil, err |
Jamie Hannaford | 3dbfb2d | 2015-02-10 11:06:47 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | var response struct { |
| 67 | Instance Instance `mapstructure:"instance"` |
| 68 | } |
| 69 | |
Jamie Hannaford | 39d4ffb | 2015-02-10 13:19:44 +0100 | [diff] [blame] | 70 | err = mapstructure.Decode(body, &response) |
Jamie Hannaford | e65ad95 | 2015-11-16 14:05:11 +0100 | [diff] [blame] | 71 | |
| 72 | val := body.(map[string]interface{})["instance"].(map[string]interface{}) |
| 73 | |
| 74 | if t, ok := val["created"].(string); ok && t != "" { |
| 75 | creationTime, err := time.Parse(time.RFC3339, t) |
| 76 | if err != nil { |
| 77 | return &response.Instance, err |
| 78 | } |
| 79 | response.Instance.Created = creationTime |
| 80 | } |
| 81 | |
| 82 | if t, ok := val["updated"].(string); ok && t != "" { |
| 83 | updatedTime, err := time.Parse(time.RFC3339, t) |
| 84 | if err != nil { |
| 85 | return &response.Instance, err |
| 86 | } |
| 87 | response.Instance.Updated = updatedTime |
| 88 | } |
| 89 | |
Jamie Hannaford | 3dbfb2d | 2015-02-10 11:06:47 +0100 | [diff] [blame] | 90 | return &response.Instance, err |
| 91 | } |
Jamie Hannaford | 39d4ffb | 2015-02-10 13:19:44 +0100 | [diff] [blame] | 92 | |
| 93 | // CreateResult represents the result of a Create operation. |
| 94 | type CreateResult struct { |
| 95 | os.CreateResult |
| 96 | } |
| 97 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 98 | // Extract will retrieve an instance from a create result. |
Jamie Hannaford | 39d4ffb | 2015-02-10 13:19:44 +0100 | [diff] [blame] | 99 | func (r CreateResult) Extract() (*Instance, error) { |
| 100 | return commonExtract(r.Err, r.Body) |
| 101 | } |
| 102 | |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 103 | // GetResult represents the result of a Get operation. |
Jamie Hannaford | 39d4ffb | 2015-02-10 13:19:44 +0100 | [diff] [blame] | 104 | type GetResult struct { |
| 105 | os.GetResult |
| 106 | } |
| 107 | |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 108 | // Extract will extract an Instance from a GetResult. |
Jamie Hannaford | 39d4ffb | 2015-02-10 13:19:44 +0100 | [diff] [blame] | 109 | func (r GetResult) Extract() (*Instance, error) { |
| 110 | return commonExtract(r.Err, r.Body) |
| 111 | } |
Jamie Hannaford | 936a547 | 2015-02-10 14:38:28 +0100 | [diff] [blame] | 112 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 113 | // ConfigResult represents the result of getting default configuration for an |
| 114 | // instance. |
Jamie Hannaford | 936a547 | 2015-02-10 14:38:28 +0100 | [diff] [blame] | 115 | type ConfigResult struct { |
| 116 | gophercloud.Result |
| 117 | } |
| 118 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 119 | // DetachResult represents the result of detaching a replica from its source. |
Jamie Hannaford | 4ec6afe | 2015-02-16 16:52:49 +0100 | [diff] [blame] | 120 | type DetachResult struct { |
| 121 | gophercloud.ErrResult |
| 122 | } |
| 123 | |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 124 | // Extract will extract the configuration information (in the form of a map) |
| 125 | // about a particular instance. |
Jamie Hannaford | 936a547 | 2015-02-10 14:38:28 +0100 | [diff] [blame] | 126 | func (r ConfigResult) Extract() (map[string]string, error) { |
Jamie Hannaford | f77fc10 | 2015-02-10 14:56:02 +0100 | [diff] [blame] | 127 | if r.Err != nil { |
| 128 | return nil, r.Err |
Jamie Hannaford | 936a547 | 2015-02-10 14:38:28 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | var response struct { |
| 132 | Instance struct { |
| 133 | Config map[string]string `mapstructure:"configuration"` |
| 134 | } `mapstructure:"instance"` |
| 135 | } |
| 136 | |
Jamie Hannaford | f77fc10 | 2015-02-10 14:56:02 +0100 | [diff] [blame] | 137 | err := mapstructure.Decode(r.Body, &response) |
| 138 | return response.Instance.Config, err |
| 139 | } |
| 140 | |
Jamie Hannaford | 27957b2 | 2015-02-12 12:50:55 +0100 | [diff] [blame] | 141 | // UpdateResult represents the result of an Update operation. |
Jamie Hannaford | f77fc10 | 2015-02-10 14:56:02 +0100 | [diff] [blame] | 142 | type UpdateResult struct { |
| 143 | gophercloud.ErrResult |
Jamie Hannaford | 936a547 | 2015-02-10 14:38:28 +0100 | [diff] [blame] | 144 | } |
Jamie Hannaford | 4ec6afe | 2015-02-16 16:52:49 +0100 | [diff] [blame] | 145 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 146 | // ExtractInstances retrieves a slice of instances from a paginated collection. |
Jamie Hannaford | 4ec6afe | 2015-02-16 16:52:49 +0100 | [diff] [blame] | 147 | func ExtractInstances(page pagination.Page) ([]Instance, error) { |
| 148 | casted := page.(os.InstancePage).Body |
| 149 | |
| 150 | var response struct { |
| 151 | Instances []Instance `mapstructure:"instances"` |
| 152 | } |
| 153 | |
| 154 | err := mapstructure.Decode(casted, &response) |
Jamie Hannaford | e65ad95 | 2015-11-16 14:05:11 +0100 | [diff] [blame] | 155 | |
| 156 | var vals []interface{} |
| 157 | switch (casted).(type) { |
| 158 | case interface{}: |
| 159 | vals = casted.(map[string]interface{})["instances"].([]interface{}) |
| 160 | } |
| 161 | |
| 162 | for i, v := range vals { |
| 163 | val := v.(map[string]interface{}) |
| 164 | |
| 165 | if t, ok := val["created"].(string); ok && t != "" { |
| 166 | creationTime, err := time.Parse(time.RFC3339, t) |
| 167 | if err != nil { |
| 168 | return response.Instances, err |
| 169 | } |
| 170 | response.Instances[i].Created = creationTime |
| 171 | } |
| 172 | |
| 173 | if t, ok := val["updated"].(string); ok && t != "" { |
| 174 | updatedTime, err := time.Parse(time.RFC3339, t) |
| 175 | if err != nil { |
| 176 | return response.Instances, err |
| 177 | } |
| 178 | response.Instances[i].Updated = updatedTime |
| 179 | } |
| 180 | } |
| 181 | |
Jamie Hannaford | 4ec6afe | 2015-02-16 16:52:49 +0100 | [diff] [blame] | 182 | return response.Instances, err |
| 183 | } |