blob: 03cfb3110a972ddfa89ccbc160e11db0a99a67d9 [file] [log] [blame]
Jamie Hannaforda7f671a2014-09-11 10:25:08 +02001package networks
2
Jamie Hannaford4721abc2014-09-16 16:29:04 +02003import (
4 "github.com/mitchellh/mapstructure"
Jamie Hannafordf0c615b2014-09-17 10:56:52 +02005 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford4721abc2014-09-16 16:29:04 +02006)
7
Jamie Hannaford686c4962014-09-23 10:46:20 +02008// Network represents, well, a network.
Jamie Hannaford79475052014-09-15 17:08:06 +02009type Network struct {
Jamie Hannaford965ae702014-09-22 14:58:19 +020010 // UUID for the network
11 ID string `mapstructure:"id" json:"id"`
12 // Human-readable name for the network. Might not be unique.
13 Name string `mapstructure:"name" json:"name"`
14 // The administrative state of network. If false (down), the network does not forward packets.
15 AdminStateUp bool `mapstructure:"admin_state_up" json:"admin_state_up"`
16 // Indicates whether network is currently operational. Possible values include
17 // `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional values.
18 Status string `mapstructure:"status" json:"status"`
19 // Subnets associated with this network.
20 Subnets []string `mapstructure:"subnets" json:"subnets"`
21 // Owner of network. Only admin users can specify a tenant_id other than its own.
22 TenantID string `mapstructure:"tenant_id" json:"tenant_id"`
23 // Specifies whether the network resource can be accessed by any tenant or not.
24 Shared bool `mapstructure:"shared" json:"shared"`
Jamie Hannaforda7f671a2014-09-11 10:25:08 +020025}
Jamie Hannaford4721abc2014-09-16 16:29:04 +020026
Jamie Hannaford686c4962014-09-23 10:46:20 +020027// NetworkPage is the page returned by a pager when traversing over a
28// collection of networks.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020029type NetworkPage struct {
30 pagination.LinkedPageBase
31}
32
Jamie Hannaford686c4962014-09-23 10:46:20 +020033// NextPageURL is invoked when a paginated collection of networks has reached
34// the end of a page and the pager seeks to traverse over a new one. In order
35// to do this, it needs to construct the next page's URL.
36func (p NetworkPage) NextPageURL() (string, error) {
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020037 type link struct {
38 Href string `mapstructure:"href"`
39 Rel string `mapstructure:"rel"`
40 }
41 type resp struct {
42 Links []link `mapstructure:"networks_links"`
43 }
44
45 var r resp
Jamie Hannaford686c4962014-09-23 10:46:20 +020046 err := mapstructure.Decode(p.Body, &r)
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020047 if err != nil {
48 return "", err
49 }
50
51 var url string
52 for _, l := range r.Links {
53 if l.Rel == "next" {
54 url = l.Href
55 }
56 }
57 if url == "" {
58 return "", nil
59 }
60
61 return url, nil
62}
63
Jamie Hannaford686c4962014-09-23 10:46:20 +020064// IsEmpty checks whether a NetworkPage struct is empty.
65func (p NetworkPage) IsEmpty() (bool, error) {
66 is, err := ExtractNetworks(p)
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020067 if err != nil {
68 return true, nil
69 }
70 return len(is) == 0, nil
71}
72
Jamie Hannaford686c4962014-09-23 10:46:20 +020073// ExtractNetworks accepts a Page struct, specifically a NetworkPage struct,
74// and extracts the elements into a slice of Network structs. In other words,
75// a generic collection is mapped into a relevant slice.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020076func ExtractNetworks(page pagination.Page) ([]Network, error) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020077 var resp struct {
78 Networks []Network `mapstructure:"networks" json:"networks"`
79 }
80
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020081 err := mapstructure.Decode(page.(NetworkPage).Body, &resp)
Jamie Hannaford4721abc2014-09-16 16:29:04 +020082 if err != nil {
83 return nil, err
84 }
85
86 return resp.Networks, nil
87}