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