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