blob: b7eeb86f920802cfb482754042eb9e2249808e51 [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 Hannaford9793d942015-02-18 15:13:20 +01006 "github.com/rackspace/gophercloud/openstack/db/v1/flavors"
Jamie Hannaford2ca55d82015-02-12 14:21:55 +01007 "github.com/rackspace/gophercloud/openstack/db/v1/users"
Jamie Hannaford90684242015-02-10 12:46:07 +01008 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford99eced52015-03-02 15:24:22 +01009 "github.com/rackspace/gophercloud/rackspace/db/v1/datastores"
Jamie Hannaford9fdda582015-02-10 12:15:43 +010010)
11
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010012// Volume represents information about an attached volume for a database instance.
Jamie Hannaford9fdda582015-02-10 12:15:43 +010013type Volume struct {
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010014 // The size in GB of the volume
Jamie Hannaford9fdda582015-02-10 12:15:43 +010015 Size int
Jamie Hannaford76e177b2015-02-16 16:53:00 +010016
17 Used float64
Jamie Hannaford9fdda582015-02-10 12:15:43 +010018}
19
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010020// Instance represents a remote MySQL instance.
Jamie Hannaford9fdda582015-02-10 12:15:43 +010021type Instance struct {
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010022 // Indicates the datetime that the instance was created
23 Created string //time.Time
24
25 // Indicates the most recent datetime that the instance was updated.
26 Updated string //time.Time
27
28 // Indicates the hardware flavor the instance uses.
Jamie Hannaford9793d942015-02-18 15:13:20 +010029 Flavor flavors.Flavor
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010030
31 // A DNS-resolvable hostname associated with the database instance (rather
32 // than an IPv4 address). Since the hostname always resolves to the correct
33 // IP address of the database instance, this relieves the user from the task
34 // of maintaining the mapping. Note that although the IP address may likely
35 // change on resizing, migrating, and so forth, the hostname always resolves
36 // to the correct database instance.
Jamie Hannaford9fdda582015-02-10 12:15:43 +010037 Hostname string
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010038
39 // Indicates the unique identifier for the instance resource.
40 ID string
41
42 // Exposes various links that reference the instance resource.
43 Links []gophercloud.Link
44
45 // The human-readable name of the instance.
46 Name string
47
48 // The build status of the instance.
49 Status string
50
51 // Information about the attached volume of the instance.
52 Volume Volume
Jamie Hannaford99eced52015-03-02 15:24:22 +010053
54 // Indicates how the instance stores data.
55 Datastore datastores.DatastorePartial
Jamie Hannaford9fdda582015-02-10 12:15:43 +010056}
57
Jamie Hannaford821015f2015-02-10 12:58:36 +010058type commonResult struct {
Jamie Hannaford9fdda582015-02-10 12:15:43 +010059 gophercloud.Result
60}
61
Jamie Hannaford821015f2015-02-10 12:58:36 +010062// CreateResult represents the result of a Create operation.
63type CreateResult struct {
64 commonResult
65}
66
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010067// GetResult represents the result of a Get operation.
Jamie Hannaford821015f2015-02-10 12:58:36 +010068type GetResult struct {
69 commonResult
70}
71
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010072// DeleteResult represents the result of a Delete operation.
Jamie Hannaford5b16b632015-02-10 13:36:23 +010073type DeleteResult struct {
74 gophercloud.ErrResult
75}
76
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010077// Extract will extract an Instance from various result structs.
Jamie Hannaford821015f2015-02-10 12:58:36 +010078func (r commonResult) Extract() (*Instance, error) {
Jamie Hannaford9fdda582015-02-10 12:15:43 +010079 if r.Err != nil {
80 return nil, r.Err
81 }
82
83 var response struct {
84 Instance Instance `mapstructure:"instance"`
85 }
86
87 err := mapstructure.Decode(r.Body, &response)
88
89 return &response.Instance, err
90}
Jamie Hannaford90684242015-02-10 12:46:07 +010091
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010092// InstancePage represents a single page of a paginated instance collection.
Jamie Hannaford90684242015-02-10 12:46:07 +010093type InstancePage struct {
94 pagination.LinkedPageBase
95}
96
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +010097// IsEmpty checks to see whether the collection is empty.
Jamie Hannaford90684242015-02-10 12:46:07 +010098func (page InstancePage) IsEmpty() (bool, error) {
99 instances, err := ExtractInstances(page)
100 if err != nil {
101 return true, err
102 }
103 return len(instances) == 0, nil
104}
105
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100106// NextPageURL will retrieve the next page URL.
Jamie Hannaford90684242015-02-10 12:46:07 +0100107func (page InstancePage) NextPageURL() (string, error) {
108 type resp struct {
109 Links []gophercloud.Link `mapstructure:"instances_links"`
110 }
111
112 var r resp
113 err := mapstructure.Decode(page.Body, &r)
114 if err != nil {
115 return "", err
116 }
117
118 return gophercloud.ExtractNextURL(r.Links)
119}
120
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100121// ExtractInstances will convert a generic pagination struct into a more
122// relevant slice of Instance structs.
Jamie Hannaford90684242015-02-10 12:46:07 +0100123func ExtractInstances(page pagination.Page) ([]Instance, error) {
124 casted := page.(InstancePage).Body
125
126 var response struct {
127 Instances []Instance `mapstructure:"instances"`
128 }
129
130 err := mapstructure.Decode(casted, &response)
131
132 return response.Instances, err
133}
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100134
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100135// UserRootResult represents the result of an operation to enable the root user.
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100136type UserRootResult struct {
137 gophercloud.Result
138}
139
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100140// Extract will extract root user information from a UserRootResult.
Jamie Hannaford3aba0b12015-02-13 14:33:39 +0100141func (r UserRootResult) Extract() (*users.User, error) {
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100142 if r.Err != nil {
143 return nil, r.Err
144 }
145
146 var response struct {
Jamie Hannaford2ca55d82015-02-12 14:21:55 +0100147 User users.User `mapstructure:"user"`
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100148 }
149
150 err := mapstructure.Decode(r.Body, &response)
151
152 return &response.User, err
153}
Jamie Hannaford219ca592015-02-10 15:59:05 +0100154
Jamie Hannaford56d0c2e2015-02-12 11:50:18 +0100155// ActionResult represents the result of action requests, such as: restarting
156// an instance service, resizing its memory allocation, and resizing its
157// attached volume size.
Jamie Hannaford219ca592015-02-10 15:59:05 +0100158type ActionResult struct {
159 gophercloud.ErrResult
160}