blob: 2dbd55f23b5968dc2909202c8b6315af55f6e690 [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"`
Jamie Hannaford9823bb62014-09-26 17:06:36 +020055
Jamie Hannaford965ae702014-09-22 14:58:19 +020056 // Human-readable name for the network. Might not be unique.
57 Name string `mapstructure:"name" json:"name"`
Jamie Hannaford9823bb62014-09-26 17:06:36 +020058
Jamie Hannaford965ae702014-09-22 14:58:19 +020059 // The administrative state of network. If false (down), the network does not forward packets.
60 AdminStateUp bool `mapstructure:"admin_state_up" json:"admin_state_up"`
Jamie Hannaford9823bb62014-09-26 17:06:36 +020061
Jamie Hannaford965ae702014-09-22 14:58:19 +020062 // Indicates whether network is currently operational. Possible values include
63 // `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional values.
64 Status string `mapstructure:"status" json:"status"`
Jamie Hannaford9823bb62014-09-26 17:06:36 +020065
Jamie Hannaford965ae702014-09-22 14:58:19 +020066 // Subnets associated with this network.
67 Subnets []string `mapstructure:"subnets" json:"subnets"`
Jamie Hannaford9823bb62014-09-26 17:06:36 +020068
Jamie Hannaford965ae702014-09-22 14:58:19 +020069 // Owner of network. Only admin users can specify a tenant_id other than its own.
70 TenantID string `mapstructure:"tenant_id" json:"tenant_id"`
Jamie Hannaford9823bb62014-09-26 17:06:36 +020071
Jamie Hannaford965ae702014-09-22 14:58:19 +020072 // Specifies whether the network resource can be accessed by any tenant or not.
73 Shared bool `mapstructure:"shared" json:"shared"`
Jamie Hannaforda7f671a2014-09-11 10:25:08 +020074}
Jamie Hannaford4721abc2014-09-16 16:29:04 +020075
Jamie Hannaford686c4962014-09-23 10:46:20 +020076// NetworkPage is the page returned by a pager when traversing over a
77// collection of networks.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020078type NetworkPage struct {
79 pagination.LinkedPageBase
80}
81
Jamie Hannaford686c4962014-09-23 10:46:20 +020082// NextPageURL is invoked when a paginated collection of networks has reached
83// the end of a page and the pager seeks to traverse over a new one. In order
84// to do this, it needs to construct the next page's URL.
85func (p NetworkPage) NextPageURL() (string, error) {
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020086 type link struct {
87 Href string `mapstructure:"href"`
88 Rel string `mapstructure:"rel"`
89 }
90 type resp struct {
91 Links []link `mapstructure:"networks_links"`
92 }
93
94 var r resp
Jamie Hannaford686c4962014-09-23 10:46:20 +020095 err := mapstructure.Decode(p.Body, &r)
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020096 if err != nil {
97 return "", err
98 }
99
100 var url string
101 for _, l := range r.Links {
102 if l.Rel == "next" {
103 url = l.Href
104 }
105 }
106 if url == "" {
107 return "", nil
108 }
109
110 return url, nil
111}
112
Jamie Hannaford686c4962014-09-23 10:46:20 +0200113// IsEmpty checks whether a NetworkPage struct is empty.
114func (p NetworkPage) IsEmpty() (bool, error) {
115 is, err := ExtractNetworks(p)
Jamie Hannafordf0c615b2014-09-17 10:56:52 +0200116 if err != nil {
117 return true, nil
118 }
119 return len(is) == 0, nil
120}
121
Jamie Hannaford686c4962014-09-23 10:46:20 +0200122// ExtractNetworks accepts a Page struct, specifically a NetworkPage struct,
123// and extracts the elements into a slice of Network structs. In other words,
124// a generic collection is mapped into a relevant slice.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +0200125func ExtractNetworks(page pagination.Page) ([]Network, error) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200126 var resp struct {
127 Networks []Network `mapstructure:"networks" json:"networks"`
128 }
129
Jamie Hannafordf0c615b2014-09-17 10:56:52 +0200130 err := mapstructure.Decode(page.(NetworkPage).Body, &resp)
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200131 if err != nil {
132 return nil, err
133 }
134
135 return resp.Networks, nil
136}