github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/networking/v2/extensions/layer3/portforwarding/requests.go (about)

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