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