github.com/gophercloud/gophercloud@v1.11.0/openstack/loadbalancer/v2/loadbalancers/requests.go (about)

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