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 | |
esalipe | 84e358b | 2017-01-19 02:19:23 +0200 | [diff] [blame] | 48 | // The IP addresses associated with the database instance |
| 49 | // Is empty if the instance has a hostname |
| 50 | IP []string |
| 51 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 52 | // Indicates the unique identifier for the instance resource. |
| 53 | ID string |
| 54 | |
| 55 | // Exposes various links that reference the instance resource. |
| 56 | Links []gophercloud.Link |
| 57 | |
| 58 | // The human-readable name of the instance. |
| 59 | Name string |
| 60 | |
| 61 | // The build status of the instance. |
| 62 | Status string |
| 63 | |
| 64 | // Information about the attached volume of the instance. |
| 65 | Volume Volume |
Jamie Hannaford | 99eced5 | 2015-03-02 15:24:22 +0100 | [diff] [blame] | 66 | |
| 67 | // Indicates how the instance stores data. |
| 68 | Datastore datastores.DatastorePartial |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 69 | } |
| 70 | |
esalipe | 58d1502 | 2017-01-13 19:31:08 +0200 | [diff] [blame] | 71 | func (r *Instance) UnmarshalJSON(b []byte) error { |
| 72 | type tmp Instance |
| 73 | var s struct { |
| 74 | tmp |
| 75 | Created gophercloud.JSONRFC3339NoZ `json:"created"` |
| 76 | Updated gophercloud.JSONRFC3339NoZ `json:"updated"` |
| 77 | } |
| 78 | err := json.Unmarshal(b, &s) |
| 79 | if err != nil { |
| 80 | return err |
| 81 | } |
| 82 | *r = Instance(s.tmp) |
| 83 | |
| 84 | r.Created = time.Time(s.Created) |
| 85 | r.Updated = time.Time(s.Updated) |
| 86 | |
| 87 | return nil |
| 88 | } |
| 89 | |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 90 | type commonResult struct { |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 91 | gophercloud.Result |
| 92 | } |
| 93 | |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 94 | // CreateResult represents the result of a Create operation. |
| 95 | type CreateResult struct { |
| 96 | commonResult |
| 97 | } |
| 98 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 99 | // GetResult represents the result of a Get operation. |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 100 | type GetResult struct { |
| 101 | commonResult |
| 102 | } |
| 103 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 104 | // DeleteResult represents the result of a Delete operation. |
Jamie Hannaford | 5b16b63 | 2015-02-10 13:36:23 +0100 | [diff] [blame] | 105 | type DeleteResult struct { |
| 106 | gophercloud.ErrResult |
| 107 | } |
| 108 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 109 | // Extract will extract an Instance from various result structs. |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 110 | func (r commonResult) Extract() (*Instance, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 111 | var s struct { |
| 112 | Instance *Instance `json:"instance"` |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 113 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 114 | err := r.ExtractInto(&s) |
| 115 | return s.Instance, err |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 116 | } |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 117 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 118 | // InstancePage represents a single page of a paginated instance collection. |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 119 | type InstancePage struct { |
| 120 | pagination.LinkedPageBase |
| 121 | } |
| 122 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 123 | // IsEmpty checks to see whether the collection is empty. |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 124 | func (page InstancePage) IsEmpty() (bool, error) { |
| 125 | instances, err := ExtractInstances(page) |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 126 | return len(instances) == 0, err |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 127 | } |
| 128 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 129 | // NextPageURL will retrieve the next page URL. |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 130 | func (page InstancePage) NextPageURL() (string, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 131 | var s struct { |
| 132 | Links []gophercloud.Link `json:"instances_links"` |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 133 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 134 | err := page.ExtractInto(&s) |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 135 | if err != nil { |
| 136 | return "", err |
| 137 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 138 | return gophercloud.ExtractNextURL(s.Links) |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 139 | } |
| 140 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 141 | // ExtractInstances will convert a generic pagination struct into a more |
| 142 | // relevant slice of Instance structs. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 143 | func ExtractInstances(r pagination.Page) ([]Instance, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 144 | var s struct { |
| 145 | Instances []Instance `json:"instances"` |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 146 | } |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 147 | err := (r.(InstancePage)).ExtractInto(&s) |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 148 | return s.Instances, err |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 149 | } |
Jamie Hannaford | 94164fa | 2015-02-10 13:58:45 +0100 | [diff] [blame] | 150 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 151 | // EnableRootUserResult represents the result of an operation to enable the root user. |
| 152 | type EnableRootUserResult struct { |
Jamie Hannaford | 94164fa | 2015-02-10 13:58:45 +0100 | [diff] [blame] | 153 | gophercloud.Result |
| 154 | } |
| 155 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 156 | // Extract will extract root user information from a UserRootResult. |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 157 | func (r EnableRootUserResult) Extract() (*users.User, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 158 | var s struct { |
| 159 | User *users.User `json:"user"` |
Jamie Hannaford | 94164fa | 2015-02-10 13:58:45 +0100 | [diff] [blame] | 160 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 161 | err := r.ExtractInto(&s) |
| 162 | return s.User, err |
Jamie Hannaford | 94164fa | 2015-02-10 13:58:45 +0100 | [diff] [blame] | 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 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 171 | |
| 172 | // IsRootEnabledResult is the result of a call to IsRootEnabled. To see if |
| 173 | // root is enabled, call the type's Extract method. |
| 174 | type IsRootEnabledResult struct { |
| 175 | gophercloud.Result |
| 176 | } |
| 177 | |
| 178 | // Extract is used to extract the data from a IsRootEnabledResult. |
| 179 | func (r IsRootEnabledResult) Extract() (bool, error) { |
| 180 | return r.Body.(map[string]interface{})["rootEnabled"] == true, r.Err |
| 181 | } |