github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/layer3/floatingips/requests.go (about)

     1  package floatingips
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/pagination"
     6  )
     7  
     8  // ListOpts allows the filtering and sorting of paginated collections through
     9  // the API. Filtering is achieved by passing in struct field values that map to
    10  // the floating IP attributes you want to see returned. SortKey allows you to
    11  // sort by a particular network attribute. SortDir sets the direction, and is
    12  // either `asc' or `desc'. Marker and Limit are used for pagination.
    13  type ListOpts struct {
    14  	ID                string `q:"id"`
    15  	FloatingNetworkID string `q:"floating_network_id"`
    16  	PortID            string `q:"port_id"`
    17  	FixedIP           string `q:"fixed_ip_address"`
    18  	FloatingIP        string `q:"floating_ip_address"`
    19  	TenantID          string `q:"tenant_id"`
    20  	ProjectID         string `q:"project_id"`
    21  	Limit             int    `q:"limit"`
    22  	Marker            string `q:"marker"`
    23  	SortKey           string `q:"sort_key"`
    24  	SortDir           string `q:"sort_dir"`
    25  	RouterID          string `q:"router_id"`
    26  	Status            string `q:"status"`
    27  }
    28  
    29  // List returns a Pager which allows you to iterate over a collection of
    30  // floating IP resources. It accepts a ListOpts struct, which allows you to
    31  // filter and sort the returned collection for greater efficiency.
    32  func List(c *golangsdk.ServiceClient, opts ListOpts) pagination.Pager {
    33  	q, err := golangsdk.BuildQueryString(&opts)
    34  	if err != nil {
    35  		return pagination.Pager{Err: err}
    36  	}
    37  	u := rootURL(c) + q.String()
    38  	return pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page {
    39  		return FloatingIPPage{pagination.LinkedPageBase{PageResult: r}}
    40  	})
    41  }
    42  
    43  // CreateOptsBuilder allows extensions to add additional parameters to the
    44  // Create request.
    45  type CreateOptsBuilder interface {
    46  	ToFloatingIPCreateMap() (map[string]interface{}, error)
    47  }
    48  
    49  // CreateOpts contains all the values needed to create a new floating IP
    50  // resource. The only required fields are FloatingNetworkID and PortID which
    51  // refer to the external network and internal port respectively.
    52  type CreateOpts struct {
    53  	FloatingNetworkID string `json:"floating_network_id" required:"true"`
    54  	FloatingIP        string `json:"floating_ip_address,omitempty"`
    55  	PortID            string `json:"port_id,omitempty"`
    56  	FixedIP           string `json:"fixed_ip_address,omitempty"`
    57  	SubnetID          string `json:"subnet_id,omitempty"`
    58  	TenantID          string `json:"tenant_id,omitempty"`
    59  	ProjectID         string `json:"project_id,omitempty"`
    60  }
    61  
    62  // ToFloatingIPCreateMap allows CreateOpts to satisfy the CreateOptsBuilder
    63  // interface
    64  func (opts CreateOpts) ToFloatingIPCreateMap() (map[string]interface{}, error) {
    65  	return golangsdk.BuildRequestBody(opts, "floatingip")
    66  }
    67  
    68  // Create accepts a CreateOpts struct and uses the values provided to create a
    69  // new floating IP resource. You can create floating IPs on external networks
    70  // only. If you provide a FloatingNetworkID which refers to a network that is
    71  // not external (i.e. its `router:external' attribute is False), the operation
    72  // will fail and return a 400 error.
    73  //
    74  // If you do not specify a FloatingIP address value, the operation will
    75  // automatically allocate an available address for the new resource. If you do
    76  // choose to specify one, it must fall within the subnet range for the external
    77  // network - otherwise the operation returns a 400 error. If the FloatingIP
    78  // address is already in use, the operation returns a 409 error code.
    79  //
    80  // You can associate the new resource with an internal port by using the PortID
    81  // field. If you specify a PortID that is not valid, the operation will fail and
    82  // return 404 error code.
    83  //
    84  // You must also configure an IP address for the port associated with the PortID
    85  // you have provided - this is what the FixedIP refers to: an IP fixed to a
    86  // port. Because a port might be associated with multiple IP addresses, you can
    87  // use the FixedIP field to associate a particular IP address rather than have
    88  // the API assume for you. If you specify an IP address that is not valid, the
    89  // operation will fail and return a 400 error code. If the PortID and FixedIP
    90  // are already associated with another resource, the operation will fail and
    91  // returns a 409 error code.
    92  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    93  	b, err := opts.ToFloatingIPCreateMap()
    94  	if err != nil {
    95  		r.Err = err
    96  		return
    97  	}
    98  	_, r.Err = c.Post(rootURL(c), b, &r.Body, nil)
    99  	return
   100  }
   101  
   102  // Get retrieves a particular floating IP resource based on its unique ID.
   103  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   104  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
   105  	return
   106  }
   107  
   108  // UpdateOptsBuilder allows extensions to add additional parameters to the
   109  // Update request.
   110  type UpdateOptsBuilder interface {
   111  	ToFloatingIPUpdateMap() (map[string]interface{}, error)
   112  }
   113  
   114  // UpdateOpts contains the values used when updating a floating IP resource. The
   115  // only value that can be updated is which internal port the floating IP is
   116  // linked to. To associate the floating IP with a new internal port, provide its
   117  // ID. To disassociate the floating IP from all ports, provide an empty string.
   118  type UpdateOpts struct {
   119  	PortID *string `json:"port_id"`
   120  }
   121  
   122  // ToFloatingIPUpdateMap allows UpdateOpts to satisfy the UpdateOptsBuilder
   123  // interface
   124  func (opts UpdateOpts) ToFloatingIPUpdateMap() (map[string]interface{}, error) {
   125  	return golangsdk.BuildRequestBody(opts, "floatingip")
   126  }
   127  
   128  // Update allows floating IP resources to be updated. Currently, the only way to
   129  // "update" a floating IP is to associate it with a new internal port, or
   130  // disassociated it from all ports. See UpdateOpts for instructions of how to
   131  // do this.
   132  func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   133  	b, err := opts.ToFloatingIPUpdateMap()
   134  	if err != nil {
   135  		r.Err = err
   136  		return
   137  	}
   138  	_, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
   139  		OkCodes: []int{200},
   140  	})
   141  	return
   142  }
   143  
   144  // Delete will permanently delete a particular floating IP resource. Please
   145  // ensure this is what you want - you can also disassociate the IP from existing
   146  // internal ports.
   147  func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   148  	_, r.Err = c.Delete(resourceURL(c, id), nil)
   149  	return
   150  }