blob: 91182a46cec8213c4ccc8b06c087da5465da28d9 [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
Jamie Hannafordf3114832014-09-24 11:00:43 +020015// Extract is a function that accepts a result and extracts a network resource.
Jamie Hannafordd9036422014-09-23 17:50:24 +020016func (r commonResult) Extract() (*Network, error) {
17 if r.Err != nil {
18 return nil, r.Err
19 }
20
21 var res struct {
22 Network *Network `json:"network"`
23 }
24
25 err := mapstructure.Decode(r.Resp, &res)
26 if err != nil {
27 return nil, fmt.Errorf("Error decoding Neutron network: %v", err)
28 }
29
30 return res.Network, nil
31}
32
Jamie Hannafordf3114832014-09-24 11:00:43 +020033// CreateResult represents the result of a create operation.
Jamie Hannafordd9036422014-09-23 17:50:24 +020034type CreateResult struct {
35 commonResult
36}
37
Jamie Hannafordf3114832014-09-24 11:00:43 +020038// GetResult represents the result of a get operation.
Jamie Hannafordd9036422014-09-23 17:50:24 +020039type GetResult struct {
40 commonResult
41}
42
Jamie Hannafordf3114832014-09-24 11:00:43 +020043// UpdateResult represents the result of an update operation.
Jamie Hannafordd9036422014-09-23 17:50:24 +020044type UpdateResult struct {
45 commonResult
46}
47
Jamie Hannafordf3114832014-09-24 11:00:43 +020048// DeleteResult represents the result of a delete operation.
Jamie Hannafordd9036422014-09-23 17:50:24 +020049type DeleteResult commonResult
50
Jamie Hannaford686c4962014-09-23 10:46:20 +020051// Network represents, well, a network.
Jamie Hannaford79475052014-09-15 17:08:06 +020052type Network struct {
Jamie Hannaford965ae702014-09-22 14:58:19 +020053 // UUID for the network
54 ID string `mapstructure:"id" json:"id"`
55 // Human-readable name for the network. Might not be unique.
56 Name string `mapstructure:"name" json:"name"`
57 // The administrative state of network. If false (down), the network does not forward packets.
58 AdminStateUp bool `mapstructure:"admin_state_up" json:"admin_state_up"`
59 // Indicates whether network is currently operational. Possible values include
60 // `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional values.
61 Status string `mapstructure:"status" json:"status"`
62 // Subnets associated with this network.
63 Subnets []string `mapstructure:"subnets" json:"subnets"`
64 // Owner of network. Only admin users can specify a tenant_id other than its own.
65 TenantID string `mapstructure:"tenant_id" json:"tenant_id"`
66 // Specifies whether the network resource can be accessed by any tenant or not.
67 Shared bool `mapstructure:"shared" json:"shared"`
Jamie Hannaforda7f671a2014-09-11 10:25:08 +020068}
Jamie Hannaford4721abc2014-09-16 16:29:04 +020069
Jamie Hannaford686c4962014-09-23 10:46:20 +020070// NetworkPage is the page returned by a pager when traversing over a
71// collection of networks.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020072type NetworkPage struct {
73 pagination.LinkedPageBase
74}
75
Jamie Hannaford686c4962014-09-23 10:46:20 +020076// NextPageURL is invoked when a paginated collection of networks has reached
77// the end of a page and the pager seeks to traverse over a new one. In order
78// to do this, it needs to construct the next page's URL.
79func (p NetworkPage) NextPageURL() (string, error) {
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020080 type link struct {
81 Href string `mapstructure:"href"`
82 Rel string `mapstructure:"rel"`
83 }
84 type resp struct {
85 Links []link `mapstructure:"networks_links"`
86 }
87
88 var r resp
Jamie Hannaford686c4962014-09-23 10:46:20 +020089 err := mapstructure.Decode(p.Body, &r)
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020090 if err != nil {
91 return "", err
92 }
93
94 var url string
95 for _, l := range r.Links {
96 if l.Rel == "next" {
97 url = l.Href
98 }
99 }
100 if url == "" {
101 return "", nil
102 }
103
104 return url, nil
105}
106
Jamie Hannaford686c4962014-09-23 10:46:20 +0200107// IsEmpty checks whether a NetworkPage struct is empty.
108func (p NetworkPage) IsEmpty() (bool, error) {
109 is, err := ExtractNetworks(p)
Jamie Hannafordf0c615b2014-09-17 10:56:52 +0200110 if err != nil {
111 return true, nil
112 }
113 return len(is) == 0, nil
114}
115
Jamie Hannaford686c4962014-09-23 10:46:20 +0200116// ExtractNetworks accepts a Page struct, specifically a NetworkPage struct,
117// and extracts the elements into a slice of Network structs. In other words,
118// a generic collection is mapped into a relevant slice.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +0200119func ExtractNetworks(page pagination.Page) ([]Network, error) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200120 var resp struct {
121 Networks []Network `mapstructure:"networks" json:"networks"`
122 }
123
Jamie Hannafordf0c615b2014-09-17 10:56:52 +0200124 err := mapstructure.Decode(page.(NetworkPage).Body, &resp)
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200125 if err != nil {
126 return nil, err
127 }
128
129 return resp.Networks, nil
130}