blob: 3ecedde9ac07389f145d9491c18150197d112d1d [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 Hannafordd9036422014-09-23 17:50:24 +02005 "github.com/rackspace/gophercloud"
Jamie Hannafordf0c615b2014-09-17 10:56:52 +02006 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford4721abc2014-09-16 16:29:04 +02007)
8
Jamie Hannafordd9036422014-09-23 17:50:24 +02009type commonResult struct {
Ash Wilsonf548aad2014-10-20 08:35:34 -040010 gophercloud.Result
Jamie Hannafordd9036422014-09-23 17:50:24 +020011}
12
Jamie Hannafordf3114832014-09-24 11:00:43 +020013// Extract is a function that accepts a result and extracts a network resource.
Jamie Hannafordd9036422014-09-23 17:50:24 +020014func (r commonResult) Extract() (*Network, error) {
15 if r.Err != nil {
16 return nil, r.Err
17 }
18
19 var res struct {
20 Network *Network `json:"network"`
21 }
22
Ash Wilsond3dc2542014-10-20 10:10:48 -040023 err := mapstructure.Decode(r.Body, &res)
Jamie Hannafordd9036422014-09-23 17:50:24 +020024
Jamie Hannafordc1f36492014-10-08 15:48:48 +020025 return res.Network, err
Jamie Hannafordd9036422014-09-23 17:50:24 +020026}
27
Jamie Hannafordf3114832014-09-24 11:00:43 +020028// CreateResult represents the result of a create operation.
Jamie Hannafordd9036422014-09-23 17:50:24 +020029type CreateResult struct {
30 commonResult
31}
32
Jamie Hannafordf3114832014-09-24 11:00:43 +020033// GetResult represents the result of a get operation.
Jamie Hannafordd9036422014-09-23 17:50:24 +020034type GetResult struct {
35 commonResult
36}
37
Jamie Hannafordf3114832014-09-24 11:00:43 +020038// UpdateResult represents the result of an update operation.
Jamie Hannafordd9036422014-09-23 17:50:24 +020039type UpdateResult struct {
40 commonResult
41}
42
Jamie Hannafordf3114832014-09-24 11:00:43 +020043// DeleteResult represents the result of a delete operation.
Jamie Hannafordd6c81b22014-10-27 14:02:53 +010044type DeleteResult struct {
Jon Perrittba2395e2014-10-27 15:23:21 -050045 gophercloud.ErrResult
Jamie Hannafordd6c81b22014-10-27 14:02:53 +010046}
Jamie Hannafordd9036422014-09-23 17:50:24 +020047
Jamie Hannaford686c4962014-09-23 10:46:20 +020048// Network represents, well, a network.
Jamie Hannaford79475052014-09-15 17:08:06 +020049type Network struct {
Jamie Hannaford965ae702014-09-22 14:58:19 +020050 // UUID for the network
51 ID string `mapstructure:"id" json:"id"`
Jamie Hannaford9823bb62014-09-26 17:06:36 +020052
Jamie Hannaford965ae702014-09-22 14:58:19 +020053 // Human-readable name for the network. Might not be unique.
54 Name string `mapstructure:"name" json:"name"`
Jamie Hannaford9823bb62014-09-26 17:06:36 +020055
Jamie Hannaford965ae702014-09-22 14:58:19 +020056 // The administrative state of network. If false (down), the network does not forward packets.
57 AdminStateUp bool `mapstructure:"admin_state_up" json:"admin_state_up"`
Jamie Hannaford9823bb62014-09-26 17:06:36 +020058
Jamie Hannaford965ae702014-09-22 14:58:19 +020059 // 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"`
Jamie Hannaford9823bb62014-09-26 17:06:36 +020062
Jamie Hannaford965ae702014-09-22 14:58:19 +020063 // Subnets associated with this network.
64 Subnets []string `mapstructure:"subnets" json:"subnets"`
Jamie Hannaford9823bb62014-09-26 17:06:36 +020065
Jamie Hannaford965ae702014-09-22 14:58:19 +020066 // Owner of network. Only admin users can specify a tenant_id other than its own.
67 TenantID string `mapstructure:"tenant_id" json:"tenant_id"`
Jamie Hannaford9823bb62014-09-26 17:06:36 +020068
Jamie Hannaford965ae702014-09-22 14:58:19 +020069 // Specifies whether the network resource can be accessed by any tenant or not.
70 Shared bool `mapstructure:"shared" json:"shared"`
Jamie Hannaforda7f671a2014-09-11 10:25:08 +020071}
Jamie Hannaford4721abc2014-09-16 16:29:04 +020072
Jamie Hannaford686c4962014-09-23 10:46:20 +020073// NetworkPage is the page returned by a pager when traversing over a
74// collection of networks.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020075type NetworkPage struct {
76 pagination.LinkedPageBase
77}
78
Jamie Hannaford686c4962014-09-23 10:46:20 +020079// NextPageURL is invoked when a paginated collection of networks has reached
80// the end of a page and the pager seeks to traverse over a new one. In order
81// to do this, it needs to construct the next page's URL.
82func (p NetworkPage) NextPageURL() (string, error) {
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020083 type resp struct {
Jamie Hannafordc1f36492014-10-08 15:48:48 +020084 Links []gophercloud.Link `mapstructure:"networks_links"`
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020085 }
86
87 var r resp
Jamie Hannaford686c4962014-09-23 10:46:20 +020088 err := mapstructure.Decode(p.Body, &r)
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020089 if err != nil {
90 return "", err
91 }
92
Jamie Hannafordc1f36492014-10-08 15:48:48 +020093 return gophercloud.ExtractNextURL(r.Links)
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020094}
95
Jamie Hannaford686c4962014-09-23 10:46:20 +020096// IsEmpty checks whether a NetworkPage struct is empty.
97func (p NetworkPage) IsEmpty() (bool, error) {
98 is, err := ExtractNetworks(p)
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020099 if err != nil {
100 return true, nil
101 }
102 return len(is) == 0, nil
103}
104
Jamie Hannaford686c4962014-09-23 10:46:20 +0200105// ExtractNetworks accepts a Page struct, specifically a NetworkPage struct,
106// and extracts the elements into a slice of Network structs. In other words,
107// a generic collection is mapped into a relevant slice.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +0200108func ExtractNetworks(page pagination.Page) ([]Network, error) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200109 var resp struct {
110 Networks []Network `mapstructure:"networks" json:"networks"`
111 }
112
Jamie Hannafordf0c615b2014-09-17 10:56:52 +0200113 err := mapstructure.Decode(page.(NetworkPage).Body, &resp)
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200114
Jamie Hannafordc1f36492014-10-08 15:48:48 +0200115 return resp.Networks, err
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200116}