blob: 562f752a6b035d4838efce9c5e8af9594e89687e [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
43func (r commonResult) Extract() (*Instance, error) {
Jamie Hannaford9fdda582015-02-10 12:15:43 +010044 if r.Err != nil {
45 return nil, r.Err
46 }
47
48 var response struct {
49 Instance Instance `mapstructure:"instance"`
50 }
51
52 err := mapstructure.Decode(r.Body, &response)
53
54 return &response.Instance, err
55}
Jamie Hannaford90684242015-02-10 12:46:07 +010056
57type InstancePage struct {
58 pagination.LinkedPageBase
59}
60
61func (page InstancePage) IsEmpty() (bool, error) {
62 instances, err := ExtractInstances(page)
63 if err != nil {
64 return true, err
65 }
66 return len(instances) == 0, nil
67}
68
69func (page InstancePage) NextPageURL() (string, error) {
70 type resp struct {
71 Links []gophercloud.Link `mapstructure:"instances_links"`
72 }
73
74 var r resp
75 err := mapstructure.Decode(page.Body, &r)
76 if err != nil {
77 return "", err
78 }
79
80 return gophercloud.ExtractNextURL(r.Links)
81}
82
83func ExtractInstances(page pagination.Page) ([]Instance, error) {
84 casted := page.(InstancePage).Body
85
86 var response struct {
87 Instances []Instance `mapstructure:"instances"`
88 }
89
90 err := mapstructure.Decode(casted, &response)
91
92 return response.Instances, err
93}