blob: f554a0d75bd6ac1932a11f104b5dc753e3ff9082 [file] [log] [blame]
Jon Perritt9776ef62015-03-16 17:11:22 -06001package cloudnetworks
2
3import (
4 "fmt"
5 "reflect"
6 "time"
7
Jon Perritt9776ef62015-03-16 17:11:22 -06008 "github.com/mitchellh/mapstructure"
Jon Perrittd91fffb2015-03-16 21:17:06 -06009 "github.com/rackspace/gophercloud"
Jon Perritt9776ef62015-03-16 17:11:22 -060010 "github.com/rackspace/gophercloud/pagination"
11)
12
13// CloudNetwork represents a network associated with a RackConnect configuration.
14type CloudNetwork struct {
15 // Specifies the ID of the newtork.
16 ID string `mapstructure:"id"`
17 // Specifies the user-provided name of the network.
18 Name string `mapstructure:"name"`
19 // Specifies the IP range for this network.
20 CIDR string `mapstructure:"cidr"`
21 // Specifies the time the network was created.
22 CreatedAt time.Time `mapstructure:"-"`
23 // Specifies the time the network was last updated.
24 UpdatedAt time.Time `mapstructure:"-"`
25}
26
27// CloudNetworkPage is the page returned by a pager when traversing over a
28// collection of CloudNetworks.
29type CloudNetworkPage struct {
30 pagination.SinglePageBase
31}
32
33// IsEmpty returns true if a CloudNetworkPage contains no CloudNetworks.
34func (r CloudNetworkPage) IsEmpty() (bool, error) {
35 cns, err := ExtractCloudNetworks(r)
36 if err != nil {
37 return true, err
38 }
39 return len(cns) == 0, nil
40}
41
42// ExtractCloudNetworks extracts and returns CloudNetworks. It is used while iterating over
43// a cloudnetworks.List call.
44func ExtractCloudNetworks(page pagination.Page) ([]CloudNetwork, error) {
45 var res []CloudNetwork
46 casted := page.(CloudNetworkPage).Body
Jon Perritt9776ef62015-03-16 17:11:22 -060047 err := mapstructure.Decode(casted, &res)
48
49 var rawNets []interface{}
50 switch casted.(type) {
51 case interface{}:
52 rawNets = casted.([]interface{})
53 default:
54 return res, fmt.Errorf("Unknown type: %v", reflect.TypeOf(casted))
55 }
56
57 for i := range rawNets {
58 thisNet := (rawNets[i]).(map[string]interface{})
59
60 if t, ok := thisNet["created"].(string); ok && t != "" {
61 creationTime, err := time.Parse(time.RFC3339, t)
62 if err != nil {
63 return res, err
64 }
65 res[i].CreatedAt = creationTime
66 }
67
68 if t, ok := thisNet["updated"].(string); ok && t != "" {
69 updatedTime, err := time.Parse(time.RFC3339, t)
70 if err != nil {
71 return res, err
72 }
73 res[i].UpdatedAt = updatedTime
74 }
75 }
76
77 return res, err
78}
79
80// GetResult represents the result of a Get operation.
81type GetResult struct {
82 gophercloud.Result
83}
84
85// Extract is a function that extracts a CloudNetwork from a GetResult.
86func (r GetResult) Extract() (*CloudNetwork, error) {
87 if r.Err != nil {
88 return nil, r.Err
89 }
90 var res CloudNetwork
91
92 err := mapstructure.Decode(r.Body, &res)
93
94 b := r.Body.(map[string]interface{})
95
96 if date, ok := b["created"]; ok && date != nil {
97 t, err := time.Parse(time.RFC3339, date.(string))
98 if err != nil {
99 return nil, err
100 }
101 res.CreatedAt = t
102 }
103
104 if date, ok := b["updated"]; ok && date != nil {
105 t, err := time.Parse(time.RFC3339, date.(string))
106 if err != nil {
107 return nil, err
108 }
109 res.UpdatedAt = t
110 }
111
112 return &res, err
113}