blob: f211868a7144815d79d657c7071d2985c7086da7 [file] [log] [blame]
Jamie Hannaford9fdda582015-02-10 12:15:43 +01001package instances
2
3import (
4 "github.com/mitchellh/mapstructure"
5 "github.com/rackspace/gophercloud"
Jamie Hannaford90684242015-02-10 12:46:07 +01006 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford9fdda582015-02-10 12:15:43 +01007)
8
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +01009// Flavor represents information about a hardware flavor for a database instance.
Jamie Hannaford9fdda582015-02-10 12:15:43 +010010type Flavor struct {
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010011 // The unique identifier for a flavor.
12 ID string
13
14 // Various links which allow a user to reference the flavor.
Jamie Hannaford9fdda582015-02-10 12:15:43 +010015 Links []gophercloud.Link
16}
17
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010018// Volume represents information about an attached volume for a database instance.
Jamie Hannaford9fdda582015-02-10 12:15:43 +010019type Volume struct {
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010020 // The size in GB of the volume
Jamie Hannaford9fdda582015-02-10 12:15:43 +010021 Size int
22}
23
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010024// Instance represents a remote MySQL instance.
Jamie Hannaford9fdda582015-02-10 12:15:43 +010025type Instance struct {
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010026 // 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 Hannaford9fdda582015-02-10 12:15:43 +010041 Hostname string
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010042
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 Hannaford9fdda582015-02-10 12:15:43 +010057}
58
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010059// User represents a database user
Jamie Hannaford94164fa2015-02-10 13:58:45 +010060type User struct {
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010061 // The user name
62 Name string
63
64 // The user password
Jamie Hannaford94164fa2015-02-10 13:58:45 +010065 Password string
66}
67
Jamie Hannaford821015f2015-02-10 12:58:36 +010068type commonResult struct {
Jamie Hannaford9fdda582015-02-10 12:15:43 +010069 gophercloud.Result
70}
71
Jamie Hannaford821015f2015-02-10 12:58:36 +010072// CreateResult represents the result of a Create operation.
73type CreateResult struct {
74 commonResult
75}
76
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010077// GetResult represents the result of a Get operation.
Jamie Hannaford821015f2015-02-10 12:58:36 +010078type GetResult struct {
79 commonResult
80}
81
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010082// DeleteResult represents the result of a Delete operation.
Jamie Hannaford5b16b632015-02-10 13:36:23 +010083type DeleteResult struct {
84 gophercloud.ErrResult
85}
86
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010087// Extract will extract an Instance from various result structs.
Jamie Hannaford821015f2015-02-10 12:58:36 +010088func (r commonResult) Extract() (*Instance, error) {
Jamie Hannaford9fdda582015-02-10 12:15:43 +010089 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 Hannaford90684242015-02-10 12:46:07 +0100101
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100102// InstancePage represents a single page of a paginated instance collection.
Jamie Hannaford90684242015-02-10 12:46:07 +0100103type InstancePage struct {
104 pagination.LinkedPageBase
105}
106
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100107// IsEmpty checks to see whether the collection is empty.
Jamie Hannaford90684242015-02-10 12:46:07 +0100108func (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 Hannaford56d0c2e2015-02-12 11:50:18 +0100116// NextPageURL will retrieve the next page URL.
Jamie Hannaford90684242015-02-10 12:46:07 +0100117func (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 Hannaford56d0c2e2015-02-12 11:50:18 +0100131// ExtractInstances will convert a generic pagination struct into a more
132// relevant slice of Instance structs.
Jamie Hannaford90684242015-02-10 12:46:07 +0100133func 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 Hannaford94164fa2015-02-10 13:58:45 +0100144
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100145// UserRootResult represents the result of an operation to enable the root user.
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100146type UserRootResult struct {
147 gophercloud.Result
148}
149
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100150// Extract will extract root user information from a UserRootResult.
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100151func (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 Hannaford219ca592015-02-10 15:59:05 +0100164
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100165// 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 Hannaford219ca592015-02-10 15:59:05 +0100168type ActionResult struct {
169 gophercloud.ErrResult
170}