github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/elb/v3/pools/requests.go (about)

     1  package pools
     2  
     3  import (
     4  	golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
     5  	"github.com/opentelekomcloud/gophertelekomcloud/pagination"
     6  )
     7  
     8  // ListOptsBuilder allows extensions to add additional parameters to the
     9  // List request.
    10  type ListOptsBuilder interface {
    11  	ToPoolListQuery() (string, error)
    12  }
    13  
    14  // ListOpts allows the filtering and sorting of paginated collections through
    15  // the API. Filtering is achieved by passing in struct field values that map to
    16  // the Pool attributes you want to see returned. SortKey allows you to
    17  // sort by a particular Pool attribute. SortDir sets the direction, and is
    18  // either `asc` or `desc`. Marker and Limit are used for pagination.
    19  type ListOpts struct {
    20  	Description     []string `q:"description"`
    21  	HealthMonitorID []string `q:"healthmonitor_id"`
    22  	LBMethod        []string `q:"lb_algorithm"`
    23  	Protocol        []string `q:"protocol"`
    24  	AdminStateUp    *bool    `q:"admin_state_up"`
    25  	Name            []string `q:"name"`
    26  	ID              []string `q:"id"`
    27  	LoadbalancerID  []string `q:"loadbalancer_id"`
    28  	Limit           int      `q:"limit"`
    29  	Marker          string   `q:"marker"`
    30  	SortKey         string   `q:"sort_key"`
    31  	SortDir         string   `q:"sort_dir"`
    32  }
    33  
    34  // ToPoolListQuery formats a ListOpts into a query string.
    35  func (opts ListOpts) ToPoolListQuery() (string, error) {
    36  	q, err := golangsdk.BuildQueryString(opts)
    37  	if err != nil {
    38  		return "", err
    39  	}
    40  	return q.String(), nil
    41  }
    42  
    43  // List returns a Pager which allows you to iterate over a collection of
    44  // pools. It accepts a ListOpts struct, which allows you to filter and sort
    45  // the returned collection for greater efficiency.
    46  //
    47  // Default policy settings return only those pools that are owned by the
    48  // tenant who submits the request, unless an admin user submits the request.
    49  func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    50  	url := rootURL(client)
    51  	if opts != nil {
    52  		query, err := opts.ToPoolListQuery()
    53  		if err != nil {
    54  			return pagination.Pager{Err: err}
    55  		}
    56  		url += query
    57  	}
    58  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    59  		return PoolPage{PageWithInfo: pagination.NewPageWithInfo(r)}
    60  	})
    61  }
    62  
    63  // CreateOptsBuilder allows extensions to add additional parameters to the
    64  // Create request.
    65  type CreateOptsBuilder interface {
    66  	ToPoolCreateMap() (map[string]interface{}, error)
    67  }
    68  
    69  // CreateOpts is the common options' struct used in this package's Create
    70  // operation.
    71  type CreateOpts struct {
    72  	// The algorithm used to distribute load between the members of the pool.
    73  	LBMethod string `json:"lb_algorithm" required:"true"`
    74  
    75  	// The protocol used by the pool members, you can use either
    76  	// ProtocolTCP, ProtocolHTTP, or ProtocolHTTPS.
    77  	Protocol string `json:"protocol" required:"true"`
    78  
    79  	// The Loadbalancer on which the members of the pool will be associated with.
    80  	// Note: one of LoadbalancerID or ListenerID must be provided.
    81  	LoadbalancerID string `json:"loadbalancer_id,omitempty"`
    82  
    83  	// The Listener on which the members of the pool will be associated with.
    84  	// Note: one of LoadbalancerID or ListenerID must be provided.
    85  	ListenerID string `json:"listener_id,omitempty"`
    86  
    87  	// ProjectID is the UUID of the project who owns the Pool.
    88  	// Only administrative users can specify a project UUID other than their own.
    89  	ProjectID string `json:"project_id,omitempty"`
    90  
    91  	// Name of the pool.
    92  	Name string `json:"name,omitempty"`
    93  
    94  	// Human-readable description for the pool.
    95  	Description string `json:"description,omitempty"`
    96  
    97  	// Persistence is the session persistence of the pool.
    98  	// Omit this field to prevent session persistence.
    99  	Persistence *SessionPersistence `json:"session_persistence,omitempty"`
   100  
   101  	SlowStart *SlowStart `json:"slow_start,omitempty"`
   102  
   103  	// The administrative state of the Pool. A valid value is true (UP)
   104  	// or false (DOWN).
   105  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   106  
   107  	// Specifies whether to enable deletion protection for the pool.
   108  	DeletionProtectionEnable *bool `json:"member_deletion_protection_enable,omitempty"`
   109  
   110  	// Specifies the ID of the VPC where the backend server group works.
   111  	VpcId string `json:"vpc_id,omitempty"`
   112  
   113  	// Specifies the type of the backend server group.
   114  	// Values:
   115  	// instance: Any type of backend servers can be added. vpc_id is mandatory.
   116  	// ip: Only cross-VPC backend servers can be added. vpc_id cannot be specified.
   117  	// "": Any type of backend servers can be added.
   118  	Type string `json:"type,omitempty"`
   119  }
   120  
   121  // SessionPersistence represents the session persistence feature of the load
   122  // balancing service. It attempts to force connections or requests in the same
   123  // session to be processed by the same member as long as it is active. Three
   124  // types of persistence are supported:
   125  type SessionPersistence struct {
   126  	// The type of persistence mode.
   127  	Type string `json:"type" required:"true"`
   128  
   129  	// Name of cookie if persistence mode is set appropriately.
   130  	CookieName string `json:"cookie_name,omitempty"`
   131  
   132  	// PersistenceTimeout specifies the stickiness duration, in minutes.
   133  	PersistenceTimeout int `json:"persistence_timeout,omitempty"`
   134  }
   135  
   136  type SlowStart struct {
   137  	// Specifies whether to Enable slow start.
   138  	Enable bool `json:"enable" required:"true"`
   139  
   140  	// Specifies the slow start Duration, in seconds.
   141  	Duration int `json:"duration" required:"true"`
   142  }
   143  
   144  // ToPoolCreateMap builds a request body from CreateOpts.
   145  func (opts CreateOpts) ToPoolCreateMap() (map[string]interface{}, error) {
   146  	return golangsdk.BuildRequestBody(opts, "pool")
   147  }
   148  
   149  // Create accepts a CreateOpts struct and uses the values to create a new
   150  // load balancer pool.
   151  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   152  	b, err := opts.ToPoolCreateMap()
   153  	if err != nil {
   154  		r.Err = err
   155  		return
   156  	}
   157  	_, r.Err = client.Post(rootURL(client), b, &r.Body, nil)
   158  	return
   159  }
   160  
   161  // Get retrieves a particular pool based on its unique ID.
   162  func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
   163  	_, r.Err = client.Get(resourceURL(client, id), &r.Body, nil)
   164  	return
   165  }
   166  
   167  // UpdateOptsBuilder allows extensions to add additional parameters to the
   168  // Update request.
   169  type UpdateOptsBuilder interface {
   170  	ToPoolUpdateMap() (map[string]interface{}, error)
   171  }
   172  
   173  // UpdateOpts is the common options' struct used in this package's Update
   174  // operation.
   175  type UpdateOpts struct {
   176  	// Name of the pool.
   177  	Name *string `json:"name,omitempty"`
   178  
   179  	// Human-readable description for the pool.
   180  	Description *string `json:"description,omitempty"`
   181  
   182  	// The algorithm used to distribute load between the members of the pool. The
   183  	// current specification supports LBMethodRoundRobin, LBMethodLeastConnections
   184  	// and LBMethodSourceIp as valid values for this attribute.
   185  	LBMethod string `json:"lb_algorithm,omitempty"`
   186  
   187  	// Specifies whether to enable sticky sessions.
   188  	Persistence *SessionPersistence `json:"session_persistence,omitempty"`
   189  
   190  	// The administrative state of the Pool. The value can only be updated to true.
   191  	// This parameter is unsupported. Please do not use it.
   192  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   193  
   194  	// Specifies whether to enable slow start.
   195  	// This parameter is unsupported. Please do not use it.
   196  	SlowStart *SlowStart `json:"slow_start,omitempty"`
   197  
   198  	// Specifies whether to enable deletion protection for the load balancer.
   199  	DeletionProtectionEnable *bool `json:"member_deletion_protection_enable,omitempty"`
   200  
   201  	// Specifies the ID of the VPC where the backend server group works.
   202  	VpcId string `json:"vpc_id,omitempty"`
   203  
   204  	// Specifies the type of the backend server group.
   205  	// Values:
   206  	// instance: Any type of backend servers can be added. vpc_id is mandatory.
   207  	// ip: Only cross-VPC backend servers can be added. vpc_id cannot be specified.
   208  	// "": Any type of backend servers can be added.
   209  	Type string `json:"type,omitempty"`
   210  }
   211  
   212  // ToPoolUpdateMap builds a request body from UpdateOpts.
   213  func (opts UpdateOpts) ToPoolUpdateMap() (map[string]interface{}, error) {
   214  	return golangsdk.BuildRequestBody(opts, "pool")
   215  }
   216  
   217  // Update allows pools to be updated.
   218  func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   219  	b, err := opts.ToPoolUpdateMap()
   220  	if err != nil {
   221  		r.Err = err
   222  		return
   223  	}
   224  	_, r.Err = client.Put(resourceURL(client, id), b, &r.Body, &golangsdk.RequestOpts{
   225  		OkCodes: []int{200},
   226  	})
   227  	return
   228  }
   229  
   230  // Delete will permanently delete a particular pool based on its unique ID.
   231  func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) {
   232  	_, r.Err = client.Delete(resourceURL(client, id), nil)
   233  	return
   234  }