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

     1  package pools
     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  	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 *golangsdk.ServiceClient, opts ListOpts) pagination.Pager {
    36  	q, err := golangsdk.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 golangsdk.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 *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   102  	b, err := opts.ToLBPoolCreateMap()
   103  	if err != nil {
   104  		r.Err = err
   105  		return
   106  	}
   107  	_, r.Err = c.Post(rootURL(c), b, &r.Body, nil)
   108  	return
   109  }
   110  
   111  // Get retrieves a particular pool based on its unique ID.
   112  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   113  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
   114  	return
   115  }
   116  
   117  // UpdateOptsBuilder allows extensions to add additional parameters ot the
   118  // Update request.
   119  type UpdateOptsBuilder interface {
   120  	ToLBPoolUpdateMap() (map[string]interface{}, error)
   121  }
   122  
   123  // UpdateOpts contains the values used when updating a pool.
   124  type UpdateOpts struct {
   125  	// Name of the pool.
   126  	Name string `json:"name,omitempty"`
   127  
   128  	// LBMethod is the algorithm used to distribute load between the members of
   129  	// the pool. The current specification supports LBMethodRoundRobin and
   130  	// LBMethodLeastConnections as valid values for this attribute.
   131  	LBMethod LBMethod `json:"lb_method,omitempty"`
   132  }
   133  
   134  // ToLBPoolUpdateMap builds a request body based on UpdateOpts.
   135  func (opts UpdateOpts) ToLBPoolUpdateMap() (map[string]interface{}, error) {
   136  	return golangsdk.BuildRequestBody(opts, "pool")
   137  }
   138  
   139  // Update allows pools to be updated.
   140  func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   141  	b, err := opts.ToLBPoolUpdateMap()
   142  	if err != nil {
   143  		r.Err = err
   144  		return
   145  	}
   146  	_, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
   147  		OkCodes: []int{200},
   148  	})
   149  	return
   150  }
   151  
   152  // Delete will permanently delete a particular pool based on its unique ID.
   153  func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   154  	_, r.Err = c.Delete(resourceURL(c, id), nil)
   155  	return
   156  }
   157  
   158  // AssociateMonitor will associate a health monitor with a particular pool.
   159  // Once associated, the health monitor will start monitoring the members of the
   160  // pool and will deactivate these members if they are deemed unhealthy. A
   161  // member can be deactivated (status set to INACTIVE) if any of health monitors
   162  // finds it unhealthy.
   163  func AssociateMonitor(c *golangsdk.ServiceClient, poolID, monitorID string) (r AssociateResult) {
   164  	b := map[string]interface{}{"health_monitor": map[string]string{"id": monitorID}}
   165  	_, r.Err = c.Post(associateURL(c, poolID), b, &r.Body, nil)
   166  	return
   167  }
   168  
   169  // DisassociateMonitor will disassociate a health monitor with a particular
   170  // pool. When dissociation is successful, the health monitor will no longer
   171  // check for the health of the members of the pool.
   172  func DisassociateMonitor(c *golangsdk.ServiceClient, poolID, monitorID string) (r AssociateResult) {
   173  	_, r.Err = c.Delete(disassociateURL(c, poolID, monitorID), nil)
   174  	return
   175  }