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

     1  package loadbalancers
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/chnsz/golangsdk"
     7  	"github.com/chnsz/golangsdk/pagination"
     8  )
     9  
    10  // ListOptsBuilder allows extensions to add additional parameters to the
    11  // List request.
    12  type ListOptsBuilder interface {
    13  	ToLoadBalancerListQuery() (string, error)
    14  }
    15  
    16  // ListOpts allows the filtering and sorting of paginated collections through
    17  // the API. Filtering is achieved by passing in struct field values that map to
    18  // the Loadbalancer attributes you want to see returned. SortKey allows you to
    19  // sort by a particular attribute. SortDir sets the direction, and is
    20  // either `asc' or `desc'. Marker and Limit are used for pagination.
    21  type ListOpts struct {
    22  	Description         string `q:"description"`
    23  	AdminStateUp        *bool  `q:"admin_state_up"`
    24  	TenantID            string `q:"tenant_id"`
    25  	ProjectID           string `q:"project_id"`
    26  	ProvisioningStatus  string `q:"provisioning_status"`
    27  	VipAddress          string `q:"vip_address"`
    28  	VipPortID           string `q:"vip_port_id"`
    29  	VipSubnetID         string `q:"vip_subnet_id"`
    30  	ID                  string `q:"id"`
    31  	OperatingStatus     string `q:"operating_status"`
    32  	Name                string `q:"name"`
    33  	Flavor              string `q:"flavor"`
    34  	Provider            string `q:"provider"`
    35  	Limit               int    `q:"limit"`
    36  	Marker              string `q:"marker"`
    37  	SortKey             string `q:"sort_key"`
    38  	SortDir             string `q:"sort_dir"`
    39  	EnterpriseProjectID string `q:"enterprise_project_id"`
    40  }
    41  
    42  // ToLoadBalancerListQuery formats a ListOpts into a query string.
    43  func (opts ListOpts) ToLoadBalancerListQuery() (string, error) {
    44  	q, err := golangsdk.BuildQueryString(opts)
    45  	return q.String(), err
    46  }
    47  
    48  // List returns a Pager which allows you to iterate over a collection of
    49  // load balancers. It accepts a ListOpts struct, which allows you to filter
    50  // and sort the returned collection for greater efficiency.
    51  //
    52  // Default policy settings return only those load balancers that are owned by
    53  // the tenant who submits the request, unless an admin user submits the request.
    54  func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    55  	url := rootURL(c)
    56  	if opts != nil {
    57  		query, err := opts.ToLoadBalancerListQuery()
    58  		if err != nil {
    59  			return pagination.Pager{Err: err}
    60  		}
    61  		url += query
    62  	}
    63  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    64  		return LoadBalancerPage{pagination.LinkedPageBase{PageResult: r}}
    65  	})
    66  }
    67  
    68  // CreateOptsBuilder allows extensions to add additional parameters to the
    69  // Create request.
    70  type CreateOptsBuilder interface {
    71  	ToLoadBalancerCreateMap() (map[string]interface{}, error)
    72  }
    73  
    74  // CreateOpts is the common options struct used in this package's Create
    75  // operation.
    76  type CreateOpts struct {
    77  	// Human-readable name for the Loadbalancer. Does not have to be unique.
    78  	Name string `json:"name,omitempty"`
    79  
    80  	// Human-readable description for the Loadbalancer.
    81  	Description string `json:"description,omitempty"`
    82  
    83  	// The network on which to allocate the Loadbalancer's address. A tenant can
    84  	// only create Loadbalancers on networks authorized by policy (e.g. networks
    85  	// that belong to them or networks that are shared).
    86  	VipSubnetID string `json:"vip_subnet_id" required:"true"`
    87  
    88  	// TenantID is the UUID of the project who owns the Loadbalancer.
    89  	// Only administrative users can specify a project UUID other than their own.
    90  	TenantID string `json:"tenant_id,omitempty"`
    91  
    92  	// ProjectID is the UUID of the project who owns the Loadbalancer.
    93  	// Only administrative users can specify a project UUID other than their own.
    94  	ProjectID string `json:"project_id,omitempty"`
    95  
    96  	// The IP address of the Loadbalancer.
    97  	VipAddress string `json:"vip_address,omitempty"`
    98  
    99  	// The administrative state of the Loadbalancer. A valid value is true (UP)
   100  	// or false (DOWN).
   101  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   102  
   103  	// The UUID of a flavor.
   104  	Flavor string `json:"flavor,omitempty"`
   105  
   106  	// The name of the provider.
   107  	Provider string `json:"provider,omitempty"`
   108  
   109  	// Enterprise project ID
   110  	EnterpriseProjectID string `json:"enterprise_project_id,omitempty"`
   111  
   112  	// Protection status
   113  	ProtectionStatus string `json:"protection_status,omitempty"`
   114  
   115  	// Protection reason
   116  	ProtectionReason string `json:"protection_reason,omitempty"`
   117  }
   118  
   119  // ToLoadBalancerCreateMap builds a request body from CreateOpts.
   120  func (opts CreateOpts) ToLoadBalancerCreateMap() (map[string]interface{}, error) {
   121  	return golangsdk.BuildRequestBody(opts, "loadbalancer")
   122  }
   123  
   124  // Create is an operation which provisions a new loadbalancer based on the
   125  // configuration defined in the CreateOpts struct. Once the request is
   126  // validated and progress has started on the provisioning process, a
   127  // CreateResult will be returned.
   128  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   129  	b, err := opts.ToLoadBalancerCreateMap()
   130  	if err != nil {
   131  		r.Err = err
   132  		return
   133  	}
   134  	_, r.Err = c.Post(rootURL(c), b, &r.Body, nil)
   135  	return
   136  }
   137  
   138  // Get retrieves a particular Loadbalancer based on its unique ID.
   139  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   140  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
   141  	return
   142  }
   143  
   144  // UpdateOptsBuilder allows extensions to add additional parameters to the
   145  // Update request.
   146  type UpdateOptsBuilder interface {
   147  	ToLoadBalancerUpdateMap() (map[string]interface{}, error)
   148  }
   149  
   150  // UpdateOpts is the common options struct used in this package's Update
   151  // operation.
   152  type UpdateOpts struct {
   153  	// Human-readable name for the Loadbalancer. Does not have to be unique.
   154  	Name string `json:"name,omitempty"`
   155  
   156  	// Human-readable description for the Loadbalancer.
   157  	Description *string `json:"description,omitempty"`
   158  
   159  	// The administrative state of the Loadbalancer. A valid value is true (UP)
   160  	// or false (DOWN).
   161  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   162  
   163  	// Update protection status
   164  	ProtectionStatus string `json:"protection_status,omitempty"`
   165  
   166  	// Update protection reason
   167  	ProtectionReason *string `json:"protection_reason,omitempty"`
   168  }
   169  
   170  // ToLoadBalancerUpdateMap builds a request body from UpdateOpts.
   171  func (opts UpdateOpts) ToLoadBalancerUpdateMap() (map[string]interface{}, error) {
   172  	return golangsdk.BuildRequestBody(opts, "loadbalancer")
   173  }
   174  
   175  // Update is an operation which modifies the attributes of the specified
   176  // LoadBalancer.
   177  func Update(c *golangsdk.ServiceClient, id string, opts UpdateOpts) (r UpdateResult) {
   178  	b, err := opts.ToLoadBalancerUpdateMap()
   179  	if err != nil {
   180  		r.Err = err
   181  		return
   182  	}
   183  	_, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
   184  		OkCodes: []int{200, 202},
   185  	})
   186  	return
   187  }
   188  
   189  // Delete will permanently delete a particular LoadBalancer based on its
   190  // 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  // CascadingDelete is like `Delete`, but will also delete any of the load balancer's
   197  // children (listener, monitor, etc).
   198  // NOTE: This function will only work with Octavia load balancers; Neutron does not
   199  // support this.
   200  func CascadingDelete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   201  	if c.Type != "load-balancer" {
   202  		r.Err = fmt.Errorf("error prior to running cascade delete: only Octavia LBs supported")
   203  		return
   204  	}
   205  	u := fmt.Sprintf("%s?cascade=true", resourceURL(c, id))
   206  	_, r.Err = c.Delete(u, nil)
   207  	return
   208  }
   209  
   210  // GetStatuses will return the status of a particular LoadBalancer.
   211  func GetStatuses(c *golangsdk.ServiceClient, id string) (r GetStatusesResult) {
   212  	_, r.Err = c.Get(statusRootURL(c, id), &r.Body, nil)
   213  	return
   214  }