blob: e605fcf79e27877ca5f2e79d0df27b857f73b62b [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 {
10 gophercloud.CommonResult
11}
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
23 err := mapstructure.Decode(r.Resp, &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 Hannafordd9036422014-09-23 17:50:24 +020044type DeleteResult commonResult
45
Jamie Hannaford686c4962014-09-23 10:46:20 +020046// Network represents, well, a network.
Jamie Hannaford79475052014-09-15 17:08:06 +020047type Network struct {
Jamie Hannaford965ae702014-09-22 14:58:19 +020048 // UUID for the network
49 ID string `mapstructure:"id" json:"id"`
Jamie Hannaford9823bb62014-09-26 17:06:36 +020050
Jamie Hannaford965ae702014-09-22 14:58:19 +020051 // Human-readable name for the network. Might not be unique.
52 Name string `mapstructure:"name" json:"name"`
Jamie Hannaford9823bb62014-09-26 17:06:36 +020053
Jamie Hannaford965ae702014-09-22 14:58:19 +020054 // The administrative state of network. If false (down), the network does not forward packets.
55 AdminStateUp bool `mapstructure:"admin_state_up" json:"admin_state_up"`
Jamie Hannaford9823bb62014-09-26 17:06:36 +020056
Jamie Hannaford965ae702014-09-22 14:58:19 +020057 // Indicates whether network is currently operational. Possible values include
58 // `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional values.
59 Status string `mapstructure:"status" json:"status"`
Jamie Hannaford9823bb62014-09-26 17:06:36 +020060
Jamie Hannaford965ae702014-09-22 14:58:19 +020061 // Subnets associated with this network.
62 Subnets []string `mapstructure:"subnets" json:"subnets"`
Jamie Hannaford9823bb62014-09-26 17:06:36 +020063
Jamie Hannaford965ae702014-09-22 14:58:19 +020064 // Owner of network. Only admin users can specify a tenant_id other than its own.
65 TenantID string `mapstructure:"tenant_id" json:"tenant_id"`
Jamie Hannaford9823bb62014-09-26 17:06:36 +020066
Jamie Hannaford965ae702014-09-22 14:58:19 +020067 // Specifies whether the network resource can be accessed by any tenant or not.
68 Shared bool `mapstructure:"shared" json:"shared"`
Jamie Hannaforda7f671a2014-09-11 10:25:08 +020069}
Jamie Hannaford4721abc2014-09-16 16:29:04 +020070
Jamie Hannaford686c4962014-09-23 10:46:20 +020071// NetworkPage is the page returned by a pager when traversing over a
72// collection of networks.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020073type NetworkPage struct {
74 pagination.LinkedPageBase
75}
76
Jamie Hannaford686c4962014-09-23 10:46:20 +020077// NextPageURL is invoked when a paginated collection of networks has reached
78// the end of a page and the pager seeks to traverse over a new one. In order
79// to do this, it needs to construct the next page's URL.
80func (p NetworkPage) NextPageURL() (string, error) {
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020081 type resp struct {
Jamie Hannafordc1f36492014-10-08 15:48:48 +020082 Links []gophercloud.Link `mapstructure:"networks_links"`
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020083 }
84
85 var r resp
Jamie Hannaford686c4962014-09-23 10:46:20 +020086 err := mapstructure.Decode(p.Body, &r)
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020087 if err != nil {
88 return "", err
89 }
90
Jamie Hannafordc1f36492014-10-08 15:48:48 +020091 return gophercloud.ExtractNextURL(r.Links)
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020092}
93
Jamie Hannaford686c4962014-09-23 10:46:20 +020094// IsEmpty checks whether a NetworkPage struct is empty.
95func (p NetworkPage) IsEmpty() (bool, error) {
96 is, err := ExtractNetworks(p)
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020097 if err != nil {
98 return true, nil
99 }
100 return len(is) == 0, nil
101}
102
Jamie Hannaford686c4962014-09-23 10:46:20 +0200103// ExtractNetworks accepts a Page struct, specifically a NetworkPage struct,
104// and extracts the elements into a slice of Network structs. In other words,
105// a generic collection is mapped into a relevant slice.
Jamie Hannafordf0c615b2014-09-17 10:56:52 +0200106func ExtractNetworks(page pagination.Page) ([]Network, error) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200107 var resp struct {
108 Networks []Network `mapstructure:"networks" json:"networks"`
109 }
110
Jamie Hannafordf0c615b2014-09-17 10:56:52 +0200111 err := mapstructure.Decode(page.(NetworkPage).Body, &resp)
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200112
Jamie Hannafordc1f36492014-10-08 15:48:48 +0200113 return resp.Networks, err
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200114}