blob: 78bdff5df1f56e706c21d94e8947519e7b2d8b44 [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 Hannaford79475052014-09-15 17:08:06 +02008type NetworkProvider struct {
9 ProviderSegmentationID int `json:"provider:segmentation_id"`
Jamie Hannaforda7f671a2014-09-11 10:25:08 +020010 ProviderPhysicalNetwork string `json:"provider:physical_network"`
Jamie Hannaford79475052014-09-15 17:08:06 +020011 ProviderNetworkType string `json:"provider:network_type"`
12}
13
14type Network struct {
Jamie Hannaford965ae702014-09-22 14:58:19 +020015 // UUID for the network
16 ID string `mapstructure:"id" json:"id"`
17 // Human-readable name for the network. Might not be unique.
18 Name string `mapstructure:"name" json:"name"`
19 // The administrative state of network. If false (down), the network does not forward packets.
20 AdminStateUp bool `mapstructure:"admin_state_up" json:"admin_state_up"`
21 // Indicates whether network is currently operational. Possible values include
22 // `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional values.
23 Status string `mapstructure:"status" json:"status"`
24 // Subnets associated with this network.
25 Subnets []string `mapstructure:"subnets" json:"subnets"`
26 // Owner of network. Only admin users can specify a tenant_id other than its own.
27 TenantID string `mapstructure:"tenant_id" json:"tenant_id"`
28 // Specifies whether the network resource can be accessed by any tenant or not.
29 Shared bool `mapstructure:"shared" json:"shared"`
30
31 ProviderSegmentationID int `mapstructure:"provider:segmentation_id" json:"provider:segmentation_id"`
32 ProviderPhysicalNetwork string `mapstructure:"provider:physical_network" json:"provider:physical_network"`
33 ProviderNetworkType string `mapstructure:"provider:network_type" json:"provider:network_type"`
34 RouterExternal bool `mapstructure:"router:external" json:"router:external"`
Jamie Hannaford79475052014-09-15 17:08:06 +020035}
36
37type NetworkCreateResult struct {
38 Network
39 Segments []NetworkProvider `json:"segments"`
40 PortSecurityEnabled bool `json:"port_security_enabled"`
Jamie Hannaforda7f671a2014-09-11 10:25:08 +020041}
Jamie Hannaford4721abc2014-09-16 16:29:04 +020042
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020043type NetworkPage struct {
44 pagination.LinkedPageBase
45}
46
47func (current NetworkPage) NextPageURL() (string, error) {
48 type link struct {
49 Href string `mapstructure:"href"`
50 Rel string `mapstructure:"rel"`
51 }
52 type resp struct {
53 Links []link `mapstructure:"networks_links"`
54 }
55
56 var r resp
57 err := mapstructure.Decode(current.Body, &r)
58 if err != nil {
59 return "", err
60 }
61
62 var url string
63 for _, l := range r.Links {
64 if l.Rel == "next" {
65 url = l.Href
66 }
67 }
68 if url == "" {
69 return "", nil
70 }
71
72 return url, nil
73}
74
75func (r NetworkPage) IsEmpty() (bool, error) {
76 is, err := ExtractNetworks(r)
77 if err != nil {
78 return true, nil
79 }
80 return len(is) == 0, nil
81}
82
83func ExtractNetworks(page pagination.Page) ([]Network, error) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020084 var resp struct {
85 Networks []Network `mapstructure:"networks" json:"networks"`
86 }
87
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020088 err := mapstructure.Decode(page.(NetworkPage).Body, &resp)
Jamie Hannaford4721abc2014-09-16 16:29:04 +020089 if err != nil {
90 return nil, err
91 }
92
93 return resp.Networks, nil
94}