Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 1 | package ports |
| 2 | |
| 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 5 | "github.com/gophercloud/gophercloud" |
| 6 | "github.com/gophercloud/gophercloud/pagination" |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 7 | ) |
| 8 | |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 9 | type commonResult struct { |
Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 10 | gophercloud.Result |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 11 | } |
| 12 | |
Jamie Hannaford | f311483 | 2014-09-24 11:00:43 +0200 | [diff] [blame] | 13 | // Extract is a function that accepts a result and extracts a port resource. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 14 | func (r commonResult) Extract() (*Port, error) { |
| 15 | if r.Err != nil { |
| 16 | return nil, r.Err |
| 17 | } |
| 18 | |
| 19 | var res struct { |
| 20 | Port *Port `json:"port"` |
| 21 | } |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 22 | err := mapstructure.Decode(r.Body, &res) |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 23 | |
Jamie Hannaford | c1f3649 | 2014-10-08 15:48:48 +0200 | [diff] [blame] | 24 | return res.Port, err |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 25 | } |
| 26 | |
Jamie Hannaford | f311483 | 2014-09-24 11:00:43 +0200 | [diff] [blame] | 27 | // CreateResult represents the result of a create operation. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 28 | type CreateResult struct { |
| 29 | commonResult |
| 30 | } |
| 31 | |
Jamie Hannaford | f311483 | 2014-09-24 11:00:43 +0200 | [diff] [blame] | 32 | // GetResult represents the result of a get operation. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 33 | type GetResult struct { |
| 34 | commonResult |
| 35 | } |
| 36 | |
Jamie Hannaford | f311483 | 2014-09-24 11:00:43 +0200 | [diff] [blame] | 37 | // UpdateResult represents the result of an update operation. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 38 | type UpdateResult struct { |
| 39 | commonResult |
| 40 | } |
| 41 | |
Jamie Hannaford | f311483 | 2014-09-24 11:00:43 +0200 | [diff] [blame] | 42 | // DeleteResult represents the result of a delete operation. |
Jamie Hannaford | d6c81b2 | 2014-10-27 14:02:53 +0100 | [diff] [blame] | 43 | type DeleteResult struct { |
Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 44 | gophercloud.ErrResult |
Jamie Hannaford | d6c81b2 | 2014-10-27 14:02:53 +0100 | [diff] [blame] | 45 | } |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 46 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 47 | // IP is a sub-struct that represents an individual IP. |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 48 | type IP struct { |
| 49 | SubnetID string `mapstructure:"subnet_id" json:"subnet_id"` |
Jamie Hannaford | 2a0492a | 2014-09-22 12:02:11 +0200 | [diff] [blame] | 50 | IPAddress string `mapstructure:"ip_address" json:"ip_address,omitempty"` |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 51 | } |
| 52 | |
Travis Truman | 0447aca | 2015-09-15 16:09:24 -0400 | [diff] [blame] | 53 | type AddressPair struct { |
| 54 | IPAddress string `mapstructure:"ip_address" json:"ip_address,omitempty"` |
Travis Truman | b02c75b | 2015-09-16 11:52:05 -0400 | [diff] [blame] | 55 | MACAddress string `mapstructure:"mac_address" json:"mac_address,omitempty"` |
Travis Truman | 0447aca | 2015-09-15 16:09:24 -0400 | [diff] [blame] | 56 | } |
| 57 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 58 | // Port represents a Neutron port. See package documentation for a top-level |
| 59 | // description of what this is. |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 60 | type Port struct { |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 61 | // UUID for the port. |
| 62 | ID string `mapstructure:"id" json:"id"` |
| 63 | // Network that this port is associated with. |
| 64 | NetworkID string `mapstructure:"network_id" json:"network_id"` |
| 65 | // Human-readable name for the port. Might not be unique. |
| 66 | Name string `mapstructure:"name" json:"name"` |
| 67 | // Administrative state of port. If false (down), port does not forward packets. |
| 68 | AdminStateUp bool `mapstructure:"admin_state_up" json:"admin_state_up"` |
| 69 | // Indicates whether network is currently operational. Possible values include |
| 70 | // `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional values. |
| 71 | Status string `mapstructure:"status" json:"status"` |
| 72 | // Mac address to use on this port. |
| 73 | MACAddress string `mapstructure:"mac_address" json:"mac_address"` |
| 74 | // Specifies IP addresses for the port thus associating the port itself with |
| 75 | // the subnets where the IP addresses are picked from |
| 76 | FixedIPs []IP `mapstructure:"fixed_ips" json:"fixed_ips"` |
| 77 | // Owner of network. Only admin users can specify a tenant_id other than its own. |
| 78 | TenantID string `mapstructure:"tenant_id" json:"tenant_id"` |
| 79 | // Identifies the entity (e.g.: dhcp agent) using this port. |
| 80 | DeviceOwner string `mapstructure:"device_owner" json:"device_owner"` |
| 81 | // Specifies the IDs of any security groups associated with a port. |
| 82 | SecurityGroups []string `mapstructure:"security_groups" json:"security_groups"` |
| 83 | // Identifies the device (e.g., virtual server) using this port. |
| 84 | DeviceID string `mapstructure:"device_id" json:"device_id"` |
Travis Truman | d3e3a10 | 2015-09-16 14:21:56 -0400 | [diff] [blame] | 85 | // Identifies the list of IP addresses the port will recognize/accept |
Travis Truman | 69e88e9 | 2015-09-16 13:55:32 -0400 | [diff] [blame] | 86 | AllowedAddressPairs []AddressPair `mapstructure:"allowed_address_pairs" json:"allowed_address_pairs"` |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 89 | // PortPage is the page returned by a pager when traversing over a collection |
| 90 | // of network ports. |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 91 | type PortPage struct { |
| 92 | pagination.LinkedPageBase |
| 93 | } |
| 94 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 95 | // NextPageURL is invoked when a paginated collection of ports has reached |
| 96 | // the end of a page and the pager seeks to traverse over a new one. In order |
| 97 | // to do this, it needs to construct the next page's URL. |
| 98 | func (p PortPage) NextPageURL() (string, error) { |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 99 | type resp struct { |
Jamie Hannaford | c1f3649 | 2014-10-08 15:48:48 +0200 | [diff] [blame] | 100 | Links []gophercloud.Link `mapstructure:"ports_links"` |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | var r resp |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 104 | err := mapstructure.Decode(p.Body, &r) |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 105 | if err != nil { |
| 106 | return "", err |
| 107 | } |
| 108 | |
Jamie Hannaford | c1f3649 | 2014-10-08 15:48:48 +0200 | [diff] [blame] | 109 | return gophercloud.ExtractNextURL(r.Links) |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 110 | } |
| 111 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 112 | // IsEmpty checks whether a PortPage struct is empty. |
| 113 | func (p PortPage) IsEmpty() (bool, error) { |
| 114 | is, err := ExtractPorts(p) |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 115 | if err != nil { |
| 116 | return true, nil |
| 117 | } |
| 118 | return len(is) == 0, nil |
| 119 | } |
| 120 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 121 | // ExtractPorts accepts a Page struct, specifically a PortPage struct, |
| 122 | // and extracts the elements into a slice of Port structs. In other words, |
| 123 | // a generic collection is mapped into a relevant slice. |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 124 | func ExtractPorts(page pagination.Page) ([]Port, error) { |
| 125 | var resp struct { |
| 126 | Ports []Port `mapstructure:"ports" json:"ports"` |
| 127 | } |
| 128 | |
| 129 | err := mapstructure.Decode(page.(PortPage).Body, &resp) |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 130 | |
Jamie Hannaford | c1f3649 | 2014-10-08 15:48:48 +0200 | [diff] [blame] | 131 | return resp.Ports, err |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 132 | } |