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