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