github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/elb/v2/pools/requests.go (about)

     1  package pools
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/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  	LBMethod            string `q:"lb_algorithm"`
    21  	Protocol            string `q:"protocol"`
    22  	TenantID            string `q:"tenant_id"`
    23  	ProjectID           string `q:"project_id"`
    24  	AdminStateUp        *bool  `q:"admin_state_up"`
    25  	Name                string `q:"name"`
    26  	ID                  string `q:"id"`
    27  	Description         string `q:"description"`
    28  	HealthMonitorID     string `q:"healthmonitor_id"`
    29  	LoadbalancerID      string `q:"loadbalancer_id"`
    30  	ListenerID          string `q:"listener_id"`
    31  	MemberAddress       string `q:"member_address"`
    32  	MemberDeviceID      string `q:"member_device_id"`
    33  	EnterpriseProjectID string `q:"enterprise_project_id"`
    34  	Limit               int    `q:"limit"`
    35  	Marker              string `q:"marker"`
    36  	PageReverse         *bool  `q:"page_reverse"`
    37  	SortKey             string `q:"sort_key"`
    38  	SortDir             string `q:"sort_dir"`
    39  }
    40  
    41  // ToPoolListQuery formats a ListOpts into a query string.
    42  func (opts ListOpts) ToPoolListQuery() (string, error) {
    43  	q, err := golangsdk.BuildQueryString(opts)
    44  	return q.String(), err
    45  }
    46  
    47  // List returns a Pager which allows you to iterate over a collection of
    48  // pools. It accepts a ListOpts struct, which allows you to filter and sort
    49  // the returned collection for greater efficiency.
    50  //
    51  // Default policy settings return only those pools that are owned by the
    52  // tenant who submits the request, unless an admin user submits the request.
    53  func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    54  	url := rootURL(c)
    55  	if opts != nil {
    56  		query, err := opts.ToPoolListQuery()
    57  		if err != nil {
    58  			return pagination.Pager{Err: err}
    59  		}
    60  		url += query
    61  	}
    62  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    63  		return PoolPage{pagination.LinkedPageBase{PageResult: r}}
    64  	})
    65  }
    66  
    67  type LBMethod string
    68  type Protocol string
    69  
    70  // Supported attributes for create/update operations.
    71  const (
    72  	LBMethodRoundRobin       LBMethod = "ROUND_ROBIN"
    73  	LBMethodLeastConnections LBMethod = "LEAST_CONNECTIONS"
    74  	LBMethodSourceIp         LBMethod = "SOURCE_IP"
    75  
    76  	ProtocolTCP   Protocol = "TCP"
    77  	ProtocolHTTP  Protocol = "HTTP"
    78  	ProtocolHTTPS Protocol = "HTTPS"
    79  )
    80  
    81  // CreateOptsBuilder allows extensions to add additional parameters to the
    82  // Create request.
    83  type CreateOptsBuilder interface {
    84  	ToPoolCreateMap() (map[string]interface{}, error)
    85  }
    86  
    87  // CreateOpts is the common options struct used in this package's Create
    88  // operation.
    89  type CreateOpts struct {
    90  	// The algorithm used to distribute load between the members of the pool. The
    91  	// current specification supports LBMethodRoundRobin, LBMethodLeastConnections
    92  	// and LBMethodSourceIp as valid values for this attribute.
    93  	LBMethod LBMethod `json:"lb_algorithm" required:"true"`
    94  
    95  	// The protocol used by the pool members, you can use either
    96  	// ProtocolTCP, ProtocolHTTP, or ProtocolHTTPS.
    97  	Protocol Protocol `json:"protocol" required:"true"`
    98  
    99  	// The Loadbalancer on which the members of the pool will be associated with.
   100  	// Note: one of LoadbalancerID or ListenerID must be provided.
   101  	LoadbalancerID string `json:"loadbalancer_id,omitempty" xor:"ListenerID"`
   102  
   103  	// The Listener on which the members of the pool will be associated with.
   104  	// Note: one of LoadbalancerID or ListenerID must be provided.
   105  	ListenerID string `json:"listener_id,omitempty" xor:"LoadbalancerID"`
   106  
   107  	// TenantID is the UUID of the project who owns the Pool.
   108  	// Only administrative users can specify a project UUID other than their own.
   109  	TenantID string `json:"tenant_id,omitempty"`
   110  
   111  	// ProjectID is the UUID of the project who owns the Pool.
   112  	// Only administrative users can specify a project UUID other than their own.
   113  	ProjectID string `json:"project_id,omitempty"`
   114  
   115  	// Name of the pool.
   116  	Name string `json:"name,omitempty"`
   117  
   118  	// Human-readable description for the pool.
   119  	Description string `json:"description,omitempty"`
   120  
   121  	// Persistence is the session persistence of the pool.
   122  	// Omit this field to prevent session persistence.
   123  	Persistence *SessionPersistence `json:"session_persistence,omitempty"`
   124  
   125  	// Protection status
   126  	ProtectionStatus string `json:"protection_status,omitempty"`
   127  
   128  	// Protection reason
   129  	ProtectionReason string `json:"protection_reason,omitempty"`
   130  
   131  	// The administrative state of the Pool. A valid value is true (UP)
   132  	// or false (DOWN).
   133  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   134  }
   135  
   136  // ToPoolCreateMap builds a request body from CreateOpts.
   137  func (opts CreateOpts) ToPoolCreateMap() (map[string]interface{}, error) {
   138  	return golangsdk.BuildRequestBody(opts, "pool")
   139  }
   140  
   141  // Create accepts a CreateOpts struct and uses the values to create a new
   142  // load balancer pool.
   143  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   144  	b, err := opts.ToPoolCreateMap()
   145  	if err != nil {
   146  		r.Err = err
   147  		return
   148  	}
   149  	_, r.Err = c.Post(rootURL(c), b, &r.Body, nil)
   150  	return
   151  }
   152  
   153  // Get retrieves a particular pool based on its unique ID.
   154  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   155  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
   156  	return
   157  }
   158  
   159  // UpdateOptsBuilder allows extensions to add additional parameters to the
   160  // Update request.
   161  type UpdateOptsBuilder interface {
   162  	ToPoolUpdateMap() (map[string]interface{}, error)
   163  }
   164  
   165  // UpdateOpts is the common options struct used in this package's Update
   166  // operation.
   167  type UpdateOpts struct {
   168  	// Name of the pool.
   169  	Name string `json:"name,omitempty"`
   170  
   171  	// Human-readable description for the pool.
   172  	Description string `json:"description,omitempty"`
   173  
   174  	// The algorithm used to distribute load between the members of the pool. The
   175  	// current specification supports LBMethodRoundRobin, LBMethodLeastConnections
   176  	// and LBMethodSourceIp as valid values for this attribute.
   177  	LBMethod LBMethod `json:"lb_algorithm,omitempty"`
   178  
   179  	// The administrative state of the Pool. A valid value is true (UP)
   180  	// or false (DOWN).
   181  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   182  
   183  	// Persistence is the session persistence of the pool.
   184  	// Omit this field to prevent session persistence.
   185  	Persistence *SessionPersistence `json:"session_persistence,omitempty"`
   186  
   187  	// Update protection status
   188  	ProtectionStatus string `json:"protection_status,omitempty"`
   189  
   190  	// Update protection reason
   191  	ProtectionReason *string `json:"protection_reason,omitempty"`
   192  }
   193  
   194  // ToPoolUpdateMap builds a request body from UpdateOpts.
   195  func (opts UpdateOpts) ToPoolUpdateMap() (map[string]interface{}, error) {
   196  	return golangsdk.BuildRequestBody(opts, "pool")
   197  }
   198  
   199  // Update allows pools to be updated.
   200  func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   201  	b, err := opts.ToPoolUpdateMap()
   202  	if err != nil {
   203  		r.Err = err
   204  		return
   205  	}
   206  	_, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
   207  		OkCodes: []int{200},
   208  	})
   209  	return
   210  }
   211  
   212  // Delete will permanently delete a particular pool based on its unique ID.
   213  func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   214  	_, r.Err = c.Delete(resourceURL(c, id), nil)
   215  	return
   216  }
   217  
   218  // ListMemberOptsBuilder allows extensions to add additional parameters to the
   219  // ListMembers request.
   220  type ListMembersOptsBuilder interface {
   221  	ToMembersListQuery() (string, error)
   222  }
   223  
   224  // ListMembersOpts allows the filtering and sorting of paginated collections
   225  // through the API. Filtering is achieved by passing in struct field values
   226  // that map to the Member attributes you want to see returned. SortKey allows
   227  // you to sort by a particular Member attribute. SortDir sets the direction,
   228  // and is either `asc' or `desc'. Marker and Limit are used for pagination.
   229  type ListMembersOpts struct {
   230  	Name         string `q:"name"`
   231  	Weight       int    `q:"weight"`
   232  	AdminStateUp *bool  `q:"admin_state_up"`
   233  	TenantID     string `q:"tenant_id"`
   234  	ProjectID    string `q:"project_id"`
   235  	Address      string `q:"address"`
   236  	ProtocolPort int    `q:"protocol_port"`
   237  	ID           string `q:"id"`
   238  	SubnetID     string `q:"subnet_id"`
   239  	Limit        int    `q:"limit"`
   240  	Marker       string `q:"marker"`
   241  	PageReverse  string `q:"page_reverse"`
   242  	SortKey      string `q:"sort_key"`
   243  	SortDir      string `q:"sort_dir"`
   244  }
   245  
   246  // ToMemberListQuery formats a ListOpts into a query string.
   247  func (opts ListMembersOpts) ToMembersListQuery() (string, error) {
   248  	q, err := golangsdk.BuildQueryString(opts)
   249  	return q.String(), err
   250  }
   251  
   252  // ListMembers returns a Pager which allows you to iterate over a collection of
   253  // members. It accepts a ListMembersOptsBuilder, which allows you to filter and
   254  // sort the returned collection for greater efficiency.
   255  //
   256  // Default policy settings return only those members that are owned by the
   257  // tenant who submits the request, unless an admin user submits the request.
   258  func ListMembers(c *golangsdk.ServiceClient, poolID string, opts ListMembersOptsBuilder) pagination.Pager {
   259  	url := memberRootURL(c, poolID)
   260  	if opts != nil {
   261  		query, err := opts.ToMembersListQuery()
   262  		if err != nil {
   263  			return pagination.Pager{Err: err}
   264  		}
   265  		url += query
   266  	}
   267  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   268  		return MemberPage{pagination.LinkedPageBase{PageResult: r}}
   269  	})
   270  }
   271  
   272  // CreateMemberOptsBuilder allows extensions to add additional parameters to the
   273  // CreateMember request.
   274  type CreateMemberOptsBuilder interface {
   275  	ToMemberCreateMap() (map[string]interface{}, error)
   276  }
   277  
   278  // CreateMemberOpts is the common options struct used in this package's CreateMember
   279  // operation.
   280  type CreateMemberOpts struct {
   281  	// The IP address of the member to receive traffic from the load balancer.
   282  	Address string `json:"address" required:"true"`
   283  
   284  	// The port on which to listen for client traffic.
   285  	ProtocolPort int `json:"protocol_port" required:"true"`
   286  
   287  	// Name of the Member.
   288  	Name string `json:"name,omitempty"`
   289  
   290  	// TenantID is the UUID of the project who owns the Member.
   291  	// Only administrative users can specify a project UUID other than their own.
   292  	TenantID string `json:"tenant_id,omitempty"`
   293  
   294  	// ProjectID is the UUID of the project who owns the Member.
   295  	// Only administrative users can specify a project UUID other than their own.
   296  	ProjectID string `json:"project_id,omitempty"`
   297  
   298  	// A positive integer value that indicates the relative portion of traffic
   299  	// that  this member should receive from the pool. For example, a member with
   300  	// a weight  of 10 receives five times as much traffic as a member with a
   301  	// weight of 2.
   302  	Weight int `json:"weight,omitempty"`
   303  
   304  	// If you omit this parameter, LBaaS uses the vip_subnet_id parameter value
   305  	// for the subnet UUID.
   306  	SubnetID string `json:"subnet_id,omitempty"`
   307  
   308  	// The administrative state of the Pool. A valid value is true (UP)
   309  	// or false (DOWN).
   310  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   311  }
   312  
   313  // ToMemberCreateMap builds a request body from CreateMemberOpts.
   314  func (opts CreateMemberOpts) ToMemberCreateMap() (map[string]interface{}, error) {
   315  	return golangsdk.BuildRequestBody(opts, "member")
   316  }
   317  
   318  // CreateMember will create and associate a Member with a particular Pool.
   319  func CreateMember(c *golangsdk.ServiceClient, poolID string, opts CreateMemberOpts) (r CreateMemberResult) {
   320  	b, err := opts.ToMemberCreateMap()
   321  	if err != nil {
   322  		r.Err = err
   323  		return
   324  	}
   325  	_, r.Err = c.Post(memberRootURL(c, poolID), b, &r.Body, nil)
   326  	return
   327  }
   328  
   329  // GetMember retrieves a particular Pool Member based on its unique ID.
   330  func GetMember(c *golangsdk.ServiceClient, poolID string, memberID string) (r GetMemberResult) {
   331  	_, r.Err = c.Get(memberResourceURL(c, poolID, memberID), &r.Body, nil)
   332  	return
   333  }
   334  
   335  // UpdateMemberOptsBuilder allows extensions to add additional parameters to the
   336  // List request.
   337  type UpdateMemberOptsBuilder interface {
   338  	ToMemberUpdateMap() (map[string]interface{}, error)
   339  }
   340  
   341  // UpdateMemberOpts is the common options struct used in this package's Update
   342  // operation.
   343  type UpdateMemberOpts struct {
   344  	// Name of the Member.
   345  	Name string `json:"name,omitempty"`
   346  
   347  	// A positive integer value that indicates the relative portion of traffic
   348  	// that this member should receive from the pool. For example, a member with
   349  	// a weight of 10 receives five times as much traffic as a member with a
   350  	// weight of 2.
   351  	Weight int `json:"weight,omitempty"`
   352  
   353  	// The administrative state of the Pool. A valid value is true (UP)
   354  	// or false (DOWN).
   355  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   356  }
   357  
   358  // ToMemberUpdateMap builds a request body from UpdateMemberOpts.
   359  func (opts UpdateMemberOpts) ToMemberUpdateMap() (map[string]interface{}, error) {
   360  	return golangsdk.BuildRequestBody(opts, "member")
   361  }
   362  
   363  // Update allows Member to be updated.
   364  func UpdateMember(c *golangsdk.ServiceClient, poolID string, memberID string, opts UpdateMemberOptsBuilder) (r UpdateMemberResult) {
   365  	b, err := opts.ToMemberUpdateMap()
   366  	if err != nil {
   367  		r.Err = err
   368  		return
   369  	}
   370  	_, r.Err = c.Put(memberResourceURL(c, poolID, memberID), b, &r.Body, &golangsdk.RequestOpts{
   371  		OkCodes: []int{200, 201, 202},
   372  	})
   373  	return
   374  }
   375  
   376  // DisassociateMember will remove and disassociate a Member from a particular
   377  // Pool.
   378  func DeleteMember(c *golangsdk.ServiceClient, poolID string, memberID string) (r DeleteMemberResult) {
   379  	_, r.Err = c.Delete(memberResourceURL(c, poolID, memberID), nil)
   380  	return
   381  }