blob: d2ea79480332cd21dd455083f58220364e11fbaa [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 Hannaford821015f2015-02-10 12:58:36 +010030type commonResult struct {
Jamie Hannaford9fdda582015-02-10 12:15:43 +010031 gophercloud.Result
32}
33
Jamie Hannaford821015f2015-02-10 12:58:36 +010034// CreateResult represents the result of a Create operation.
35type CreateResult struct {
36 commonResult
37}
38
39type GetResult struct {
40 commonResult
41}
42
Jamie Hannaford5b16b632015-02-10 13:36:23 +010043type DeleteResult struct {
44 gophercloud.ErrResult
45}
46
Jamie Hannaford821015f2015-02-10 12:58:36 +010047func (r commonResult) Extract() (*Instance, error) {
Jamie Hannaford9fdda582015-02-10 12:15:43 +010048 if r.Err != nil {
49 return nil, r.Err
50 }
51
52 var response struct {
53 Instance Instance `mapstructure:"instance"`
54 }
55
56 err := mapstructure.Decode(r.Body, &response)
57
58 return &response.Instance, err
59}
Jamie Hannaford90684242015-02-10 12:46:07 +010060
61type InstancePage struct {
62 pagination.LinkedPageBase
63}
64
65func (page InstancePage) IsEmpty() (bool, error) {
66 instances, err := ExtractInstances(page)
67 if err != nil {
68 return true, err
69 }
70 return len(instances) == 0, nil
71}
72
73func (page InstancePage) NextPageURL() (string, error) {
74 type resp struct {
75 Links []gophercloud.Link `mapstructure:"instances_links"`
76 }
77
78 var r resp
79 err := mapstructure.Decode(page.Body, &r)
80 if err != nil {
81 return "", err
82 }
83
84 return gophercloud.ExtractNextURL(r.Links)
85}
86
87func ExtractInstances(page pagination.Page) ([]Instance, error) {
88 casted := page.(InstancePage).Body
89
90 var response struct {
91 Instances []Instance `mapstructure:"instances"`
92 }
93
94 err := mapstructure.Decode(casted, &response)
95
96 return response.Instances, err
97}