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