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 | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 9 | ) |
| 10 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 11 | // Volume represents information about an attached volume for a database instance. |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 12 | type Volume struct { |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 13 | // The size in GB of the volume |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 14 | Size int |
Jamie Hannaford | 76e177b | 2015-02-16 16:53:00 +0100 | [diff] [blame] | 15 | |
| 16 | Used float64 |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 17 | } |
| 18 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 19 | // Instance represents a remote MySQL instance. |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 20 | type Instance struct { |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 21 | // Indicates the datetime that the instance was created |
| 22 | Created string //time.Time |
| 23 | |
| 24 | // Indicates the most recent datetime that the instance was updated. |
| 25 | Updated string //time.Time |
| 26 | |
| 27 | // Indicates the hardware flavor the instance uses. |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 28 | Flavor flavors.Flavor |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +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. |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 36 | Hostname string |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 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 Volume |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 52 | } |
| 53 | |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 54 | type commonResult struct { |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 55 | gophercloud.Result |
| 56 | } |
| 57 | |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 58 | // CreateResult represents the result of a Create operation. |
| 59 | type CreateResult struct { |
| 60 | commonResult |
| 61 | } |
| 62 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 63 | // GetResult represents the result of a Get operation. |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 64 | type GetResult struct { |
| 65 | commonResult |
| 66 | } |
| 67 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 68 | // DeleteResult represents the result of a Delete operation. |
Jamie Hannaford | 5b16b63 | 2015-02-10 13:36:23 +0100 | [diff] [blame] | 69 | type DeleteResult struct { |
| 70 | gophercloud.ErrResult |
| 71 | } |
| 72 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 73 | // Extract will extract an Instance from various result structs. |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 74 | func (r commonResult) Extract() (*Instance, error) { |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 75 | if r.Err != nil { |
| 76 | return nil, r.Err |
| 77 | } |
| 78 | |
| 79 | var response struct { |
| 80 | Instance Instance `mapstructure:"instance"` |
| 81 | } |
| 82 | |
| 83 | err := mapstructure.Decode(r.Body, &response) |
| 84 | |
| 85 | return &response.Instance, err |
| 86 | } |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 87 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 88 | // InstancePage represents a single page of a paginated instance collection. |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 89 | type InstancePage struct { |
| 90 | pagination.LinkedPageBase |
| 91 | } |
| 92 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 93 | // IsEmpty checks to see whether the collection is empty. |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 94 | func (page InstancePage) IsEmpty() (bool, error) { |
| 95 | instances, err := ExtractInstances(page) |
| 96 | if err != nil { |
| 97 | return true, err |
| 98 | } |
| 99 | return len(instances) == 0, nil |
| 100 | } |
| 101 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 102 | // NextPageURL will retrieve the next page URL. |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 103 | func (page InstancePage) NextPageURL() (string, error) { |
| 104 | type resp struct { |
| 105 | Links []gophercloud.Link `mapstructure:"instances_links"` |
| 106 | } |
| 107 | |
| 108 | var r resp |
| 109 | err := mapstructure.Decode(page.Body, &r) |
| 110 | if err != nil { |
| 111 | return "", err |
| 112 | } |
| 113 | |
| 114 | return gophercloud.ExtractNextURL(r.Links) |
| 115 | } |
| 116 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 117 | // ExtractInstances will convert a generic pagination struct into a more |
| 118 | // relevant slice of Instance structs. |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 119 | func ExtractInstances(page pagination.Page) ([]Instance, error) { |
| 120 | casted := page.(InstancePage).Body |
| 121 | |
| 122 | var response struct { |
| 123 | Instances []Instance `mapstructure:"instances"` |
| 124 | } |
| 125 | |
| 126 | err := mapstructure.Decode(casted, &response) |
| 127 | |
| 128 | return response.Instances, err |
| 129 | } |
Jamie Hannaford | 94164fa | 2015-02-10 13:58:45 +0100 | [diff] [blame] | 130 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 131 | // UserRootResult represents the result of an operation to enable the root user. |
Jamie Hannaford | 94164fa | 2015-02-10 13:58:45 +0100 | [diff] [blame] | 132 | type UserRootResult struct { |
| 133 | gophercloud.Result |
| 134 | } |
| 135 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 136 | // Extract will extract root user information from a UserRootResult. |
Jamie Hannaford | 3aba0b1 | 2015-02-13 14:33:39 +0100 | [diff] [blame] | 137 | func (r UserRootResult) Extract() (*users.User, error) { |
Jamie Hannaford | 94164fa | 2015-02-10 13:58:45 +0100 | [diff] [blame] | 138 | if r.Err != nil { |
| 139 | return nil, r.Err |
| 140 | } |
| 141 | |
| 142 | var response struct { |
Jamie Hannaford | 2ca55d8 | 2015-02-12 14:21:55 +0100 | [diff] [blame] | 143 | User users.User `mapstructure:"user"` |
Jamie Hannaford | 94164fa | 2015-02-10 13:58:45 +0100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | err := mapstructure.Decode(r.Body, &response) |
| 147 | |
| 148 | return &response.User, err |
| 149 | } |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 150 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 151 | // ActionResult represents the result of action requests, such as: restarting |
| 152 | // an instance service, resizing its memory allocation, and resizing its |
| 153 | // attached volume size. |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 154 | type ActionResult struct { |
| 155 | gophercloud.ErrResult |
| 156 | } |