github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/lbaas_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  }
    40  
    41  // ToLoadBalancerListQuery formats a ListOpts into a query string.
    42  func (opts ListOpts) ToLoadBalancerListQuery() (string, error) {
    43  	q, err := golangsdk.BuildQueryString(opts)
    44  	return q.String(), err
    45  }
    46  
    47  // List returns a Pager which allows you to iterate over a collection of
    48  // load balancers. It accepts a ListOpts struct, which allows you to filter
    49  // and sort the returned collection for greater efficiency.
    50  //
    51  // Default policy settings return only those load balancers that are owned by
    52  // the tenant who submits the request, unless an admin user submits the request.
    53  func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    54  	url := rootURL(c)
    55  	if opts != nil {
    56  		query, err := opts.ToLoadBalancerListQuery()
    57  		if err != nil {
    58  			return pagination.Pager{Err: err}
    59  		}
    60  		url += query
    61  	}
    62  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    63  		return LoadBalancerPage{pagination.LinkedPageBase{PageResult: r}}
    64  	})
    65  }
    66  
    67  // CreateOptsBuilder allows extensions to add additional parameters to the
    68  // Create request.
    69  type CreateOptsBuilder interface {
    70  	ToLoadBalancerCreateMap() (map[string]interface{}, error)
    71  }
    72  
    73  // CreateOpts is the common options struct used in this package's Create
    74  // operation.
    75  type CreateOpts struct {
    76  	// Human-readable name for the Loadbalancer. Does not have to be unique.
    77  	Name string `json:"name,omitempty"`
    78  
    79  	// Human-readable description for the Loadbalancer.
    80  	Description string `json:"description,omitempty"`
    81  
    82  	// The network on which to allocate the Loadbalancer's address. A tenant can
    83  	// only create Loadbalancers on networks authorized by policy (e.g. networks
    84  	// that belong to them or networks that are shared).
    85  	VipSubnetID string `json:"vip_subnet_id" required:"true"`
    86  
    87  	// TenantID is the UUID of the project who owns the Loadbalancer.
    88  	// Only administrative users can specify a project UUID other than their own.
    89  	TenantID string `json:"tenant_id,omitempty"`
    90  
    91  	// ProjectID is the UUID of the project who owns the Loadbalancer.
    92  	// Only administrative users can specify a project UUID other than their own.
    93  	ProjectID string `json:"project_id,omitempty"`
    94  
    95  	// The IP address of the Loadbalancer.
    96  	VipAddress string `json:"vip_address,omitempty"`
    97  
    98  	// The administrative state of the Loadbalancer. A valid value is true (UP)
    99  	// or false (DOWN).
   100  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   101  
   102  	// The UUID of a flavor.
   103  	Flavor string `json:"flavor,omitempty"`
   104  
   105  	// The name of the provider.
   106  	Provider string `json:"provider,omitempty"`
   107  }
   108  
   109  // ToLoadBalancerCreateMap builds a request body from CreateOpts.
   110  func (opts CreateOpts) ToLoadBalancerCreateMap() (map[string]interface{}, error) {
   111  	return golangsdk.BuildRequestBody(opts, "loadbalancer")
   112  }
   113  
   114  // Create is an operation which provisions a new loadbalancer based on the
   115  // configuration defined in the CreateOpts struct. Once the request is
   116  // validated and progress has started on the provisioning process, a
   117  // CreateResult will be returned.
   118  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   119  	b, err := opts.ToLoadBalancerCreateMap()
   120  	if err != nil {
   121  		r.Err = err
   122  		return
   123  	}
   124  	_, r.Err = c.Post(rootURL(c), b, &r.Body, nil)
   125  	return
   126  }
   127  
   128  // Get retrieves a particular Loadbalancer based on its unique ID.
   129  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   130  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
   131  	return
   132  }
   133  
   134  // UpdateOptsBuilder allows extensions to add additional parameters to the
   135  // Update request.
   136  type UpdateOptsBuilder interface {
   137  	ToLoadBalancerUpdateMap() (map[string]interface{}, error)
   138  }
   139  
   140  // UpdateOpts is the common options struct used in this package's Update
   141  // operation.
   142  type UpdateOpts struct {
   143  	// Human-readable name for the Loadbalancer. Does not have to be unique.
   144  	Name string `json:"name,omitempty"`
   145  
   146  	// Human-readable description for the Loadbalancer.
   147  	Description string `json:"description,omitempty"`
   148  
   149  	// The administrative state of the Loadbalancer. A valid value is true (UP)
   150  	// or false (DOWN).
   151  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   152  }
   153  
   154  // ToLoadBalancerUpdateMap builds a request body from UpdateOpts.
   155  func (opts UpdateOpts) ToLoadBalancerUpdateMap() (map[string]interface{}, error) {
   156  	return golangsdk.BuildRequestBody(opts, "loadbalancer")
   157  }
   158  
   159  // Update is an operation which modifies the attributes of the specified
   160  // LoadBalancer.
   161  func Update(c *golangsdk.ServiceClient, id string, opts UpdateOpts) (r UpdateResult) {
   162  	b, err := opts.ToLoadBalancerUpdateMap()
   163  	if err != nil {
   164  		r.Err = err
   165  		return
   166  	}
   167  	_, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
   168  		OkCodes: []int{200, 202},
   169  	})
   170  	return
   171  }
   172  
   173  // Delete will permanently delete a particular LoadBalancer based on its
   174  // unique ID.
   175  func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   176  	_, r.Err = c.Delete(resourceURL(c, id), nil)
   177  	return
   178  }
   179  
   180  // CascadingDelete is like `Delete`, but will also delete any of the load balancer's
   181  // children (listener, monitor, etc).
   182  // NOTE: This function will only work with Octavia load balancers; Neutron does not
   183  // support this.
   184  func CascadingDelete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   185  	if c.Type != "load-balancer" {
   186  		r.Err = fmt.Errorf("error prior to running cascade delete: only Octavia LBs supported")
   187  		return
   188  	}
   189  	u := fmt.Sprintf("%s?cascade=true", resourceURL(c, id))
   190  	_, r.Err = c.Delete(u, nil)
   191  	return
   192  }
   193  
   194  // GetStatuses will return the status of a particular LoadBalancer.
   195  func GetStatuses(c *golangsdk.ServiceClient, id string) (r GetStatusesResult) {
   196  	_, r.Err = c.Get(statusRootURL(c, id), &r.Body, nil)
   197  	return
   198  }