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