blob: 28360b78c53fadc3033b10c845ebb339cf29431c [file] [log] [blame]
Jamie Hannaforda7f671a2014-09-11 10:25:08 +02001package networks
2
Jamie Hannaford4721abc2014-09-16 16:29:04 +02003import (
Jamie Hannafordd9036422014-09-23 17:50:24 +02004 "fmt"
5
Jamie Hannaford4721abc2014-09-16 16:29:04 +02006 "github.com/mitchellh/mapstructure"
Jamie Hannafordd9036422014-09-23 17:50:24 +02007 "github.com/rackspace/gophercloud"
Jamie Hannafordf0c615b2014-09-17 10:56:52 +02008 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford4721abc2014-09-16 16:29:04 +02009)
10
Jamie Hannafordd9036422014-09-23 17:50:24 +020011type commonResult struct {
12 gophercloud.CommonResult
13}
14
15func (r commonResult) Extract() (*Network, error) {
16 if r.Err != nil {
17 return nil, r.Err
18 }
19
20 var res struct {
21 Network *Network `json:"network"`
22 }
23
24 err := mapstructure.Decode(r.Resp, &res)
25 if err != nil {
26 return nil, fmt.Errorf("Error decoding Neutron network: %v", err)
27 }
28
29 return res.Network, nil
30}
31
32type CreateResult struct {
33 commonResult
34}
35
36type GetResult struct {
37 commonResult
38}
39
40type UpdateResult struct {
41 commonResult
42}
43
44type DeleteResult commonResult
45
Jamie Hannaford686c4962014-09-23 10:46:20 +020046// Network represents, well, a network.
Jamie Hannaford79475052014-09-15 17:08:06 +020047type Network struct {
Jamie Hannaford965ae702014-09-22 14:58:19 +020048 // UUID for the network
49 ID string `mapstructure:"id" json:"id"`
50 // Human-readable name for the network. Might not be unique.
51 Name string `mapstructure:"name" json:"name"`
52 // The administrative state of network. If false (down), the network does not forward packets.
53 AdminStateUp bool `mapstructure:"admin_state_up" json:"admin_state_up"`
54 // Indicates whether network is currently operational. Possible values include
55 // `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional values.
56 Status string `mapstructure:"status" json:"status"`
57 // Subnets associated with this network.
58 Subnets []string `mapstructure:"subnets" json:"subnets"`
59 // Owner of network. Only admin users can specify a tenant_id other than its own.
60 TenantID string `mapstructure:"tenant_id" json:"tenant_id"`
61 // Specifies whether the network resource can be accessed by any tenant or not.
62 Shared bool `mapstructure:"shared" json:"shared"`
Jamie Hannaforda7f671a2014-09-11 10:25:08 +020063}
Jamie Hannaford4721abc2014-09-16 16:29:04 +020064
Jamie Hannaford686c4962014-09-23 10:46:20 +020065// NetworkPage is the page returned by a pager when traversing over a
66// collection of networks.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020067type NetworkPage struct {
68 pagination.LinkedPageBase
69}
70
Jamie Hannaford686c4962014-09-23 10:46:20 +020071// NextPageURL is invoked when a paginated collection of networks has reached
72// the end of a page and the pager seeks to traverse over a new one. In order
73// to do this, it needs to construct the next page's URL.
74func (p NetworkPage) NextPageURL() (string, error) {
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020075 type link struct {
76 Href string `mapstructure:"href"`
77 Rel string `mapstructure:"rel"`
78 }
79 type resp struct {
80 Links []link `mapstructure:"networks_links"`
81 }
82
83 var r resp
Jamie Hannaford686c4962014-09-23 10:46:20 +020084 err := mapstructure.Decode(p.Body, &r)
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020085 if err != nil {
86 return "", err
87 }
88
89 var url string
90 for _, l := range r.Links {
91 if l.Rel == "next" {
92 url = l.Href
93 }
94 }
95 if url == "" {
96 return "", nil
97 }
98
99 return url, nil
100}
101
Jamie Hannaford686c4962014-09-23 10:46:20 +0200102// IsEmpty checks whether a NetworkPage struct is empty.
103func (p NetworkPage) IsEmpty() (bool, error) {
104 is, err := ExtractNetworks(p)
Jamie Hannafordf0c615b2014-09-17 10:56:52 +0200105 if err != nil {
106 return true, nil
107 }
108 return len(is) == 0, nil
109}
110
Jamie Hannaford686c4962014-09-23 10:46:20 +0200111// ExtractNetworks accepts a Page struct, specifically a NetworkPage struct,
112// and extracts the elements into a slice of Network structs. In other words,
113// a generic collection is mapped into a relevant slice.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +0200114func ExtractNetworks(page pagination.Page) ([]Network, error) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200115 var resp struct {
116 Networks []Network `mapstructure:"networks" json:"networks"`
117 }
118
Jamie Hannafordf0c615b2014-09-17 10:56:52 +0200119 err := mapstructure.Decode(page.(NetworkPage).Body, &resp)
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200120 if err != nil {
121 return nil, err
122 }
123
124 return resp.Networks, nil
125}