blob: 60f0c0416b093bd17a3e980efe610139d65cd5a7 [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
9type Flavor struct {
10 ID string
11 Links []gophercloud.Link
12}
13
14type Volume struct {
15 Size int
16}
17
18type Instance struct {
19 Created string //time.Time
20 Updated string //time.Time
21 Flavor Flavor
22 Hostname string
23 ID string
24 Links []gophercloud.Link
25 Name string
26 Status string
27 Volume Volume
28}
29
Jamie Hannaford94164fa2015-02-10 13:58:45 +010030type User struct {
31 Name string
32 Password string
33}
34
Jamie Hannaford821015f2015-02-10 12:58:36 +010035type commonResult struct {
Jamie Hannaford9fdda582015-02-10 12:15:43 +010036 gophercloud.Result
37}
38
Jamie Hannaford821015f2015-02-10 12:58:36 +010039// CreateResult represents the result of a Create operation.
40type CreateResult struct {
41 commonResult
42}
43
44type GetResult struct {
45 commonResult
46}
47
Jamie Hannaford5b16b632015-02-10 13:36:23 +010048type DeleteResult struct {
49 gophercloud.ErrResult
50}
51
Jamie Hannaford821015f2015-02-10 12:58:36 +010052func (r commonResult) Extract() (*Instance, error) {
Jamie Hannaford9fdda582015-02-10 12:15:43 +010053 if r.Err != nil {
54 return nil, r.Err
55 }
56
57 var response struct {
58 Instance Instance `mapstructure:"instance"`
59 }
60
61 err := mapstructure.Decode(r.Body, &response)
62
63 return &response.Instance, err
64}
Jamie Hannaford90684242015-02-10 12:46:07 +010065
66type InstancePage struct {
67 pagination.LinkedPageBase
68}
69
70func (page InstancePage) IsEmpty() (bool, error) {
71 instances, err := ExtractInstances(page)
72 if err != nil {
73 return true, err
74 }
75 return len(instances) == 0, nil
76}
77
78func (page InstancePage) NextPageURL() (string, error) {
79 type resp struct {
80 Links []gophercloud.Link `mapstructure:"instances_links"`
81 }
82
83 var r resp
84 err := mapstructure.Decode(page.Body, &r)
85 if err != nil {
86 return "", err
87 }
88
89 return gophercloud.ExtractNextURL(r.Links)
90}
91
92func ExtractInstances(page pagination.Page) ([]Instance, error) {
93 casted := page.(InstancePage).Body
94
95 var response struct {
96 Instances []Instance `mapstructure:"instances"`
97 }
98
99 err := mapstructure.Decode(casted, &response)
100
101 return response.Instances, err
102}
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100103
104type UserRootResult struct {
105 gophercloud.Result
106}
107
108func (r UserRootResult) Extract() (*User, error) {
109 if r.Err != nil {
110 return nil, r.Err
111 }
112
113 var response struct {
114 User User `mapstructure:"user"`
115 }
116
117 err := mapstructure.Decode(r.Body, &response)
118
119 return &response.User, err
120}