github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/extensions/layer3/portforwarding/results.go (about)

     1  package portforwarding
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/pagination"
     6  )
     7  
     8  type PortForwarding struct {
     9  	// The ID of the floating IP port forwarding
    10  	ID string `json:"id"`
    11  
    12  	// The ID of the Neutron port associated to the floating IP port forwarding.
    13  	InternalPortID string `json:"internal_port_id"`
    14  
    15  	// The TCP/UDP/other protocol port number of the port forwarding’s floating IP address.
    16  	ExternalPort int `json:"external_port"`
    17  
    18  	// The IP protocol used in the floating IP port forwarding.
    19  	Protocol string `json:"protocol"`
    20  
    21  	// The TCP/UDP/other protocol port number of the Neutron port fixed
    22  	// IP address associated to the floating ip port forwarding.
    23  	InternalPort int `json:"internal_port"`
    24  
    25  	// The fixed IPv4 address of the Neutron port associated
    26  	// to the floating IP port forwarding.
    27  	InternalIPAddress string `json:"internal_ip_address"`
    28  }
    29  
    30  type commonResult struct {
    31  	gophercloud.Result
    32  }
    33  
    34  // CreateResult represents the result of a create operation. Call its Extract
    35  // method to interpret it as a PortForwarding.
    36  type CreateResult struct {
    37  	commonResult
    38  }
    39  
    40  // GetResult represents the result of a get operation. Call its Extract
    41  // method to interpret it as a PortForwarding.
    42  type GetResult struct {
    43  	commonResult
    44  }
    45  
    46  // UpdateResult represents the result of an update operation. Call its Extract
    47  // method to interpret it as a PortForwarding.
    48  type UpdateResult struct {
    49  	commonResult
    50  }
    51  
    52  // DeleteResult represents the result of a delete operation. Call its
    53  // ExtractErr method to determine if the request succeeded or failed.
    54  type DeleteResult struct {
    55  	gophercloud.ErrResult
    56  }
    57  
    58  // Extract will extract a Port Forwarding resource from a result.
    59  func (r commonResult) Extract() (*PortForwarding, error) {
    60  	var s PortForwarding
    61  	err := r.ExtractInto(&s)
    62  	return &s, err
    63  }
    64  
    65  func (r commonResult) ExtractInto(v interface{}) error {
    66  	return r.Result.ExtractIntoStructPtr(v, "port_forwarding")
    67  }
    68  
    69  // PortForwardingPage is the page returned by a pager when traversing over a
    70  // collection of port forwardings.
    71  type PortForwardingPage struct {
    72  	pagination.LinkedPageBase
    73  }
    74  
    75  // NextPageURL is invoked when a paginated collection of port forwardings has
    76  // reached the end of a page and the pager seeks to traverse over a new one.
    77  // In order to do this, it needs to construct the next page's URL.
    78  func (r PortForwardingPage) NextPageURL() (string, error) {
    79  	var s struct {
    80  		Links []gophercloud.Link `json:"port_forwarding_links"`
    81  	}
    82  	err := r.ExtractInto(&s)
    83  	if err != nil {
    84  		return "", err
    85  	}
    86  	return gophercloud.ExtractNextURL(s.Links)
    87  }
    88  
    89  // IsEmpty checks whether a PortForwardingPage struct is empty.
    90  func (r PortForwardingPage) IsEmpty() (bool, error) {
    91  	if r.StatusCode == 204 {
    92  		return true, nil
    93  	}
    94  
    95  	is, err := ExtractPortForwardings(r)
    96  	return len(is) == 0, err
    97  }
    98  
    99  // ExtractPortForwardings accepts a Page struct, specifically a PortForwardingPage
   100  // struct, and extracts the elements into a slice of PortForwarding structs. In
   101  // other words, a generic collection is mapped into a relevant slice.
   102  func ExtractPortForwardings(r pagination.Page) ([]PortForwarding, error) {
   103  	var s struct {
   104  		PortForwardings []PortForwarding `json:"port_forwardings"`
   105  	}
   106  	err := (r.(PortForwardingPage)).ExtractInto(&s)
   107  	return s.PortForwardings, err
   108  }