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

     1  package floatingips
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/pagination"
     6  )
     7  
     8  // List returns a Pager that allows you to iterate over a collection of FloatingIPs.
     9  func List(client *golangsdk.ServiceClient) pagination.Pager {
    10  	return pagination.NewPager(client, listURL(client), func(r pagination.PageResult) pagination.Page {
    11  		return FloatingIPPage{pagination.SinglePageBase(r)}
    12  	})
    13  }
    14  
    15  // CreateOptsBuilder allows extensions to add additional parameters to the
    16  // Create request.
    17  type CreateOptsBuilder interface {
    18  	ToFloatingIPCreateMap() (map[string]interface{}, error)
    19  }
    20  
    21  // CreateOpts specifies a Floating IP allocation request.
    22  type CreateOpts struct {
    23  	// Pool is the pool of Floating IPs to allocate one from.
    24  	Pool string `json:"pool" required:"true"`
    25  }
    26  
    27  // ToFloatingIPCreateMap constructs a request body from CreateOpts.
    28  func (opts CreateOpts) ToFloatingIPCreateMap() (map[string]interface{}, error) {
    29  	return golangsdk.BuildRequestBody(opts, "")
    30  }
    31  
    32  // Create requests the creation of a new Floating IP.
    33  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    34  	b, err := opts.ToFloatingIPCreateMap()
    35  	if err != nil {
    36  		r.Err = err
    37  		return
    38  	}
    39  	_, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{
    40  		OkCodes: []int{200},
    41  	})
    42  	return
    43  }
    44  
    45  // Get returns data about a previously created Floating IP.
    46  func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
    47  	_, r.Err = client.Get(getURL(client, id), &r.Body, nil)
    48  	return
    49  }
    50  
    51  // Delete requests the deletion of a previous allocated Floating IP.
    52  func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) {
    53  	_, r.Err = client.Delete(deleteURL(client, id), nil)
    54  	return
    55  }
    56  
    57  // AssociateOptsBuilder allows extensions to add additional parameters to the
    58  // Associate request.
    59  type AssociateOptsBuilder interface {
    60  	ToFloatingIPAssociateMap() (map[string]interface{}, error)
    61  }
    62  
    63  // AssociateOpts specifies the required information to associate a Floating IP with an instance
    64  type AssociateOpts struct {
    65  	// FloatingIP is the Floating IP to associate with an instance.
    66  	FloatingIP string `json:"address" required:"true"`
    67  
    68  	// FixedIP is an optional fixed IP address of the server.
    69  	FixedIP string `json:"fixed_address,omitempty"`
    70  }
    71  
    72  // ToFloatingIPAssociateMap constructs a request body from AssociateOpts.
    73  func (opts AssociateOpts) ToFloatingIPAssociateMap() (map[string]interface{}, error) {
    74  	return golangsdk.BuildRequestBody(opts, "addFloatingIp")
    75  }
    76  
    77  // AssociateInstance pairs an allocated Floating IP with a server.
    78  func AssociateInstance(client *golangsdk.ServiceClient, serverID string, opts AssociateOptsBuilder) (r AssociateResult) {
    79  	b, err := opts.ToFloatingIPAssociateMap()
    80  	if err != nil {
    81  		r.Err = err
    82  		return
    83  	}
    84  	_, r.Err = client.Post(associateURL(client, serverID), b, nil, nil)
    85  	return
    86  }
    87  
    88  // DisassociateOptsBuilder allows extensions to add additional parameters to
    89  // the Disassociate request.
    90  type DisassociateOptsBuilder interface {
    91  	ToFloatingIPDisassociateMap() (map[string]interface{}, error)
    92  }
    93  
    94  // DisassociateOpts specifies the required information to disassociate a
    95  // Floating IP with a server.
    96  type DisassociateOpts struct {
    97  	FloatingIP string `json:"address" required:"true"`
    98  }
    99  
   100  // ToFloatingIPDisassociateMap constructs a request body from DisassociateOpts.
   101  func (opts DisassociateOpts) ToFloatingIPDisassociateMap() (map[string]interface{}, error) {
   102  	return golangsdk.BuildRequestBody(opts, "removeFloatingIp")
   103  }
   104  
   105  // DisassociateInstance decouples an allocated Floating IP from an instance
   106  func DisassociateInstance(client *golangsdk.ServiceClient, serverID string, opts DisassociateOptsBuilder) (r DisassociateResult) {
   107  	b, err := opts.ToFloatingIPDisassociateMap()
   108  	if err != nil {
   109  		r.Err = err
   110  		return
   111  	}
   112  	_, r.Err = client.Post(disassociateURL(client, serverID), b, nil, nil)
   113  	return
   114  }