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