blob: 020732905aabd6e3542c607d88ccd2ae18d5ff81 [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
30// CreateResult represents the result of a Create operation.
31type CreateResult struct {
32 gophercloud.Result
33}
34
35func (r CreateResult) Extract() (*Instance, error) {
36 if r.Err != nil {
37 return nil, r.Err
38 }
39
40 var response struct {
41 Instance Instance `mapstructure:"instance"`
42 }
43
44 err := mapstructure.Decode(r.Body, &response)
45
46 return &response.Instance, err
47}
Jamie Hannaford90684242015-02-10 12:46:07 +010048
49type InstancePage struct {
50 pagination.LinkedPageBase
51}
52
53func (page InstancePage) IsEmpty() (bool, error) {
54 instances, err := ExtractInstances(page)
55 if err != nil {
56 return true, err
57 }
58 return len(instances) == 0, nil
59}
60
61func (page InstancePage) NextPageURL() (string, error) {
62 type resp struct {
63 Links []gophercloud.Link `mapstructure:"instances_links"`
64 }
65
66 var r resp
67 err := mapstructure.Decode(page.Body, &r)
68 if err != nil {
69 return "", err
70 }
71
72 return gophercloud.ExtractNextURL(r.Links)
73}
74
75func ExtractInstances(page pagination.Page) ([]Instance, error) {
76 casted := page.(InstancePage).Body
77
78 var response struct {
79 Instances []Instance `mapstructure:"instances"`
80 }
81
82 err := mapstructure.Decode(casted, &response)
83
84 return response.Instances, err
85}