github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/networking/v2/extensions/lbaas_v2/loadbalancers/requests.go (about)

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