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

     1  package portforwarding
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/pagination"
     6  )
     7  
     8  type ListOptsBuilder interface {
     9  	ToPortForwardingListQuery() (string, error)
    10  }
    11  
    12  // ListOpts allows the filtering and sorting of paginated collections through
    13  // the API. Filtering is achieved by passing in struct field values that map to
    14  // the port forwarding attributes you want to see returned. SortKey allows you to
    15  // sort by a particular network attribute. SortDir sets the direction, and is
    16  // either `asc' or `desc'. Marker and Limit are used for pagination.
    17  type ListOpts struct {
    18  	ID                string `q:"id"`
    19  	Description       string `q:"description"`
    20  	InternalPortID    string `q:"internal_port_id"`
    21  	ExternalPort      string `q:"external_port"`
    22  	InternalIPAddress string `q:"internal_ip_address"`
    23  	Protocol          string `q:"protocol"`
    24  	InternalPort      string `q:"internal_port"`
    25  	SortKey           string `q:"sort_key"`
    26  	SortDir           string `q:"sort_dir"`
    27  	Fields            string `q:"fields"`
    28  	Limit             int    `q:"limit"`
    29  	Marker            string `q:"marker"`
    30  }
    31  
    32  // ToPortForwardingListQuery formats a ListOpts into a query string.
    33  func (opts ListOpts) ToPortForwardingListQuery() (string, error) {
    34  	q, err := gophercloud.BuildQueryString(opts)
    35  	return q.String(), err
    36  }
    37  
    38  // List returns a Pager which allows you to iterate over a collection of
    39  // Port Forwarding resources. It accepts a ListOpts struct, which allows you to
    40  // filter and sort the returned collection for greater efficiency.
    41  func List(c *gophercloud.ServiceClient, opts ListOptsBuilder, id string) pagination.Pager {
    42  	url := portForwardingUrl(c, id)
    43  	if opts != nil {
    44  		query, err := opts.ToPortForwardingListQuery()
    45  		if err != nil {
    46  			return pagination.Pager{Err: err}
    47  		}
    48  		url += query
    49  	}
    50  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    51  		return PortForwardingPage{pagination.LinkedPageBase{PageResult: r}}
    52  	})
    53  }
    54  
    55  // Get retrieves a particular port forwarding resource based on its unique ID.
    56  func Get(c *gophercloud.ServiceClient, floatingIpId string, pfId string) (r GetResult) {
    57  	resp, err := c.Get(singlePortForwardingUrl(c, floatingIpId, pfId), &r.Body, nil)
    58  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    59  	return
    60  }
    61  
    62  // CreateOpts contains all the values needed to create a new port forwarding
    63  // resource. All attributes are required.
    64  type CreateOpts struct {
    65  	Description       string `json:"description,omitempty"`
    66  	InternalPortID    string `json:"internal_port_id"`
    67  	InternalIPAddress string `json:"internal_ip_address"`
    68  	InternalPort      int    `json:"internal_port"`
    69  	ExternalPort      int    `json:"external_port"`
    70  	Protocol          string `json:"protocol"`
    71  }
    72  
    73  // CreateOptsBuilder allows extensions to add additional parameters to the
    74  // Create request.
    75  type CreateOptsBuilder interface {
    76  	ToPortForwardingCreateMap() (map[string]interface{}, error)
    77  }
    78  
    79  // ToPortForwardingCreateMap allows CreateOpts to satisfy the CreateOptsBuilder
    80  // interface
    81  func (opts CreateOpts) ToPortForwardingCreateMap() (map[string]interface{}, error) {
    82  	return gophercloud.BuildRequestBody(opts, "port_forwarding")
    83  }
    84  
    85  // Create accepts a CreateOpts struct and uses the values provided to create a
    86  // new port forwarding for an existing floating IP.
    87  func Create(c *gophercloud.ServiceClient, floatingIpId string, opts CreateOptsBuilder) (r CreateResult) {
    88  	b, err := opts.ToPortForwardingCreateMap()
    89  	if err != nil {
    90  		r.Err = err
    91  		return
    92  	}
    93  	resp, err := c.Post(portForwardingUrl(c, floatingIpId), b, &r.Body, nil)
    94  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    95  	return
    96  }
    97  
    98  // UpdateOpts contains the values used when updating a port forwarding resource.
    99  type UpdateOpts struct {
   100  	Description       *string `json:"description,omitempty"`
   101  	InternalPortID    string  `json:"internal_port_id,omitempty"`
   102  	InternalIPAddress string  `json:"internal_ip_address,omitempty"`
   103  	InternalPort      int     `json:"internal_port,omitempty"`
   104  	ExternalPort      int     `json:"external_port,omitempty"`
   105  	Protocol          string  `json:"protocol,omitempty"`
   106  }
   107  
   108  // ToPortForwardingUpdateMap allows UpdateOpts to satisfy the UpdateOptsBuilder
   109  // interface
   110  func (opts UpdateOpts) ToPortForwardingUpdateMap() (map[string]interface{}, error) {
   111  	return gophercloud.BuildRequestBody(opts, "port_forwarding")
   112  }
   113  
   114  // UpdateOptsBuilder allows extensions to add additional parameters to the
   115  // Update request.
   116  type UpdateOptsBuilder interface {
   117  	ToPortForwardingUpdateMap() (map[string]interface{}, error)
   118  }
   119  
   120  // Update allows port forwarding resources to be updated.
   121  func Update(c *gophercloud.ServiceClient, fipID string, pfID string, opts UpdateOptsBuilder) (r UpdateResult) {
   122  	b, err := opts.ToPortForwardingUpdateMap()
   123  	if err != nil {
   124  		r.Err = err
   125  		return
   126  	}
   127  	resp, err := c.Put(singlePortForwardingUrl(c, fipID, pfID), b, &r.Body, &gophercloud.RequestOpts{
   128  		OkCodes: []int{200},
   129  	})
   130  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   131  	return
   132  }
   133  
   134  // Delete will permanently delete a particular port forwarding for a given floating ID.
   135  func Delete(c *gophercloud.ServiceClient, floatingIpId string, pfId string) (r DeleteResult) {
   136  	resp, err := c.Delete(singlePortForwardingUrl(c, floatingIpId, pfId), nil)
   137  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   138  	return
   139  }