github.com/gophercloud/gophercloud@v1.11.0/openstack/loadbalancer/v2/pools/requests.go (about)

     1  package pools
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/monitors"
     6  	"github.com/gophercloud/gophercloud/pagination"
     7  )
     8  
     9  // ListOptsBuilder allows extensions to add additional parameters to the
    10  // List request.
    11  type ListOptsBuilder interface {
    12  	ToPoolListQuery() (string, error)
    13  }
    14  
    15  // ListOpts allows the filtering and sorting of paginated collections through
    16  // the API. Filtering is achieved by passing in struct field values that map to
    17  // the Pool attributes you want to see returned. SortKey allows you to
    18  // sort by a particular Pool attribute. SortDir sets the direction, and is
    19  // either `asc' or `desc'. Marker and Limit are used for pagination.
    20  type ListOpts struct {
    21  	LBMethod       string `q:"lb_algorithm"`
    22  	Protocol       string `q:"protocol"`
    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  	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 := gophercloud.BuildQueryString(opts)
    37  	return q.String(), err
    38  }
    39  
    40  // List returns a Pager which allows you to iterate over a collection of
    41  // pools. It accepts a ListOpts struct, which allows you to filter and sort
    42  // the returned collection for greater efficiency.
    43  //
    44  // Default policy settings return only those pools that are owned by the
    45  // project who submits the request, unless an admin user submits the request.
    46  func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    47  	url := rootURL(c)
    48  	if opts != nil {
    49  		query, err := opts.ToPoolListQuery()
    50  		if err != nil {
    51  			return pagination.Pager{Err: err}
    52  		}
    53  		url += query
    54  	}
    55  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    56  		return PoolPage{pagination.LinkedPageBase{PageResult: r}}
    57  	})
    58  }
    59  
    60  type LBMethod string
    61  type Protocol string
    62  
    63  // Supported attributes for create/update operations.
    64  const (
    65  	LBMethodRoundRobin       LBMethod = "ROUND_ROBIN"
    66  	LBMethodLeastConnections LBMethod = "LEAST_CONNECTIONS"
    67  	LBMethodSourceIp         LBMethod = "SOURCE_IP"
    68  	LBMethodSourceIpPort     LBMethod = "SOURCE_IP_PORT"
    69  
    70  	ProtocolTCP   Protocol = "TCP"
    71  	ProtocolUDP   Protocol = "UDP"
    72  	ProtocolPROXY Protocol = "PROXY"
    73  	ProtocolHTTP  Protocol = "HTTP"
    74  	ProtocolHTTPS Protocol = "HTTPS"
    75  	// Protocol PROXYV2 requires octavia microversion 2.22
    76  	ProtocolPROXYV2 Protocol = "PROXYV2"
    77  	// Protocol SCTP requires octavia microversion 2.23
    78  	ProtocolSCTP Protocol = "SCTP"
    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  	// LBMethodSourceIp and LBMethodSourceIpPort 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, ProtocolUDP, ProtocolPROXY, ProtocolHTTP, ProtocolHTTPS,
    97  	// ProtocolSCTP or ProtocolPROXYV2.
    98  	Protocol Protocol `json:"protocol" required:"true"`
    99  
   100  	// The Loadbalancer on which the members of the pool will be associated with.
   101  	// Note: one of LoadbalancerID or ListenerID must be provided.
   102  	LoadbalancerID string `json:"loadbalancer_id,omitempty"`
   103  
   104  	// The Listener on which the members of the pool will be associated with.
   105  	// Note: one of LoadbalancerID or ListenerID must be provided.
   106  	ListenerID string `json:"listener_id,omitempty"`
   107  
   108  	// ProjectID is the UUID of the project who owns the Pool.
   109  	// Only administrative users can specify a project UUID other than their own.
   110  	ProjectID string `json:"project_id,omitempty"`
   111  
   112  	// Name of the pool.
   113  	Name string `json:"name,omitempty"`
   114  
   115  	// Human-readable description for the pool.
   116  	Description string `json:"description,omitempty"`
   117  
   118  	// Persistence is the session persistence of the pool.
   119  	// Omit this field to prevent session persistence.
   120  	Persistence *SessionPersistence `json:"session_persistence,omitempty"`
   121  
   122  	// The administrative state of the Pool. A valid value is true (UP)
   123  	// or false (DOWN).
   124  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   125  
   126  	// Members is a slice of BatchUpdateMemberOpts which allows a set of
   127  	// members to be created at the same time the pool is created.
   128  	//
   129  	// This is only possible to use when creating a fully populated
   130  	// Loadbalancer.
   131  	Members []BatchUpdateMemberOpts `json:"members,omitempty"`
   132  
   133  	// Monitor is an instance of monitors.CreateOpts which allows a monitor
   134  	// to be created at the same time the pool is created.
   135  	//
   136  	// This is only possible to use when creating a fully populated
   137  	// Loadbalancer.
   138  	Monitor *monitors.CreateOpts `json:"healthmonitor,omitempty"`
   139  
   140  	// Tags is a set of resource tags. New in version 2.5
   141  	Tags []string `json:"tags,omitempty"`
   142  }
   143  
   144  // ToPoolCreateMap builds a request body from CreateOpts.
   145  func (opts CreateOpts) ToPoolCreateMap() (map[string]interface{}, error) {
   146  	return gophercloud.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(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   152  	b, err := opts.ToPoolCreateMap()
   153  	if err != nil {
   154  		r.Err = err
   155  		return
   156  	}
   157  	resp, err := c.Post(rootURL(c), b, &r.Body, nil)
   158  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   159  	return
   160  }
   161  
   162  // Get retrieves a particular pool based on its unique ID.
   163  func Get(c *gophercloud.ServiceClient, id string) (r GetResult) {
   164  	resp, err := c.Get(resourceURL(c, id), &r.Body, nil)
   165  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   166  	return
   167  }
   168  
   169  // UpdateOptsBuilder allows extensions to add additional parameters to the
   170  // Update request.
   171  type UpdateOptsBuilder interface {
   172  	ToPoolUpdateMap() (map[string]interface{}, error)
   173  }
   174  
   175  // UpdateOpts is the common options struct used in this package's Update
   176  // operation.
   177  type UpdateOpts struct {
   178  	// Name of the pool.
   179  	Name *string `json:"name,omitempty"`
   180  
   181  	// Human-readable description for the pool.
   182  	Description *string `json:"description,omitempty"`
   183  
   184  	// The algorithm used to distribute load between the members of the pool. The
   185  	// current specification supports LBMethodRoundRobin, LBMethodLeastConnections,
   186  	// LBMethodSourceIp and LBMethodSourceIpPort as valid values for this attribute.
   187  	LBMethod LBMethod `json:"lb_algorithm,omitempty"`
   188  
   189  	// The administrative state of the Pool. A valid value is true (UP)
   190  	// or false (DOWN).
   191  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   192  
   193  	// Persistence is the session persistence of the pool.
   194  	Persistence *SessionPersistence `json:"session_persistence,omitempty"`
   195  
   196  	// Tags is a set of resource tags. New in version 2.5
   197  	Tags *[]string `json:"tags,omitempty"`
   198  }
   199  
   200  // ToPoolUpdateMap builds a request body from UpdateOpts.
   201  func (opts UpdateOpts) ToPoolUpdateMap() (map[string]interface{}, error) {
   202  	return gophercloud.BuildRequestBody(opts, "pool")
   203  }
   204  
   205  // Update allows pools to be updated.
   206  func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   207  	b, err := opts.ToPoolUpdateMap()
   208  	if err != nil {
   209  		r.Err = err
   210  		return
   211  	}
   212  	resp, err := c.Put(resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
   213  		OkCodes: []int{200},
   214  	})
   215  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   216  	return
   217  }
   218  
   219  // Delete will permanently delete a particular pool based on its unique ID.
   220  func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) {
   221  	resp, err := c.Delete(resourceURL(c, id), nil)
   222  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   223  	return
   224  }
   225  
   226  // ListMemberOptsBuilder allows extensions to add additional parameters to the
   227  // ListMembers request.
   228  type ListMembersOptsBuilder interface {
   229  	ToMembersListQuery() (string, error)
   230  }
   231  
   232  // ListMembersOpts allows the filtering and sorting of paginated collections
   233  // through the API. Filtering is achieved by passing in struct field values
   234  // that map to the Member attributes you want to see returned. SortKey allows
   235  // you to sort by a particular Member attribute. SortDir sets the direction,
   236  // and is either `asc' or `desc'. Marker and Limit are used for pagination.
   237  type ListMembersOpts struct {
   238  	Name         string `q:"name"`
   239  	Weight       int    `q:"weight"`
   240  	AdminStateUp *bool  `q:"admin_state_up"`
   241  	ProjectID    string `q:"project_id"`
   242  	Address      string `q:"address"`
   243  	ProtocolPort int    `q:"protocol_port"`
   244  	ID           string `q:"id"`
   245  	Limit        int    `q:"limit"`
   246  	Marker       string `q:"marker"`
   247  	SortKey      string `q:"sort_key"`
   248  	SortDir      string `q:"sort_dir"`
   249  }
   250  
   251  // ToMemberListQuery formats a ListOpts into a query string.
   252  func (opts ListMembersOpts) ToMembersListQuery() (string, error) {
   253  	q, err := gophercloud.BuildQueryString(opts)
   254  	return q.String(), err
   255  }
   256  
   257  // ListMembers returns a Pager which allows you to iterate over a collection of
   258  // members. It accepts a ListMembersOptsBuilder, which allows you to filter and
   259  // sort the returned collection for greater efficiency.
   260  //
   261  // Default policy settings return only those members that are owned by the
   262  // project who submits the request, unless an admin user submits the request.
   263  func ListMembers(c *gophercloud.ServiceClient, poolID string, opts ListMembersOptsBuilder) pagination.Pager {
   264  	url := memberRootURL(c, poolID)
   265  	if opts != nil {
   266  		query, err := opts.ToMembersListQuery()
   267  		if err != nil {
   268  			return pagination.Pager{Err: err}
   269  		}
   270  		url += query
   271  	}
   272  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   273  		return MemberPage{pagination.LinkedPageBase{PageResult: r}}
   274  	})
   275  }
   276  
   277  // CreateMemberOptsBuilder allows extensions to add additional parameters to the
   278  // CreateMember request.
   279  type CreateMemberOptsBuilder interface {
   280  	ToMemberCreateMap() (map[string]interface{}, error)
   281  }
   282  
   283  // CreateMemberOpts is the common options struct used in this package's CreateMember
   284  // operation.
   285  type CreateMemberOpts struct {
   286  	// The IP address of the member to receive traffic from the load balancer.
   287  	Address string `json:"address" required:"true"`
   288  
   289  	// The port on which to listen for client traffic.
   290  	ProtocolPort int `json:"protocol_port" required:"true"`
   291  
   292  	// Name of the Member.
   293  	Name string `json:"name,omitempty"`
   294  
   295  	// ProjectID is the UUID of the project who owns the Member.
   296  	// Only administrative users can specify a project UUID other than their own.
   297  	ProjectID string `json:"project_id,omitempty"`
   298  
   299  	// A positive integer value that indicates the relative portion of traffic
   300  	// that this member should receive from the pool. For example, a member with
   301  	// a weight of 10 receives five times as much traffic as a member with a
   302  	// weight of 2.
   303  	Weight *int `json:"weight,omitempty"`
   304  
   305  	// If you omit this parameter, LBaaS uses the vip_subnet_id parameter value
   306  	// for the subnet UUID.
   307  	SubnetID string `json:"subnet_id,omitempty"`
   308  
   309  	// The administrative state of the Pool. A valid value is true (UP)
   310  	// or false (DOWN).
   311  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   312  
   313  	// Is the member a backup? Backup members only receive traffic when all
   314  	// non-backup members are down.
   315  	// Requires microversion 2.1 or later.
   316  	Backup *bool `json:"backup,omitempty"`
   317  
   318  	// An alternate IP address used for health monitoring a backend member.
   319  	MonitorAddress string `json:"monitor_address,omitempty"`
   320  
   321  	// An alternate protocol port used for health monitoring a backend member.
   322  	MonitorPort *int `json:"monitor_port,omitempty"`
   323  
   324  	// A list of simple strings assigned to the resource.
   325  	// Requires microversion 2.5 or later.
   326  	Tags []string `json:"tags,omitempty"`
   327  }
   328  
   329  // ToMemberCreateMap builds a request body from CreateMemberOpts.
   330  func (opts CreateMemberOpts) ToMemberCreateMap() (map[string]interface{}, error) {
   331  	return gophercloud.BuildRequestBody(opts, "member")
   332  }
   333  
   334  // CreateMember will create and associate a Member with a particular Pool.
   335  func CreateMember(c *gophercloud.ServiceClient, poolID string, opts CreateMemberOptsBuilder) (r CreateMemberResult) {
   336  	b, err := opts.ToMemberCreateMap()
   337  	if err != nil {
   338  		r.Err = err
   339  		return
   340  	}
   341  	resp, err := c.Post(memberRootURL(c, poolID), b, &r.Body, nil)
   342  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   343  	return
   344  }
   345  
   346  // GetMember retrieves a particular Pool Member based on its unique ID.
   347  func GetMember(c *gophercloud.ServiceClient, poolID string, memberID string) (r GetMemberResult) {
   348  	resp, err := c.Get(memberResourceURL(c, poolID, memberID), &r.Body, nil)
   349  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   350  	return
   351  }
   352  
   353  // UpdateMemberOptsBuilder allows extensions to add additional parameters to the
   354  // List request.
   355  type UpdateMemberOptsBuilder interface {
   356  	ToMemberUpdateMap() (map[string]interface{}, error)
   357  }
   358  
   359  // UpdateMemberOpts is the common options struct used in this package's Update
   360  // operation.
   361  type UpdateMemberOpts struct {
   362  	// Name of the Member.
   363  	Name *string `json:"name,omitempty"`
   364  
   365  	// A positive integer value that indicates the relative portion of traffic
   366  	// that this member should receive from the pool. For example, a member with
   367  	// a weight of 10 receives five times as much traffic as a member with a
   368  	// weight of 2.
   369  	Weight *int `json:"weight,omitempty"`
   370  
   371  	// The administrative state of the Pool. A valid value is true (UP)
   372  	// or false (DOWN).
   373  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   374  
   375  	// Is the member a backup? Backup members only receive traffic when all
   376  	// non-backup members are down.
   377  	// Requires microversion 2.1 or later.
   378  	Backup *bool `json:"backup,omitempty"`
   379  
   380  	// An alternate IP address used for health monitoring a backend member.
   381  	MonitorAddress *string `json:"monitor_address,omitempty"`
   382  
   383  	// An alternate protocol port used for health monitoring a backend member.
   384  	MonitorPort *int `json:"monitor_port,omitempty"`
   385  
   386  	// A list of simple strings assigned to the resource.
   387  	// Requires microversion 2.5 or later.
   388  	Tags []string `json:"tags,omitempty"`
   389  }
   390  
   391  // ToMemberUpdateMap builds a request body from UpdateMemberOpts.
   392  func (opts UpdateMemberOpts) ToMemberUpdateMap() (map[string]interface{}, error) {
   393  	return gophercloud.BuildRequestBody(opts, "member")
   394  }
   395  
   396  // Update allows Member to be updated.
   397  func UpdateMember(c *gophercloud.ServiceClient, poolID string, memberID string, opts UpdateMemberOptsBuilder) (r UpdateMemberResult) {
   398  	b, err := opts.ToMemberUpdateMap()
   399  	if err != nil {
   400  		r.Err = err
   401  		return
   402  	}
   403  	resp, err := c.Put(memberResourceURL(c, poolID, memberID), b, &r.Body, &gophercloud.RequestOpts{
   404  		OkCodes: []int{200, 201, 202},
   405  	})
   406  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   407  	return
   408  }
   409  
   410  // BatchUpdateMemberOptsBuilder allows extensions to add additional parameters to the BatchUpdateMembers request.
   411  type BatchUpdateMemberOptsBuilder interface {
   412  	ToBatchMemberUpdateMap() (map[string]interface{}, error)
   413  }
   414  
   415  // BatchUpdateMemberOpts is the common options struct used in this package's BatchUpdateMembers
   416  // operation.
   417  type BatchUpdateMemberOpts struct {
   418  	// The IP address of the member to receive traffic from the load balancer.
   419  	Address string `json:"address" required:"true"`
   420  
   421  	// The port on which to listen for client traffic.
   422  	ProtocolPort int `json:"protocol_port" required:"true"`
   423  
   424  	// Name of the Member.
   425  	Name *string `json:"name,omitempty"`
   426  
   427  	// ProjectID is the UUID of the project who owns the Member.
   428  	// Only administrative users can specify a project UUID other than their own.
   429  	ProjectID string `json:"project_id,omitempty"`
   430  
   431  	// A positive integer value that indicates the relative portion of traffic
   432  	// that this member should receive from the pool. For example, a member with
   433  	// a weight of 10 receives five times as much traffic as a member with a
   434  	// weight of 2.
   435  	Weight *int `json:"weight,omitempty"`
   436  
   437  	// If you omit this parameter, LBaaS uses the vip_subnet_id parameter value
   438  	// for the subnet UUID.
   439  	SubnetID *string `json:"subnet_id,omitempty"`
   440  
   441  	// The administrative state of the Pool. A valid value is true (UP)
   442  	// or false (DOWN).
   443  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   444  
   445  	// Is the member a backup? Backup members only receive traffic when all
   446  	// non-backup members are down.
   447  	// Requires microversion 2.1 or later.
   448  	Backup *bool `json:"backup,omitempty"`
   449  
   450  	// An alternate IP address used for health monitoring a backend member.
   451  	MonitorAddress *string `json:"monitor_address,omitempty"`
   452  
   453  	// An alternate protocol port used for health monitoring a backend member.
   454  	MonitorPort *int `json:"monitor_port,omitempty"`
   455  
   456  	// A list of simple strings assigned to the resource.
   457  	// Requires microversion 2.5 or later.
   458  	Tags []string `json:"tags,omitempty"`
   459  }
   460  
   461  // ToBatchMemberUpdateMap builds a request body from BatchUpdateMemberOpts.
   462  func (opts BatchUpdateMemberOpts) ToBatchMemberUpdateMap() (map[string]interface{}, error) {
   463  	b, err := gophercloud.BuildRequestBody(opts, "")
   464  	if err != nil {
   465  		return nil, err
   466  	}
   467  
   468  	if b["subnet_id"] == "" {
   469  		b["subnet_id"] = nil
   470  	}
   471  
   472  	return b, nil
   473  }
   474  
   475  // BatchUpdateMembers updates the pool members in batch
   476  func BatchUpdateMembers(c *gophercloud.ServiceClient, poolID string, opts []BatchUpdateMemberOpts) (r UpdateMembersResult) {
   477  	members := []map[string]interface{}{}
   478  	for _, opt := range opts {
   479  		b, err := opt.ToBatchMemberUpdateMap()
   480  		if err != nil {
   481  			r.Err = err
   482  			return
   483  		}
   484  		members = append(members, b)
   485  	}
   486  
   487  	b := map[string]interface{}{"members": members}
   488  
   489  	resp, err := c.Put(memberRootURL(c, poolID), b, nil, &gophercloud.RequestOpts{OkCodes: []int{202}})
   490  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   491  	return
   492  }
   493  
   494  // DeleteMember will remove and disassociate a Member from a particular Pool.
   495  func DeleteMember(c *gophercloud.ServiceClient, poolID string, memberID string) (r DeleteMemberResult) {
   496  	resp, err := c.Delete(memberResourceURL(c, poolID, memberID), nil)
   497  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   498  	return
   499  }