blob: 1f4b80915bcf35f4fb3291651771b4bb6d871346 [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 Hannaford4721abc2014-09-16 16:29:04 +020015 Status string `mapstructure:"status" json:"status"`
16 Subnets []interface{} `mapstructure:"subnets" json:"subnets"`
17 Name string `mapstructure:"name" json:"name"`
18 AdminStateUp bool `mapstructure:"admin_state_up" json:"admin_state_up"`
19 TenantID string `mapstructure:"tenant_id" json:"tenant_id"`
20 Shared bool `mapstructure:"shared" json:"shared"`
21 ID string `mapstructure:"id" json:"id"`
22 ProviderSegmentationID int `mapstructure:"provider:segmentation_id" json:"provider:segmentation_id"`
23 ProviderPhysicalNetwork string `mapstructure:"provider:physical_network" json:"provider:physical_network"`
24 ProviderNetworkType string `mapstructure:"provider:network_type" json:"provider:network_type"`
25 RouterExternal bool `mapstructure:"router:external" json:"router:external"`
Jamie Hannaford79475052014-09-15 17:08:06 +020026}
27
28type NetworkCreateResult struct {
29 Network
30 Segments []NetworkProvider `json:"segments"`
31 PortSecurityEnabled bool `json:"port_security_enabled"`
Jamie Hannaforda7f671a2014-09-11 10:25:08 +020032}
Jamie Hannaford4721abc2014-09-16 16:29:04 +020033
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020034type NetworkPage struct {
35 pagination.LinkedPageBase
36}
37
38func (current NetworkPage) NextPageURL() (string, error) {
39 type link struct {
40 Href string `mapstructure:"href"`
41 Rel string `mapstructure:"rel"`
42 }
43 type resp struct {
44 Links []link `mapstructure:"networks_links"`
45 }
46
47 var r resp
48 err := mapstructure.Decode(current.Body, &r)
49 if err != nil {
50 return "", err
51 }
52
53 var url string
54 for _, l := range r.Links {
55 if l.Rel == "next" {
56 url = l.Href
57 }
58 }
59 if url == "" {
60 return "", nil
61 }
62
63 return url, nil
64}
65
66func (r NetworkPage) IsEmpty() (bool, error) {
67 is, err := ExtractNetworks(r)
68 if err != nil {
69 return true, nil
70 }
71 return len(is) == 0, nil
72}
73
74func ExtractNetworks(page pagination.Page) ([]Network, error) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020075 var resp struct {
76 Networks []Network `mapstructure:"networks" json:"networks"`
77 }
78
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020079 err := mapstructure.Decode(page.(NetworkPage).Body, &resp)
Jamie Hannaford4721abc2014-09-16 16:29:04 +020080 if err != nil {
81 return nil, err
82 }
83
84 return resp.Networks, nil
85}