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

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