2017-04-08 15:52:57 -04:00
|
|
|
package godo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2018-11-21 12:04:55 -05:00
|
|
|
"net/http"
|
2017-04-08 15:52:57 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const loadBalancersBasePath = "/v2/load_balancers"
|
|
|
|
const forwardingRulesPath = "forwarding_rules"
|
2018-11-21 12:04:55 -05:00
|
|
|
|
2017-04-08 15:52:57 -04:00
|
|
|
const dropletsPath = "droplets"
|
|
|
|
|
|
|
|
// LoadBalancersService is an interface for managing load balancers with the DigitalOcean API.
|
|
|
|
// See: https://developers.digitalocean.com/documentation/v2#load-balancers
|
|
|
|
type LoadBalancersService interface {
|
|
|
|
Get(context.Context, string) (*LoadBalancer, *Response, error)
|
|
|
|
List(context.Context, *ListOptions) ([]LoadBalancer, *Response, error)
|
|
|
|
Create(context.Context, *LoadBalancerRequest) (*LoadBalancer, *Response, error)
|
|
|
|
Update(ctx context.Context, lbID string, lbr *LoadBalancerRequest) (*LoadBalancer, *Response, error)
|
|
|
|
Delete(ctx context.Context, lbID string) (*Response, error)
|
|
|
|
AddDroplets(ctx context.Context, lbID string, dropletIDs ...int) (*Response, error)
|
|
|
|
RemoveDroplets(ctx context.Context, lbID string, dropletIDs ...int) (*Response, error)
|
|
|
|
AddForwardingRules(ctx context.Context, lbID string, rules ...ForwardingRule) (*Response, error)
|
|
|
|
RemoveForwardingRules(ctx context.Context, lbID string, rules ...ForwardingRule) (*Response, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadBalancer represents a DigitalOcean load balancer configuration.
|
2019-04-10 09:41:22 -04:00
|
|
|
// Tags can only be provided upon the creation of a Load Balancer.
|
2017-04-08 15:52:57 -04:00
|
|
|
type LoadBalancer struct {
|
|
|
|
ID string `json:"id,omitempty"`
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
IP string `json:"ip,omitempty"`
|
|
|
|
Algorithm string `json:"algorithm,omitempty"`
|
|
|
|
Status string `json:"status,omitempty"`
|
|
|
|
Created string `json:"created_at,omitempty"`
|
|
|
|
ForwardingRules []ForwardingRule `json:"forwarding_rules,omitempty"`
|
|
|
|
HealthCheck *HealthCheck `json:"health_check,omitempty"`
|
|
|
|
StickySessions *StickySessions `json:"sticky_sessions,omitempty"`
|
|
|
|
Region *Region `json:"region,omitempty"`
|
|
|
|
DropletIDs []int `json:"droplet_ids,omitempty"`
|
|
|
|
Tag string `json:"tag,omitempty"`
|
2019-04-10 09:41:22 -04:00
|
|
|
Tags []string `json:"tags,omitempty"`
|
2017-04-08 15:52:57 -04:00
|
|
|
RedirectHttpToHttps bool `json:"redirect_http_to_https,omitempty"`
|
2019-04-10 09:41:22 -04:00
|
|
|
EnableProxyProtocol bool `json:"enable_proxy_protocol,omitempty"`
|
|
|
|
VPCUUID string `json:"vpc_uuid,omitempty"`
|
2017-04-08 15:52:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// String creates a human-readable description of a LoadBalancer.
|
|
|
|
func (l LoadBalancer) String() string {
|
|
|
|
return Stringify(l)
|
|
|
|
}
|
|
|
|
|
2018-11-21 12:04:55 -05:00
|
|
|
func (l LoadBalancer) URN() string {
|
|
|
|
return ToURN("LoadBalancer", l.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AsRequest creates a LoadBalancerRequest that can be submitted to Update with the current values of the LoadBalancer.
|
|
|
|
// Modifying the returned LoadBalancerRequest will not modify the original LoadBalancer.
|
|
|
|
func (l LoadBalancer) AsRequest() *LoadBalancerRequest {
|
|
|
|
r := LoadBalancerRequest{
|
|
|
|
Name: l.Name,
|
|
|
|
Algorithm: l.Algorithm,
|
|
|
|
ForwardingRules: append([]ForwardingRule(nil), l.ForwardingRules...),
|
|
|
|
DropletIDs: append([]int(nil), l.DropletIDs...),
|
|
|
|
Tag: l.Tag,
|
|
|
|
RedirectHttpToHttps: l.RedirectHttpToHttps,
|
2019-04-10 09:41:22 -04:00
|
|
|
EnableProxyProtocol: l.EnableProxyProtocol,
|
2018-11-21 12:04:55 -05:00
|
|
|
HealthCheck: l.HealthCheck,
|
2019-04-10 09:41:22 -04:00
|
|
|
VPCUUID: l.VPCUUID,
|
2018-11-21 12:04:55 -05:00
|
|
|
}
|
2019-04-10 09:41:22 -04:00
|
|
|
|
2018-11-21 12:04:55 -05:00
|
|
|
if l.HealthCheck != nil {
|
|
|
|
r.HealthCheck = &HealthCheck{}
|
|
|
|
*r.HealthCheck = *l.HealthCheck
|
|
|
|
}
|
|
|
|
if l.StickySessions != nil {
|
|
|
|
r.StickySessions = &StickySessions{}
|
|
|
|
*r.StickySessions = *l.StickySessions
|
|
|
|
}
|
|
|
|
if l.Region != nil {
|
|
|
|
r.Region = l.Region.Slug
|
|
|
|
}
|
|
|
|
return &r
|
|
|
|
}
|
|
|
|
|
2017-04-08 15:52:57 -04:00
|
|
|
// ForwardingRule represents load balancer forwarding rules.
|
|
|
|
type ForwardingRule struct {
|
|
|
|
EntryProtocol string `json:"entry_protocol,omitempty"`
|
|
|
|
EntryPort int `json:"entry_port,omitempty"`
|
|
|
|
TargetProtocol string `json:"target_protocol,omitempty"`
|
|
|
|
TargetPort int `json:"target_port,omitempty"`
|
|
|
|
CertificateID string `json:"certificate_id,omitempty"`
|
|
|
|
TlsPassthrough bool `json:"tls_passthrough,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// String creates a human-readable description of a ForwardingRule.
|
|
|
|
func (f ForwardingRule) String() string {
|
|
|
|
return Stringify(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HealthCheck represents optional load balancer health check rules.
|
|
|
|
type HealthCheck struct {
|
|
|
|
Protocol string `json:"protocol,omitempty"`
|
|
|
|
Port int `json:"port,omitempty"`
|
|
|
|
Path string `json:"path,omitempty"`
|
|
|
|
CheckIntervalSeconds int `json:"check_interval_seconds,omitempty"`
|
|
|
|
ResponseTimeoutSeconds int `json:"response_timeout_seconds,omitempty"`
|
|
|
|
HealthyThreshold int `json:"healthy_threshold,omitempty"`
|
|
|
|
UnhealthyThreshold int `json:"unhealthy_threshold,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// String creates a human-readable description of a HealthCheck.
|
|
|
|
func (h HealthCheck) String() string {
|
|
|
|
return Stringify(h)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StickySessions represents optional load balancer session affinity rules.
|
|
|
|
type StickySessions struct {
|
|
|
|
Type string `json:"type,omitempty"`
|
|
|
|
CookieName string `json:"cookie_name,omitempty"`
|
|
|
|
CookieTtlSeconds int `json:"cookie_ttl_seconds,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// String creates a human-readable description of a StickySessions instance.
|
|
|
|
func (s StickySessions) String() string {
|
|
|
|
return Stringify(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadBalancerRequest represents the configuration to be applied to an existing or a new load balancer.
|
|
|
|
type LoadBalancerRequest struct {
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Algorithm string `json:"algorithm,omitempty"`
|
|
|
|
Region string `json:"region,omitempty"`
|
|
|
|
ForwardingRules []ForwardingRule `json:"forwarding_rules,omitempty"`
|
|
|
|
HealthCheck *HealthCheck `json:"health_check,omitempty"`
|
|
|
|
StickySessions *StickySessions `json:"sticky_sessions,omitempty"`
|
|
|
|
DropletIDs []int `json:"droplet_ids,omitempty"`
|
|
|
|
Tag string `json:"tag,omitempty"`
|
2019-04-10 09:41:22 -04:00
|
|
|
Tags []string `json:"tags,omitempty"`
|
2017-04-08 15:52:57 -04:00
|
|
|
RedirectHttpToHttps bool `json:"redirect_http_to_https,omitempty"`
|
2019-04-10 09:41:22 -04:00
|
|
|
EnableProxyProtocol bool `json:"enable_proxy_protocol,omitempty"`
|
|
|
|
VPCUUID string `json:"vpc_uuid,omitempty"`
|
2017-04-08 15:52:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// String creates a human-readable description of a LoadBalancerRequest.
|
|
|
|
func (l LoadBalancerRequest) String() string {
|
|
|
|
return Stringify(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
type forwardingRulesRequest struct {
|
|
|
|
Rules []ForwardingRule `json:"forwarding_rules,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l forwardingRulesRequest) String() string {
|
|
|
|
return Stringify(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
type dropletIDsRequest struct {
|
|
|
|
IDs []int `json:"droplet_ids,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l dropletIDsRequest) String() string {
|
|
|
|
return Stringify(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
type loadBalancersRoot struct {
|
|
|
|
LoadBalancers []LoadBalancer `json:"load_balancers"`
|
|
|
|
Links *Links `json:"links"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type loadBalancerRoot struct {
|
|
|
|
LoadBalancer *LoadBalancer `json:"load_balancer"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadBalancersServiceOp handles communication with load balancer-related methods of the DigitalOcean API.
|
|
|
|
type LoadBalancersServiceOp struct {
|
|
|
|
client *Client
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ LoadBalancersService = &LoadBalancersServiceOp{}
|
|
|
|
|
|
|
|
// Get an existing load balancer by its identifier.
|
|
|
|
func (l *LoadBalancersServiceOp) Get(ctx context.Context, lbID string) (*LoadBalancer, *Response, error) {
|
|
|
|
path := fmt.Sprintf("%s/%s", loadBalancersBasePath, lbID)
|
|
|
|
|
2018-11-21 12:04:55 -05:00
|
|
|
req, err := l.client.NewRequest(ctx, http.MethodGet, path, nil)
|
2017-04-08 15:52:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(loadBalancerRoot)
|
2018-11-21 12:04:55 -05:00
|
|
|
resp, err := l.client.Do(ctx, req, root)
|
2017-04-08 15:52:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return root.LoadBalancer, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// List load balancers, with optional pagination.
|
|
|
|
func (l *LoadBalancersServiceOp) List(ctx context.Context, opt *ListOptions) ([]LoadBalancer, *Response, error) {
|
|
|
|
path, err := addOptions(loadBalancersBasePath, opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-21 12:04:55 -05:00
|
|
|
req, err := l.client.NewRequest(ctx, http.MethodGet, path, nil)
|
2017-04-08 15:52:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(loadBalancersRoot)
|
2018-11-21 12:04:55 -05:00
|
|
|
resp, err := l.client.Do(ctx, req, root)
|
2017-04-08 15:52:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
if l := root.Links; l != nil {
|
|
|
|
resp.Links = l
|
|
|
|
}
|
|
|
|
|
|
|
|
return root.LoadBalancers, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new load balancer with a given configuration.
|
|
|
|
func (l *LoadBalancersServiceOp) Create(ctx context.Context, lbr *LoadBalancerRequest) (*LoadBalancer, *Response, error) {
|
2018-11-21 12:04:55 -05:00
|
|
|
req, err := l.client.NewRequest(ctx, http.MethodPost, loadBalancersBasePath, lbr)
|
2017-04-08 15:52:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(loadBalancerRoot)
|
2018-11-21 12:04:55 -05:00
|
|
|
resp, err := l.client.Do(ctx, req, root)
|
2017-04-08 15:52:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return root.LoadBalancer, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update an existing load balancer with new configuration.
|
|
|
|
func (l *LoadBalancersServiceOp) Update(ctx context.Context, lbID string, lbr *LoadBalancerRequest) (*LoadBalancer, *Response, error) {
|
|
|
|
path := fmt.Sprintf("%s/%s", loadBalancersBasePath, lbID)
|
|
|
|
|
|
|
|
req, err := l.client.NewRequest(ctx, "PUT", path, lbr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(loadBalancerRoot)
|
2018-11-21 12:04:55 -05:00
|
|
|
resp, err := l.client.Do(ctx, req, root)
|
2017-04-08 15:52:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return root.LoadBalancer, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete a load balancer by its identifier.
|
|
|
|
func (l *LoadBalancersServiceOp) Delete(ctx context.Context, ldID string) (*Response, error) {
|
|
|
|
path := fmt.Sprintf("%s/%s", loadBalancersBasePath, ldID)
|
|
|
|
|
2018-11-21 12:04:55 -05:00
|
|
|
req, err := l.client.NewRequest(ctx, http.MethodDelete, path, nil)
|
2017-04-08 15:52:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-21 12:04:55 -05:00
|
|
|
return l.client.Do(ctx, req, nil)
|
2017-04-08 15:52:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddDroplets adds droplets to a load balancer.
|
|
|
|
func (l *LoadBalancersServiceOp) AddDroplets(ctx context.Context, lbID string, dropletIDs ...int) (*Response, error) {
|
|
|
|
path := fmt.Sprintf("%s/%s/%s", loadBalancersBasePath, lbID, dropletsPath)
|
|
|
|
|
2018-11-21 12:04:55 -05:00
|
|
|
req, err := l.client.NewRequest(ctx, http.MethodPost, path, &dropletIDsRequest{IDs: dropletIDs})
|
2017-04-08 15:52:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-21 12:04:55 -05:00
|
|
|
return l.client.Do(ctx, req, nil)
|
2017-04-08 15:52:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveDroplets removes droplets from a load balancer.
|
|
|
|
func (l *LoadBalancersServiceOp) RemoveDroplets(ctx context.Context, lbID string, dropletIDs ...int) (*Response, error) {
|
|
|
|
path := fmt.Sprintf("%s/%s/%s", loadBalancersBasePath, lbID, dropletsPath)
|
|
|
|
|
2018-11-21 12:04:55 -05:00
|
|
|
req, err := l.client.NewRequest(ctx, http.MethodDelete, path, &dropletIDsRequest{IDs: dropletIDs})
|
2017-04-08 15:52:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-21 12:04:55 -05:00
|
|
|
return l.client.Do(ctx, req, nil)
|
2017-04-08 15:52:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddForwardingRules adds forwarding rules to a load balancer.
|
|
|
|
func (l *LoadBalancersServiceOp) AddForwardingRules(ctx context.Context, lbID string, rules ...ForwardingRule) (*Response, error) {
|
|
|
|
path := fmt.Sprintf("%s/%s/%s", loadBalancersBasePath, lbID, forwardingRulesPath)
|
|
|
|
|
2018-11-21 12:04:55 -05:00
|
|
|
req, err := l.client.NewRequest(ctx, http.MethodPost, path, &forwardingRulesRequest{Rules: rules})
|
2017-04-08 15:52:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-21 12:04:55 -05:00
|
|
|
return l.client.Do(ctx, req, nil)
|
2017-04-08 15:52:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveForwardingRules removes forwarding rules from a load balancer.
|
|
|
|
func (l *LoadBalancersServiceOp) RemoveForwardingRules(ctx context.Context, lbID string, rules ...ForwardingRule) (*Response, error) {
|
|
|
|
path := fmt.Sprintf("%s/%s/%s", loadBalancersBasePath, lbID, forwardingRulesPath)
|
|
|
|
|
2018-11-21 12:04:55 -05:00
|
|
|
req, err := l.client.NewRequest(ctx, http.MethodDelete, path, &forwardingRulesRequest{Rules: rules})
|
2017-04-08 15:52:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-21 12:04:55 -05:00
|
|
|
return l.client.Do(ctx, req, nil)
|
2017-04-08 15:52:57 -04:00
|
|
|
}
|