blob: 7f9e51a621a00a64236ae802c00ed16192468b9c [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// NetworkProvider represents provider extension data
Jamie Hannaford79475052014-09-15 17:08:06 +02009type NetworkProvider struct {
10 ProviderSegmentationID int `json:"provider:segmentation_id"`
Jamie Hannaforda7f671a2014-09-11 10:25:08 +020011 ProviderPhysicalNetwork string `json:"provider:physical_network"`
Jamie Hannaford79475052014-09-15 17:08:06 +020012 ProviderNetworkType string `json:"provider:network_type"`
13}
14
Jamie Hannaford686c4962014-09-23 10:46:20 +020015// Network represents, well, a network.
Jamie Hannaford79475052014-09-15 17:08:06 +020016type Network struct {
Jamie Hannaford965ae702014-09-22 14:58:19 +020017 // UUID for the network
18 ID string `mapstructure:"id" json:"id"`
19 // Human-readable name for the network. Might not be unique.
20 Name string `mapstructure:"name" json:"name"`
21 // The administrative state of network. If false (down), the network does not forward packets.
22 AdminStateUp bool `mapstructure:"admin_state_up" json:"admin_state_up"`
23 // Indicates whether network is currently operational. Possible values include
24 // `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional values.
25 Status string `mapstructure:"status" json:"status"`
26 // Subnets associated with this network.
27 Subnets []string `mapstructure:"subnets" json:"subnets"`
28 // Owner of network. Only admin users can specify a tenant_id other than its own.
29 TenantID string `mapstructure:"tenant_id" json:"tenant_id"`
30 // Specifies whether the network resource can be accessed by any tenant or not.
31 Shared bool `mapstructure:"shared" json:"shared"`
32
33 ProviderSegmentationID int `mapstructure:"provider:segmentation_id" json:"provider:segmentation_id"`
34 ProviderPhysicalNetwork string `mapstructure:"provider:physical_network" json:"provider:physical_network"`
35 ProviderNetworkType string `mapstructure:"provider:network_type" json:"provider:network_type"`
36 RouterExternal bool `mapstructure:"router:external" json:"router:external"`
Jamie Hannaford79475052014-09-15 17:08:06 +020037}
38
Jamie Hannaford686c4962014-09-23 10:46:20 +020039// NetworkCreateResult represents what is returned by a create operation.
Jamie Hannaford79475052014-09-15 17:08:06 +020040type NetworkCreateResult struct {
41 Network
42 Segments []NetworkProvider `json:"segments"`
43 PortSecurityEnabled bool `json:"port_security_enabled"`
Jamie Hannaforda7f671a2014-09-11 10:25:08 +020044}
Jamie Hannaford4721abc2014-09-16 16:29:04 +020045
Jamie Hannaford686c4962014-09-23 10:46:20 +020046// NetworkPage is the page returned by a pager when traversing over a
47// collection of networks.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020048type NetworkPage struct {
49 pagination.LinkedPageBase
50}
51
Jamie Hannaford686c4962014-09-23 10:46:20 +020052// NextPageURL is invoked when a paginated collection of networks has reached
53// the end of a page and the pager seeks to traverse over a new one. In order
54// to do this, it needs to construct the next page's URL.
55func (p NetworkPage) NextPageURL() (string, error) {
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020056 type link struct {
57 Href string `mapstructure:"href"`
58 Rel string `mapstructure:"rel"`
59 }
60 type resp struct {
61 Links []link `mapstructure:"networks_links"`
62 }
63
64 var r resp
Jamie Hannaford686c4962014-09-23 10:46:20 +020065 err := mapstructure.Decode(p.Body, &r)
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020066 if err != nil {
67 return "", err
68 }
69
70 var url string
71 for _, l := range r.Links {
72 if l.Rel == "next" {
73 url = l.Href
74 }
75 }
76 if url == "" {
77 return "", nil
78 }
79
80 return url, nil
81}
82
Jamie Hannaford686c4962014-09-23 10:46:20 +020083// IsEmpty checks whether a NetworkPage struct is empty.
84func (p NetworkPage) IsEmpty() (bool, error) {
85 is, err := ExtractNetworks(p)
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020086 if err != nil {
87 return true, nil
88 }
89 return len(is) == 0, nil
90}
91
Jamie Hannaford686c4962014-09-23 10:46:20 +020092// ExtractNetworks accepts a Page struct, specifically a NetworkPage struct,
93// and extracts the elements into a slice of Network structs. In other words,
94// a generic collection is mapped into a relevant slice.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020095func ExtractNetworks(page pagination.Page) ([]Network, error) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020096 var resp struct {
97 Networks []Network `mapstructure:"networks" json:"networks"`
98 }
99
Jamie Hannafordf0c615b2014-09-17 10:56:52 +0200100 err := mapstructure.Decode(page.(NetworkPage).Body, &resp)
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200101 if err != nil {
102 return nil, err
103 }
104
105 return resp.Networks, nil
106}