github.com/gophercloud/gophercloud@v1.11.0/openstack/compute/v2/extensions/floatingips/requests.go (about)

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