blob: 33c68c17e15030fb8d91b837d96ea4ce1b8ebde1 [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 Hannaford2ca55d82015-02-12 14:21:55 +01006 "github.com/rackspace/gophercloud/openstack/db/v1/users"
Jamie Hannaford90684242015-02-10 12:46:07 +01007 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford9fdda582015-02-10 12:15:43 +01008)
9
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010010// Flavor represents information about a hardware flavor for a database instance.
Jamie Hannaford9fdda582015-02-10 12:15:43 +010011type Flavor struct {
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010012 // The unique identifier for a flavor.
13 ID string
14
15 // Various links which allow a user to reference the flavor.
Jamie Hannaford9fdda582015-02-10 12:15:43 +010016 Links []gophercloud.Link
17}
18
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010019// Volume represents information about an attached volume for a database instance.
Jamie Hannaford9fdda582015-02-10 12:15:43 +010020type Volume struct {
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010021 // The size in GB of the volume
Jamie Hannaford9fdda582015-02-10 12:15:43 +010022 Size int
23}
24
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010025// Instance represents a remote MySQL instance.
Jamie Hannaford9fdda582015-02-10 12:15:43 +010026type Instance struct {
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010027 // 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 Hannaford9fdda582015-02-10 12:15:43 +010042 Hostname string
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010043
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 Hannaford9fdda582015-02-10 12:15:43 +010058}
59
Jamie Hannaford821015f2015-02-10 12:58:36 +010060type commonResult struct {
Jamie Hannaford9fdda582015-02-10 12:15:43 +010061 gophercloud.Result
62}
63
Jamie Hannaford821015f2015-02-10 12:58:36 +010064// CreateResult represents the result of a Create operation.
65type CreateResult struct {
66 commonResult
67}
68
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010069// GetResult represents the result of a Get operation.
Jamie Hannaford821015f2015-02-10 12:58:36 +010070type GetResult struct {
71 commonResult
72}
73
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010074// DeleteResult represents the result of a Delete operation.
Jamie Hannaford5b16b632015-02-10 13:36:23 +010075type DeleteResult struct {
76 gophercloud.ErrResult
77}
78
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010079// Extract will extract an Instance from various result structs.
Jamie Hannaford821015f2015-02-10 12:58:36 +010080func (r commonResult) Extract() (*Instance, error) {
Jamie Hannaford9fdda582015-02-10 12:15:43 +010081 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 Hannaford90684242015-02-10 12:46:07 +010093
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010094// InstancePage represents a single page of a paginated instance collection.
Jamie Hannaford90684242015-02-10 12:46:07 +010095type InstancePage struct {
96 pagination.LinkedPageBase
97}
98
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010099// IsEmpty checks to see whether the collection is empty.
Jamie Hannaford90684242015-02-10 12:46:07 +0100100func (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 Hannaford56d0c2e2015-02-12 11:50:18 +0100108// NextPageURL will retrieve the next page URL.
Jamie Hannaford90684242015-02-10 12:46:07 +0100109func (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 Hannaford56d0c2e2015-02-12 11:50:18 +0100123// ExtractInstances will convert a generic pagination struct into a more
124// relevant slice of Instance structs.
Jamie Hannaford90684242015-02-10 12:46:07 +0100125func 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 Hannaford94164fa2015-02-10 13:58:45 +0100136
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100137// UserRootResult represents the result of an operation to enable the root user.
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100138type UserRootResult struct {
139 gophercloud.Result
140}
141
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100142// Extract will extract root user information from a UserRootResult.
Jamie Hannaford3aba0b12015-02-13 14:33:39 +0100143func (r UserRootResult) Extract() (*users.User, error) {
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100144 if r.Err != nil {
145 return nil, r.Err
146 }
147
148 var response struct {
Jamie Hannaford2ca55d82015-02-12 14:21:55 +0100149 User users.User `mapstructure:"user"`
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100150 }
151
152 err := mapstructure.Decode(r.Body, &response)
153
154 return &response.User, err
155}
Jamie Hannaford219ca592015-02-10 15:59:05 +0100156
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100157// 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 Hannaford219ca592015-02-10 15:59:05 +0100160type ActionResult struct {
161 gophercloud.ErrResult
162}