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