Jon Perritt | 9776ef6 | 2015-03-16 17:11:22 -0600 | [diff] [blame] | 1 | package cloudnetworks |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "reflect" |
| 6 | "time" |
| 7 | |
Jon Perritt | 9776ef6 | 2015-03-16 17:11:22 -0600 | [diff] [blame] | 8 | "github.com/mitchellh/mapstructure" |
Jon Perritt | d91fffb | 2015-03-16 21:17:06 -0600 | [diff] [blame^] | 9 | "github.com/rackspace/gophercloud" |
Jon Perritt | 9776ef6 | 2015-03-16 17:11:22 -0600 | [diff] [blame] | 10 | "github.com/rackspace/gophercloud/pagination" |
| 11 | ) |
| 12 | |
| 13 | // CloudNetwork represents a network associated with a RackConnect configuration. |
| 14 | type 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. |
| 29 | type CloudNetworkPage struct { |
| 30 | pagination.SinglePageBase |
| 31 | } |
| 32 | |
| 33 | // IsEmpty returns true if a CloudNetworkPage contains no CloudNetworks. |
| 34 | func (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. |
| 44 | func ExtractCloudNetworks(page pagination.Page) ([]CloudNetwork, error) { |
| 45 | var res []CloudNetwork |
| 46 | casted := page.(CloudNetworkPage).Body |
| 47 | |
| 48 | err := mapstructure.Decode(casted, &res) |
| 49 | |
| 50 | var rawNets []interface{} |
| 51 | switch casted.(type) { |
| 52 | case interface{}: |
| 53 | rawNets = casted.([]interface{}) |
| 54 | default: |
| 55 | return res, fmt.Errorf("Unknown type: %v", reflect.TypeOf(casted)) |
| 56 | } |
| 57 | |
| 58 | for i := range rawNets { |
| 59 | thisNet := (rawNets[i]).(map[string]interface{}) |
| 60 | |
| 61 | if t, ok := thisNet["created"].(string); ok && t != "" { |
| 62 | creationTime, err := time.Parse(time.RFC3339, t) |
| 63 | if err != nil { |
| 64 | return res, err |
| 65 | } |
| 66 | res[i].CreatedAt = creationTime |
| 67 | } |
| 68 | |
| 69 | if t, ok := thisNet["updated"].(string); ok && t != "" { |
| 70 | updatedTime, err := time.Parse(time.RFC3339, t) |
| 71 | if err != nil { |
| 72 | return res, err |
| 73 | } |
| 74 | res[i].UpdatedAt = updatedTime |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | return res, err |
| 79 | } |
| 80 | |
| 81 | // GetResult represents the result of a Get operation. |
| 82 | type GetResult struct { |
| 83 | gophercloud.Result |
| 84 | } |
| 85 | |
| 86 | // Extract is a function that extracts a CloudNetwork from a GetResult. |
| 87 | func (r GetResult) Extract() (*CloudNetwork, error) { |
| 88 | if r.Err != nil { |
| 89 | return nil, r.Err |
| 90 | } |
| 91 | var res CloudNetwork |
| 92 | |
| 93 | err := mapstructure.Decode(r.Body, &res) |
| 94 | |
| 95 | b := r.Body.(map[string]interface{}) |
| 96 | |
| 97 | if date, ok := b["created"]; ok && date != nil { |
| 98 | t, err := time.Parse(time.RFC3339, date.(string)) |
| 99 | if err != nil { |
| 100 | return nil, err |
| 101 | } |
| 102 | res.CreatedAt = t |
| 103 | } |
| 104 | |
| 105 | if date, ok := b["updated"]; ok && date != nil { |
| 106 | t, err := time.Parse(time.RFC3339, date.(string)) |
| 107 | if err != nil { |
| 108 | return nil, err |
| 109 | } |
| 110 | res.UpdatedAt = t |
| 111 | } |
| 112 | |
| 113 | return &res, err |
| 114 | } |