blob: 2334faf19c69befae47af08d54f518dff8114e3d [file] [log] [blame]
Jon Perritt24270c42014-10-21 21:11:04 -05001package networks
2
3import (
4 "github.com/mitchellh/mapstructure"
5 "github.com/rackspace/gophercloud"
6 "github.com/rackspace/gophercloud/pagination"
7)
8
9type commonResult struct {
10 gophercloud.Result
11}
12
13// Extract is a function that accepts a result and extracts a network resource.
14func (r commonResult) Extract() (*Network, error) {
15 if r.Err != nil {
16 return nil, r.Err
17 }
18
19 var res struct {
20 Network *Network `json:"network"`
21 }
22
23 err := mapstructure.Decode(r.Body, &res)
24
25 return res.Network, err
26}
27
28// CreateResult represents the result of a create operation.
29type CreateResult struct {
30 commonResult
31}
32
33// GetResult represents the result of a get operation.
34type GetResult struct {
35 commonResult
36}
37
38// DeleteResult represents the result of a delete operation.
39type DeleteResult commonResult
40
41// Network represents, well, a network.
42type Network struct {
43 // UUID for the network
44 ID string `mapstructure:"id" json:"id"`
45
46 // Human-readable name for the network. Might not be unique.
47 Label string `mapstructure:"label" json:"label"`
48
49 // Classless Inter-Domain Routing
50 CIDR string `mapstructure:"cidr" json:"cidr"`
51}
52
53// NetworkPage is the page returned by a pager when traversing over a
54// collection of networks.
55type NetworkPage struct {
56 pagination.SinglePageBase
57}
58
59// ExtractNetworks accepts a Page struct, specifically a NetworkPage struct,
60// and extracts the elements into a slice of Network structs. In other words,
61// a generic collection is mapped into a relevant slice.
62func ExtractNetworks(page pagination.Page) ([]Network, error) {
63 var resp struct {
64 Networks []Network `mapstructure:"networks" json:"networks"`
65 }
66
67 err := mapstructure.Decode(page.(NetworkPage).Body, &resp)
68
69 return resp.Networks, err
70}