Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 1 | package instances |
| 2 | |
| 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 | 9fdda58 | 2015-02-10 12:15:43 +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 | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 11 | "github.com/rackspace/gophercloud/openstack/db/v1/flavors" |
Jamie Hannaford | 2ca55d8 | 2015-02-12 14:21:55 +0100 | [diff] [blame] | 12 | "github.com/rackspace/gophercloud/openstack/db/v1/users" |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 13 | "github.com/rackspace/gophercloud/pagination" |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 14 | ) |
| 15 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 16 | // Volume represents information about an attached volume for a database instance. |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 17 | type Volume struct { |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 18 | // The size in GB of the volume |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 19 | Size int |
Jamie Hannaford | 76e177b | 2015-02-16 16:53:00 +0100 | [diff] [blame] | 20 | |
| 21 | Used float64 |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 22 | } |
| 23 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 24 | // Instance represents a remote MySQL instance. |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 25 | type Instance struct { |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 26 | // Indicates the datetime that the instance was created |
Jamie Hannaford | e65ad95 | 2015-11-16 14:05:11 +0100 | [diff] [blame] | 27 | Created time.Time `mapstructure:"-"` |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 28 | |
| 29 | // Indicates the most recent datetime that the instance was updated. |
Jamie Hannaford | e65ad95 | 2015-11-16 14:05:11 +0100 | [diff] [blame] | 30 | Updated time.Time `mapstructure:"-"` |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 31 | |
| 32 | // Indicates the hardware flavor the instance uses. |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 33 | Flavor flavors.Flavor |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 34 | |
| 35 | // A DNS-resolvable hostname associated with the database instance (rather |
| 36 | // than an IPv4 address). Since the hostname always resolves to the correct |
| 37 | // IP address of the database instance, this relieves the user from the task |
| 38 | // of maintaining the mapping. Note that although the IP address may likely |
| 39 | // change on resizing, migrating, and so forth, the hostname always resolves |
| 40 | // to the correct database instance. |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 41 | Hostname string |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 42 | |
| 43 | // Indicates the unique identifier for the instance resource. |
| 44 | ID string |
| 45 | |
| 46 | // Exposes various links that reference the instance resource. |
| 47 | Links []gophercloud.Link |
| 48 | |
| 49 | // The human-readable name of the instance. |
| 50 | Name string |
| 51 | |
| 52 | // The build status of the instance. |
| 53 | Status string |
| 54 | |
| 55 | // Information about the attached volume of the instance. |
| 56 | Volume Volume |
Jamie Hannaford | 99eced5 | 2015-03-02 15:24:22 +0100 | [diff] [blame] | 57 | |
| 58 | // Indicates how the instance stores data. |
| 59 | Datastore datastores.DatastorePartial |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 60 | } |
| 61 | |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 62 | type commonResult struct { |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 63 | gophercloud.Result |
| 64 | } |
| 65 | |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 66 | // CreateResult represents the result of a Create operation. |
| 67 | type CreateResult struct { |
| 68 | commonResult |
| 69 | } |
| 70 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 71 | // GetResult represents the result of a Get operation. |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 72 | type GetResult struct { |
| 73 | commonResult |
| 74 | } |
| 75 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 76 | // DeleteResult represents the result of a Delete operation. |
Jamie Hannaford | 5b16b63 | 2015-02-10 13:36:23 +0100 | [diff] [blame] | 77 | type DeleteResult struct { |
| 78 | gophercloud.ErrResult |
| 79 | } |
| 80 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 81 | // Extract will extract an Instance from various result structs. |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 82 | func (r commonResult) Extract() (*Instance, error) { |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 83 | if r.Err != nil { |
| 84 | return nil, r.Err |
| 85 | } |
| 86 | |
| 87 | var response struct { |
| 88 | Instance Instance `mapstructure:"instance"` |
| 89 | } |
| 90 | |
| 91 | err := mapstructure.Decode(r.Body, &response) |
Jamie Hannaford | e65ad95 | 2015-11-16 14:05:11 +0100 | [diff] [blame] | 92 | val := r.Body.(map[string]interface{})["instance"].(map[string]interface{}) |
| 93 | |
| 94 | if t, ok := val["created"].(string); ok && t != "" { |
| 95 | creationTime, err := time.Parse(time.RFC3339, t) |
| 96 | if err != nil { |
| 97 | return &response.Instance, err |
| 98 | } |
| 99 | response.Instance.Created = creationTime |
| 100 | } |
| 101 | |
| 102 | if t, ok := val["updated"].(string); ok && t != "" { |
| 103 | updatedTime, err := time.Parse(time.RFC3339, t) |
| 104 | if err != nil { |
| 105 | return &response.Instance, err |
| 106 | } |
| 107 | response.Instance.Updated = updatedTime |
| 108 | } |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 109 | |
| 110 | return &response.Instance, err |
| 111 | } |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 112 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 113 | // InstancePage represents a single page of a paginated instance collection. |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 114 | type InstancePage struct { |
| 115 | pagination.LinkedPageBase |
| 116 | } |
| 117 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 118 | // IsEmpty checks to see whether the collection is empty. |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 119 | func (page InstancePage) IsEmpty() (bool, error) { |
| 120 | instances, err := ExtractInstances(page) |
| 121 | if err != nil { |
| 122 | return true, err |
| 123 | } |
| 124 | return len(instances) == 0, nil |
| 125 | } |
| 126 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 127 | // NextPageURL will retrieve the next page URL. |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 128 | func (page InstancePage) NextPageURL() (string, error) { |
| 129 | type resp struct { |
| 130 | Links []gophercloud.Link `mapstructure:"instances_links"` |
| 131 | } |
| 132 | |
| 133 | var r resp |
| 134 | err := mapstructure.Decode(page.Body, &r) |
| 135 | if err != nil { |
| 136 | return "", err |
| 137 | } |
| 138 | |
| 139 | return gophercloud.ExtractNextURL(r.Links) |
| 140 | } |
| 141 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 142 | // ExtractInstances will convert a generic pagination struct into a more |
| 143 | // relevant slice of Instance structs. |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 144 | func ExtractInstances(page pagination.Page) ([]Instance, error) { |
| 145 | casted := page.(InstancePage).Body |
| 146 | |
Jamie Hannaford | 87704ba | 2016-01-14 11:49:56 +0100 | [diff] [blame] | 147 | var resp struct { |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 148 | Instances []Instance `mapstructure:"instances"` |
| 149 | } |
| 150 | |
Jamie Hannaford | 87704ba | 2016-01-14 11:49:56 +0100 | [diff] [blame] | 151 | if err := mapstructure.Decode(casted, &resp); err != nil { |
| 152 | return nil, err |
| 153 | } |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 154 | |
Jamie Hannaford | e65ad95 | 2015-11-16 14:05:11 +0100 | [diff] [blame] | 155 | var vals []interface{} |
Jamie Hannaford | 87704ba | 2016-01-14 11:49:56 +0100 | [diff] [blame] | 156 | switch casted.(type) { |
| 157 | case map[string]interface{}: |
Jamie Hannaford | e65ad95 | 2015-11-16 14:05:11 +0100 | [diff] [blame] | 158 | vals = casted.(map[string]interface{})["instances"].([]interface{}) |
Jamie Hannaford | 87704ba | 2016-01-14 11:49:56 +0100 | [diff] [blame] | 159 | case map[string][]interface{}: |
| 160 | vals = casted.(map[string][]interface{})["instances"] |
| 161 | default: |
| 162 | return resp.Instances, fmt.Errorf("Unknown type: %v", reflect.TypeOf(casted)) |
Jamie Hannaford | e65ad95 | 2015-11-16 14:05:11 +0100 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | for i, v := range vals { |
| 166 | val := v.(map[string]interface{}) |
| 167 | |
| 168 | if t, ok := val["created"].(string); ok && t != "" { |
| 169 | creationTime, err := time.Parse(time.RFC3339, t) |
| 170 | if err != nil { |
Jamie Hannaford | 87704ba | 2016-01-14 11:49:56 +0100 | [diff] [blame] | 171 | return resp.Instances, err |
Jamie Hannaford | e65ad95 | 2015-11-16 14:05:11 +0100 | [diff] [blame] | 172 | } |
Jamie Hannaford | 87704ba | 2016-01-14 11:49:56 +0100 | [diff] [blame] | 173 | resp.Instances[i].Created = creationTime |
Jamie Hannaford | e65ad95 | 2015-11-16 14:05:11 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | if t, ok := val["updated"].(string); ok && t != "" { |
| 177 | updatedTime, err := time.Parse(time.RFC3339, t) |
| 178 | if err != nil { |
Jamie Hannaford | 87704ba | 2016-01-14 11:49:56 +0100 | [diff] [blame] | 179 | return resp.Instances, err |
Jamie Hannaford | e65ad95 | 2015-11-16 14:05:11 +0100 | [diff] [blame] | 180 | } |
Jamie Hannaford | 87704ba | 2016-01-14 11:49:56 +0100 | [diff] [blame] | 181 | resp.Instances[i].Updated = updatedTime |
Jamie Hannaford | e65ad95 | 2015-11-16 14:05:11 +0100 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Jamie Hannaford | 87704ba | 2016-01-14 11:49:56 +0100 | [diff] [blame] | 185 | return resp.Instances, nil |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 186 | } |
Jamie Hannaford | 94164fa | 2015-02-10 13:58:45 +0100 | [diff] [blame] | 187 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 188 | // UserRootResult represents the result of an operation to enable the root user. |
Jamie Hannaford | 94164fa | 2015-02-10 13:58:45 +0100 | [diff] [blame] | 189 | type UserRootResult struct { |
| 190 | gophercloud.Result |
| 191 | } |
| 192 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 193 | // Extract will extract root user information from a UserRootResult. |
Jamie Hannaford | 3aba0b1 | 2015-02-13 14:33:39 +0100 | [diff] [blame] | 194 | func (r UserRootResult) Extract() (*users.User, error) { |
Jamie Hannaford | 94164fa | 2015-02-10 13:58:45 +0100 | [diff] [blame] | 195 | if r.Err != nil { |
| 196 | return nil, r.Err |
| 197 | } |
| 198 | |
| 199 | var response struct { |
Jamie Hannaford | 2ca55d8 | 2015-02-12 14:21:55 +0100 | [diff] [blame] | 200 | User users.User `mapstructure:"user"` |
Jamie Hannaford | 94164fa | 2015-02-10 13:58:45 +0100 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | err := mapstructure.Decode(r.Body, &response) |
| 204 | |
| 205 | return &response.User, err |
| 206 | } |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 207 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 208 | // ActionResult represents the result of action requests, such as: restarting |
| 209 | // an instance service, resizing its memory allocation, and resizing its |
| 210 | // attached volume size. |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 211 | type ActionResult struct { |
| 212 | gophercloud.ErrResult |
| 213 | } |