github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/loadbalancer/v2/loadbalancers/requests.go (about)

     1  package loadbalancers
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/vnpaycloud-console/gophercloud/v2"
     7  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/loadbalancer/v2/listeners"
     8  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/loadbalancer/v2/pools"
     9  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
    10  )
    11  
    12  // ListOptsBuilder allows extensions to add additional parameters to the
    13  // List request.
    14  type ListOptsBuilder interface {
    15  	ToLoadBalancerListQuery() (string, error)
    16  }
    17  
    18  // ListOpts allows the filtering and sorting of paginated collections through
    19  // the API. Filtering is achieved by passing in struct field values that map to
    20  // the Loadbalancer attributes you want to see returned. SortKey allows you to
    21  // sort by a particular attribute. SortDir sets the direction, and is
    22  // either `asc' or `desc'. Marker and Limit are used for pagination.
    23  type ListOpts struct {
    24  	Description        string   `q:"description"`
    25  	AdminStateUp       *bool    `q:"admin_state_up"`
    26  	ProjectID          string   `q:"project_id"`
    27  	ProvisioningStatus string   `q:"provisioning_status"`
    28  	VipAddress         string   `q:"vip_address"`
    29  	VipPortID          string   `q:"vip_port_id"`
    30  	VipSubnetID        string   `q:"vip_subnet_id"`
    31  	VipNetworkID       string   `q:"vip_network_id"`
    32  	ID                 string   `q:"id"`
    33  	OperatingStatus    string   `q:"operating_status"`
    34  	Name               string   `q:"name"`
    35  	FlavorID           string   `q:"flavor_id"`
    36  	AvailabilityZone   string   `q:"availability_zone"`
    37  	Provider           string   `q:"provider"`
    38  	Limit              int      `q:"limit"`
    39  	Marker             string   `q:"marker"`
    40  	SortKey            string   `q:"sort_key"`
    41  	SortDir            string   `q:"sort_dir"`
    42  	Tags               []string `q:"tags"`
    43  	TagsAny            []string `q:"tags-any"`
    44  	TagsNot            []string `q:"not-tags"`
    45  	TagsNotAny         []string `q:"not-tags-any"`
    46  }
    47  
    48  // ToLoadBalancerListQuery formats a ListOpts into a query string.
    49  func (opts ListOpts) ToLoadBalancerListQuery() (string, error) {
    50  	q, err := gophercloud.BuildQueryString(opts)
    51  	return q.String(), err
    52  }
    53  
    54  // List returns a Pager which allows you to iterate over a collection of
    55  // load balancers. It accepts a ListOpts struct, which allows you to filter
    56  // and sort the returned collection for greater efficiency.
    57  //
    58  // Default policy settings return only those load balancers that are owned by
    59  // the project who submits the request, unless an admin user submits the request.
    60  func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    61  	url := rootURL(c)
    62  	if opts != nil {
    63  		query, err := opts.ToLoadBalancerListQuery()
    64  		if err != nil {
    65  			return pagination.Pager{Err: err}
    66  		}
    67  		url += query
    68  	}
    69  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    70  		return LoadBalancerPage{pagination.LinkedPageBase{PageResult: r}}
    71  	})
    72  }
    73  
    74  // CreateOptsBuilder allows extensions to add additional parameters to the
    75  // Create request.
    76  type CreateOptsBuilder interface {
    77  	ToLoadBalancerCreateMap() (map[string]any, error)
    78  }
    79  
    80  // CreateOpts is the common options struct used in this package's Create
    81  // operation.
    82  type CreateOpts struct {
    83  	// Human-readable name for the Loadbalancer. Does not have to be unique.
    84  	Name string `json:"name,omitempty"`
    85  
    86  	// Human-readable description for the Loadbalancer.
    87  	Description string `json:"description,omitempty"`
    88  
    89  	// Providing a neutron port ID for the vip_port_id tells Octavia to use this
    90  	// port for the VIP. If the port has more than one subnet you must specify
    91  	// either the vip_subnet_id or vip_address to clarify which address should
    92  	// be used for the VIP.
    93  	VipPortID string `json:"vip_port_id,omitempty"`
    94  
    95  	// The subnet on which to allocate the Loadbalancer's address. A project can
    96  	// only create Loadbalancers on networks authorized by policy (e.g. networks
    97  	// that belong to them or networks that are shared).
    98  	VipSubnetID string `json:"vip_subnet_id,omitempty"`
    99  
   100  	// The network on which to allocate the Loadbalancer's address. A tenant can
   101  	// only create Loadbalancers on networks authorized by policy (e.g. networks
   102  	// that belong to them or networks that are shared).
   103  	VipNetworkID string `json:"vip_network_id,omitempty"`
   104  
   105  	// ProjectID is the UUID of the project who owns the Loadbalancer.
   106  	// Only administrative users can specify a project UUID other than their own.
   107  	ProjectID string `json:"project_id,omitempty"`
   108  
   109  	// The IP address of the Loadbalancer.
   110  	VipAddress string `json:"vip_address,omitempty"`
   111  
   112  	// The ID of the QoS Policy which will apply to the Virtual IP
   113  	VipQosPolicyID string `json:"vip_qos_policy_id,omitempty"`
   114  
   115  	// The administrative state of the Loadbalancer. A valid value is true (UP)
   116  	// or false (DOWN).
   117  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   118  
   119  	// The UUID of a flavor.
   120  	FlavorID string `json:"flavor_id,omitempty"`
   121  
   122  	// The name of an Octavia availability zone.
   123  	// Requires Octavia API version 2.14 or later.
   124  	AvailabilityZone string `json:"availability_zone,omitempty"`
   125  
   126  	// The name of the provider.
   127  	Provider string `json:"provider,omitempty"`
   128  
   129  	// Listeners is a slice of listeners.CreateOpts which allows a set
   130  	// of listeners to be created at the same time the Loadbalancer is created.
   131  	//
   132  	// This is only possible to use when creating a fully populated
   133  	// load balancer.
   134  	Listeners []listeners.CreateOpts `json:"listeners,omitempty"`
   135  
   136  	// Pools is a slice of pools.CreateOpts which allows a set of pools
   137  	// to be created at the same time the Loadbalancer is created.
   138  	//
   139  	// This is only possible to use when creating a fully populated
   140  	// load balancer.
   141  	Pools []pools.CreateOpts `json:"pools,omitempty"`
   142  
   143  	// Tags is a set of resource tags.
   144  	Tags []string `json:"tags,omitempty"`
   145  
   146  	// The additional ips of the loadbalancer. Subnets must all belong to the same network as the primary VIP.
   147  	// New in version 2.26
   148  	AdditionalVips []AdditionalVip `json:"additional_vips,omitempty"`
   149  }
   150  
   151  // ToLoadBalancerCreateMap builds a request body from CreateOpts.
   152  func (opts CreateOpts) ToLoadBalancerCreateMap() (map[string]any, error) {
   153  	return gophercloud.BuildRequestBody(opts, "loadbalancer")
   154  }
   155  
   156  // Create is an operation which provisions a new loadbalancer based on the
   157  // configuration defined in the CreateOpts struct. Once the request is
   158  // validated and progress has started on the provisioning process, a
   159  // CreateResult will be returned.
   160  func Create(ctx context.Context, c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   161  	b, err := opts.ToLoadBalancerCreateMap()
   162  	if err != nil {
   163  		r.Err = err
   164  		return
   165  	}
   166  	resp, err := c.Post(ctx, rootURL(c), b, &r.Body, nil)
   167  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   168  	return
   169  }
   170  
   171  // Get retrieves a particular Loadbalancer based on its unique ID.
   172  func Get(ctx context.Context, c *gophercloud.ServiceClient, id string) (r GetResult) {
   173  	resp, err := c.Get(ctx, resourceURL(c, id), &r.Body, nil)
   174  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   175  	return
   176  }
   177  
   178  // UpdateOptsBuilder allows extensions to add additional parameters to the
   179  // Update request.
   180  type UpdateOptsBuilder interface {
   181  	ToLoadBalancerUpdateMap() (map[string]any, error)
   182  }
   183  
   184  // UpdateOpts is the common options struct used in this package's Update
   185  // operation.
   186  type UpdateOpts struct {
   187  	// Human-readable name for the Loadbalancer. Does not have to be unique.
   188  	Name *string `json:"name,omitempty"`
   189  
   190  	// Human-readable description for the Loadbalancer.
   191  	Description *string `json:"description,omitempty"`
   192  
   193  	// The administrative state of the Loadbalancer. A valid value is true (UP)
   194  	// or false (DOWN).
   195  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   196  
   197  	// The ID of the QoS Policy which will apply to the Virtual IP
   198  	VipQosPolicyID *string `json:"vip_qos_policy_id,omitempty"`
   199  
   200  	// Tags is a set of resource tags.
   201  	Tags *[]string `json:"tags,omitempty"`
   202  }
   203  
   204  // ToLoadBalancerUpdateMap builds a request body from UpdateOpts.
   205  func (opts UpdateOpts) ToLoadBalancerUpdateMap() (map[string]any, error) {
   206  	return gophercloud.BuildRequestBody(opts, "loadbalancer")
   207  }
   208  
   209  // Update is an operation which modifies the attributes of the specified
   210  // LoadBalancer.
   211  func Update(ctx context.Context, c *gophercloud.ServiceClient, id string, opts UpdateOpts) (r UpdateResult) {
   212  	b, err := opts.ToLoadBalancerUpdateMap()
   213  	if err != nil {
   214  		r.Err = err
   215  		return
   216  	}
   217  	resp, err := c.Put(ctx, resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
   218  		OkCodes: []int{200, 202},
   219  	})
   220  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   221  	return
   222  }
   223  
   224  // DeleteOptsBuilder allows extensions to add additional parameters to the
   225  // Delete request.
   226  type DeleteOptsBuilder interface {
   227  	ToLoadBalancerDeleteQuery() (string, error)
   228  }
   229  
   230  // DeleteOpts is the common options struct used in this package's Delete
   231  // operation.
   232  type DeleteOpts struct {
   233  	// Cascade will delete all children of the load balancer (listners, monitors, etc).
   234  	Cascade bool `q:"cascade"`
   235  }
   236  
   237  // ToLoadBalancerDeleteQuery formats a DeleteOpts into a query string.
   238  func (opts DeleteOpts) ToLoadBalancerDeleteQuery() (string, error) {
   239  	q, err := gophercloud.BuildQueryString(opts)
   240  	return q.String(), err
   241  }
   242  
   243  // Delete will permanently delete a particular LoadBalancer based on its
   244  // unique ID.
   245  func Delete(ctx context.Context, c *gophercloud.ServiceClient, id string, opts DeleteOptsBuilder) (r DeleteResult) {
   246  	url := resourceURL(c, id)
   247  	if opts != nil {
   248  		query, err := opts.ToLoadBalancerDeleteQuery()
   249  		if err != nil {
   250  			r.Err = err
   251  			return
   252  		}
   253  		url += query
   254  	}
   255  	resp, err := c.Delete(ctx, url, nil)
   256  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   257  	return
   258  }
   259  
   260  // GetStatuses will return the status of a particular LoadBalancer.
   261  func GetStatuses(ctx context.Context, c *gophercloud.ServiceClient, id string) (r GetStatusesResult) {
   262  	resp, err := c.Get(ctx, statusRootURL(c, id), &r.Body, nil)
   263  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   264  	return
   265  }
   266  
   267  // GetStats will return the shows the current statistics of a particular LoadBalancer.
   268  func GetStats(ctx context.Context, c *gophercloud.ServiceClient, id string) (r StatsResult) {
   269  	resp, err := c.Get(ctx, statisticsRootURL(c, id), &r.Body, nil)
   270  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   271  	return
   272  }
   273  
   274  // Failover performs a failover of a load balancer.
   275  func Failover(ctx context.Context, c *gophercloud.ServiceClient, id string) (r FailoverResult) {
   276  	resp, err := c.Put(ctx, failoverRootURL(c, id), nil, nil, &gophercloud.RequestOpts{
   277  		OkCodes: []int{202},
   278  	})
   279  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   280  	return
   281  }