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