blob: 8229f166239d19e0281b1ae0a39c32c1450dec96 [file] [log] [blame]
Jamie Hannaford548d3402014-09-18 15:50:08 +02001package ports
2
3import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
Krzysztof Szukiełojć24a29ce2017-05-07 14:24:02 +02005 "gerrit.mcp.mirantis.net/debian/gophercloud.git/pagination"
Jamie Hannaford548d3402014-09-18 15:50:08 +02006)
7
Jamie Hannafordd9036422014-09-23 17:50:24 +02008type commonResult struct {
Ash Wilsonf548aad2014-10-20 08:35:34 -04009 gophercloud.Result
Jamie Hannafordd9036422014-09-23 17:50:24 +020010}
11
Jamie Hannafordf3114832014-09-24 11:00:43 +020012// Extract is a function that accepts a result and extracts a port resource.
Jamie Hannafordd9036422014-09-23 17:50:24 +020013func (r commonResult) Extract() (*Port, error) {
Jon Perritt3c166472016-02-25 03:07:41 -060014 var s struct {
Jamie Hannafordd9036422014-09-23 17:50:24 +020015 Port *Port `json:"port"`
16 }
Jon Perritt3c166472016-02-25 03:07:41 -060017 err := r.ExtractInto(&s)
18 return s.Port, err
Jamie Hannafordd9036422014-09-23 17:50:24 +020019}
20
Jamie Hannafordf3114832014-09-24 11:00:43 +020021// CreateResult represents the result of a create operation.
Jamie Hannafordd9036422014-09-23 17:50:24 +020022type CreateResult struct {
23 commonResult
24}
25
Jamie Hannafordf3114832014-09-24 11:00:43 +020026// GetResult represents the result of a get operation.
Jamie Hannafordd9036422014-09-23 17:50:24 +020027type GetResult struct {
28 commonResult
29}
30
Jamie Hannafordf3114832014-09-24 11:00:43 +020031// UpdateResult represents the result of an update operation.
Jamie Hannafordd9036422014-09-23 17:50:24 +020032type UpdateResult struct {
33 commonResult
34}
35
Jamie Hannafordf3114832014-09-24 11:00:43 +020036// DeleteResult represents the result of a delete operation.
Jamie Hannafordd6c81b22014-10-27 14:02:53 +010037type DeleteResult struct {
Jon Perrittba2395e2014-10-27 15:23:21 -050038 gophercloud.ErrResult
Jamie Hannafordd6c81b22014-10-27 14:02:53 +010039}
Jamie Hannafordd9036422014-09-23 17:50:24 +020040
Jamie Hannaford686c4962014-09-23 10:46:20 +020041// IP is a sub-struct that represents an individual IP.
Jamie Hannaford548d3402014-09-18 15:50:08 +020042type IP struct {
Jon Perritt3c166472016-02-25 03:07:41 -060043 SubnetID string `json:"subnet_id"`
44 IPAddress string `json:"ip_address,omitempty"`
Jamie Hannaford548d3402014-09-18 15:50:08 +020045}
46
Jon Perritt3c166472016-02-25 03:07:41 -060047// AddressPair contains the IP Address and the MAC address.
Travis Truman0447aca2015-09-15 16:09:24 -040048type AddressPair struct {
Jon Perritt3c166472016-02-25 03:07:41 -060049 IPAddress string `json:"ip_address,omitempty"`
50 MACAddress string `json:"mac_address,omitempty"`
Travis Truman0447aca2015-09-15 16:09:24 -040051}
52
Jamie Hannaford686c4962014-09-23 10:46:20 +020053// Port represents a Neutron port. See package documentation for a top-level
54// description of what this is.
Jamie Hannaford548d3402014-09-18 15:50:08 +020055type Port struct {
Jamie Hannaford965ae702014-09-22 14:58:19 +020056 // UUID for the port.
Jon Perritt3c166472016-02-25 03:07:41 -060057 ID string `json:"id"`
Jamie Hannaford965ae702014-09-22 14:58:19 +020058 // Network that this port is associated with.
Jon Perritt3c166472016-02-25 03:07:41 -060059 NetworkID string `json:"network_id"`
Jamie Hannaford965ae702014-09-22 14:58:19 +020060 // Human-readable name for the port. Might not be unique.
Jon Perritt3c166472016-02-25 03:07:41 -060061 Name string `json:"name"`
Jamie Hannaford965ae702014-09-22 14:58:19 +020062 // Administrative state of port. If false (down), port does not forward packets.
Jon Perritt3c166472016-02-25 03:07:41 -060063 AdminStateUp bool `json:"admin_state_up"`
Jamie Hannaford965ae702014-09-22 14:58:19 +020064 // Indicates whether network is currently operational. Possible values include
65 // `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional values.
Jon Perritt3c166472016-02-25 03:07:41 -060066 Status string `json:"status"`
Jamie Hannaford965ae702014-09-22 14:58:19 +020067 // Mac address to use on this port.
Jon Perritt3c166472016-02-25 03:07:41 -060068 MACAddress string `json:"mac_address"`
Jamie Hannaford965ae702014-09-22 14:58:19 +020069 // Specifies IP addresses for the port thus associating the port itself with
70 // the subnets where the IP addresses are picked from
Jon Perritt3c166472016-02-25 03:07:41 -060071 FixedIPs []IP `json:"fixed_ips"`
Jamie Hannaford965ae702014-09-22 14:58:19 +020072 // Owner of network. Only admin users can specify a tenant_id other than its own.
Jon Perritt3c166472016-02-25 03:07:41 -060073 TenantID string `json:"tenant_id"`
Jamie Hannaford965ae702014-09-22 14:58:19 +020074 // Identifies the entity (e.g.: dhcp agent) using this port.
Jon Perritt3c166472016-02-25 03:07:41 -060075 DeviceOwner string `json:"device_owner"`
Jamie Hannaford965ae702014-09-22 14:58:19 +020076 // Specifies the IDs of any security groups associated with a port.
Jon Perritt3c166472016-02-25 03:07:41 -060077 SecurityGroups []string `json:"security_groups"`
Jamie Hannaford965ae702014-09-22 14:58:19 +020078 // Identifies the device (e.g., virtual server) using this port.
Jon Perritt3c166472016-02-25 03:07:41 -060079 DeviceID string `json:"device_id"`
Travis Trumand3e3a102015-09-16 14:21:56 -040080 // Identifies the list of IP addresses the port will recognize/accept
Jon Perritt3c166472016-02-25 03:07:41 -060081 AllowedAddressPairs []AddressPair `json:"allowed_address_pairs"`
Jamie Hannaford548d3402014-09-18 15:50:08 +020082}
83
Jamie Hannaford686c4962014-09-23 10:46:20 +020084// PortPage is the page returned by a pager when traversing over a collection
85// of network ports.
Jamie Hannaford548d3402014-09-18 15:50:08 +020086type PortPage struct {
87 pagination.LinkedPageBase
88}
89
Jamie Hannaford686c4962014-09-23 10:46:20 +020090// 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 Perritt31b66462016-02-25 22:25:30 -060093func (r PortPage) NextPageURL() (string, error) {
Jon Perritt3c166472016-02-25 03:07:41 -060094 var s struct {
95 Links []gophercloud.Link `json:"ports_links"`
Jamie Hannaford548d3402014-09-18 15:50:08 +020096 }
Jon Perritt31b66462016-02-25 22:25:30 -060097 err := r.ExtractInto(&s)
Jamie Hannaford548d3402014-09-18 15:50:08 +020098 if err != nil {
99 return "", err
100 }
Jon Perritt3c166472016-02-25 03:07:41 -0600101 return gophercloud.ExtractNextURL(s.Links)
Jamie Hannaford548d3402014-09-18 15:50:08 +0200102}
103
Jamie Hannaford686c4962014-09-23 10:46:20 +0200104// IsEmpty checks whether a PortPage struct is empty.
Jon Perritt31b66462016-02-25 22:25:30 -0600105func (r PortPage) IsEmpty() (bool, error) {
106 is, err := ExtractPorts(r)
Jon Perritt3c166472016-02-25 03:07:41 -0600107 return len(is) == 0, err
Jamie Hannaford548d3402014-09-18 15:50:08 +0200108}
109
Jamie Hannaford686c4962014-09-23 10:46:20 +0200110// 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 Perritt31b66462016-02-25 22:25:30 -0600113func ExtractPorts(r pagination.Page) ([]Port, error) {
Jon Perritt3c166472016-02-25 03:07:41 -0600114 var s struct {
115 Ports []Port `json:"ports"`
Jamie Hannaford548d3402014-09-18 15:50:08 +0200116 }
Jon Perritt31b66462016-02-25 22:25:30 -0600117 err := (r.(PortPage)).ExtractInto(&s)
Jon Perritt3c166472016-02-25 03:07:41 -0600118 return s.Ports, err
Jamie Hannaford548d3402014-09-18 15:50:08 +0200119}