github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/extensions/lbaas/pools/requests.go (about)

     1  package pools
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/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  	Status       string `q:"status"`
    15  	LBMethod     string `q:"lb_method"`
    16  	Protocol     string `q:"protocol"`
    17  	SubnetID     string `q:"subnet_id"`
    18  	TenantID     string `q:"tenant_id"`
    19  	AdminStateUp *bool  `q:"admin_state_up"`
    20  	Name         string `q:"name"`
    21  	ID           string `q:"id"`
    22  	VIPID        string `q:"vip_id"`
    23  	Limit        int    `q:"limit"`
    24  	Marker       string `q:"marker"`
    25  	SortKey      string `q:"sort_key"`
    26  	SortDir      string `q:"sort_dir"`
    27  }
    28  
    29  // List returns a Pager which allows you to iterate over a collection of
    30  // pools. It accepts a ListOpts struct, which allows you to filter and sort
    31  // the returned collection for greater efficiency.
    32  //
    33  // Default policy settings return only those pools that are owned by the
    34  // tenant who submits the request, unless an admin user submits the request.
    35  func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
    36  	q, err := gophercloud.BuildQueryString(&opts)
    37  	if err != nil {
    38  		return pagination.Pager{Err: err}
    39  	}
    40  	u := rootURL(c) + q.String()
    41  	return pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page {
    42  		return PoolPage{pagination.LinkedPageBase{PageResult: r}}
    43  	})
    44  }
    45  
    46  // LBMethod is a type used for possible load balancing methods.
    47  type LBMethod string
    48  
    49  // LBProtocol is a type used for possible load balancing protocols.
    50  type LBProtocol string
    51  
    52  // Supported attributes for create/update operations.
    53  const (
    54  	LBMethodRoundRobin       LBMethod = "ROUND_ROBIN"
    55  	LBMethodLeastConnections LBMethod = "LEAST_CONNECTIONS"
    56  
    57  	ProtocolTCP   LBProtocol = "TCP"
    58  	ProtocolHTTP  LBProtocol = "HTTP"
    59  	ProtocolHTTPS LBProtocol = "HTTPS"
    60  )
    61  
    62  // CreateOptsBuilder allows extensions to add additional parameters to the
    63  // Create request.
    64  type CreateOptsBuilder interface {
    65  	ToLBPoolCreateMap() (map[string]interface{}, error)
    66  }
    67  
    68  // CreateOpts contains all the values needed to create a new pool.
    69  type CreateOpts struct {
    70  	// Name of the pool.
    71  	Name string `json:"name" required:"true"`
    72  
    73  	// Protocol used by the pool members, you can use either
    74  	// ProtocolTCP, ProtocolHTTP, or ProtocolHTTPS.
    75  	Protocol LBProtocol `json:"protocol" required:"true"`
    76  
    77  	// TenantID is only required if the caller has an admin role and wants
    78  	// to create a pool for another tenant.
    79  	TenantID string `json:"tenant_id,omitempty"`
    80  
    81  	// SubnetID is the network on which the members of the pool will be located.
    82  	// Only members that are on this network can be added to the pool.
    83  	SubnetID string `json:"subnet_id,omitempty"`
    84  
    85  	// LBMethod is the algorithm used to distribute load between the members of
    86  	// the pool. The current specification supports LBMethodRoundRobin and
    87  	// LBMethodLeastConnections as valid values for this attribute.
    88  	LBMethod LBMethod `json:"lb_method" required:"true"`
    89  
    90  	// Provider of the pool.
    91  	Provider string `json:"provider,omitempty"`
    92  }
    93  
    94  // ToLBPoolCreateMap builds a request body based on CreateOpts.
    95  func (opts CreateOpts) ToLBPoolCreateMap() (map[string]interface{}, error) {
    96  	return gophercloud.BuildRequestBody(opts, "pool")
    97  }
    98  
    99  // Create accepts a CreateOptsBuilder and uses the values to create a new
   100  // load balancer pool.
   101  func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   102  	b, err := opts.ToLBPoolCreateMap()
   103  	if err != nil {
   104  		r.Err = err
   105  		return
   106  	}
   107  	resp, err := c.Post(rootURL(c), b, &r.Body, nil)
   108  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   109  	return
   110  }
   111  
   112  // Get retrieves a particular pool based on its unique ID.
   113  func Get(c *gophercloud.ServiceClient, id string) (r GetResult) {
   114  	resp, err := c.Get(resourceURL(c, id), &r.Body, nil)
   115  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   116  	return
   117  }
   118  
   119  // UpdateOptsBuilder allows extensions to add additional parameters ot the
   120  // Update request.
   121  type UpdateOptsBuilder interface {
   122  	ToLBPoolUpdateMap() (map[string]interface{}, error)
   123  }
   124  
   125  // UpdateOpts contains the values used when updating a pool.
   126  type UpdateOpts struct {
   127  	// Name of the pool.
   128  	Name *string `json:"name,omitempty"`
   129  
   130  	// LBMethod is the algorithm used to distribute load between the members of
   131  	// the pool. The current specification supports LBMethodRoundRobin and
   132  	// LBMethodLeastConnections as valid values for this attribute.
   133  	LBMethod LBMethod `json:"lb_method,omitempty"`
   134  }
   135  
   136  // ToLBPoolUpdateMap builds a request body based on UpdateOpts.
   137  func (opts UpdateOpts) ToLBPoolUpdateMap() (map[string]interface{}, error) {
   138  	return gophercloud.BuildRequestBody(opts, "pool")
   139  }
   140  
   141  // Update allows pools to be updated.
   142  func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   143  	b, err := opts.ToLBPoolUpdateMap()
   144  	if err != nil {
   145  		r.Err = err
   146  		return
   147  	}
   148  	resp, err := c.Put(resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
   149  		OkCodes: []int{200},
   150  	})
   151  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   152  	return
   153  }
   154  
   155  // Delete will permanently delete a particular pool based on its unique ID.
   156  func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) {
   157  	resp, err := c.Delete(resourceURL(c, id), nil)
   158  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   159  	return
   160  }
   161  
   162  // AssociateMonitor will associate a health monitor with a particular pool.
   163  // Once associated, the health monitor will start monitoring the members of the
   164  // pool and will deactivate these members if they are deemed unhealthy. A
   165  // member can be deactivated (status set to INACTIVE) if any of health monitors
   166  // finds it unhealthy.
   167  func AssociateMonitor(c *gophercloud.ServiceClient, poolID, monitorID string) (r AssociateResult) {
   168  	b := map[string]interface{}{"health_monitor": map[string]string{"id": monitorID}}
   169  	resp, err := c.Post(associateURL(c, poolID), b, &r.Body, nil)
   170  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   171  	return
   172  }
   173  
   174  // DisassociateMonitor will disassociate a health monitor with a particular
   175  // pool. When dissociation is successful, the health monitor will no longer
   176  // check for the health of the members of the pool.
   177  func DisassociateMonitor(c *gophercloud.ServiceClient, poolID, monitorID string) (r AssociateResult) {
   178  	resp, err := c.Delete(disassociateURL(c, poolID, monitorID), nil)
   179  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   180  	return
   181  }