github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/elb/v2/loadbalancers/requests.go (about)

     1  package loadbalancers
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/huaweicloud/golangsdk"
     7  	"github.com/huaweicloud/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  
   113  // ToLoadBalancerCreateMap builds a request body from CreateOpts.
   114  func (opts CreateOpts) ToLoadBalancerCreateMap() (map[string]interface{}, error) {
   115  	return golangsdk.BuildRequestBody(opts, "loadbalancer")
   116  }
   117  
   118  // Create is an operation which provisions a new loadbalancer based on the
   119  // configuration defined in the CreateOpts struct. Once the request is
   120  // validated and progress has started on the provisioning process, a
   121  // CreateResult will be returned.
   122  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   123  	b, err := opts.ToLoadBalancerCreateMap()
   124  	if err != nil {
   125  		r.Err = err
   126  		return
   127  	}
   128  	_, r.Err = c.Post(rootURL(c), b, &r.Body, nil)
   129  	return
   130  }
   131  
   132  // Get retrieves a particular Loadbalancer based on its unique ID.
   133  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   134  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
   135  	return
   136  }
   137  
   138  // UpdateOptsBuilder allows extensions to add additional parameters to the
   139  // Update request.
   140  type UpdateOptsBuilder interface {
   141  	ToLoadBalancerUpdateMap() (map[string]interface{}, error)
   142  }
   143  
   144  // UpdateOpts is the common options struct used in this package's Update
   145  // operation.
   146  type UpdateOpts struct {
   147  	// Human-readable name for the Loadbalancer. Does not have to be unique.
   148  	Name string `json:"name,omitempty"`
   149  
   150  	// Human-readable description for the Loadbalancer.
   151  	Description string `json:"description,omitempty"`
   152  
   153  	// The administrative state of the Loadbalancer. A valid value is true (UP)
   154  	// or false (DOWN).
   155  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   156  }
   157  
   158  // ToLoadBalancerUpdateMap builds a request body from UpdateOpts.
   159  func (opts UpdateOpts) ToLoadBalancerUpdateMap() (map[string]interface{}, error) {
   160  	return golangsdk.BuildRequestBody(opts, "loadbalancer")
   161  }
   162  
   163  // Update is an operation which modifies the attributes of the specified
   164  // LoadBalancer.
   165  func Update(c *golangsdk.ServiceClient, id string, opts UpdateOpts) (r UpdateResult) {
   166  	b, err := opts.ToLoadBalancerUpdateMap()
   167  	if err != nil {
   168  		r.Err = err
   169  		return
   170  	}
   171  	_, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
   172  		OkCodes: []int{200, 202},
   173  	})
   174  	return
   175  }
   176  
   177  // Delete will permanently delete a particular LoadBalancer based on its
   178  // unique ID.
   179  func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   180  	_, r.Err = c.Delete(resourceURL(c, id), nil)
   181  	return
   182  }
   183  
   184  // CascadingDelete is like `Delete`, but will also delete any of the load balancer's
   185  // children (listener, monitor, etc).
   186  // NOTE: This function will only work with Octavia load balancers; Neutron does not
   187  // support this.
   188  func CascadingDelete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   189  	if c.Type != "load-balancer" {
   190  		r.Err = fmt.Errorf("error prior to running cascade delete: only Octavia LBs supported")
   191  		return
   192  	}
   193  	u := fmt.Sprintf("%s?cascade=true", resourceURL(c, id))
   194  	_, r.Err = c.Delete(u, nil)
   195  	return
   196  }
   197  
   198  // GetStatuses will return the status of a particular LoadBalancer.
   199  func GetStatuses(c *golangsdk.ServiceClient, id string) (r GetStatusesResult) {
   200  	_, r.Err = c.Get(statusRootURL(c, id), &r.Body, nil)
   201  	return
   202  }