Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce glb settings in godo #674

Merged
merged 3 commits into from
Apr 2, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
90 changes: 89 additions & 1 deletion load_balancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

const (
cachePath = "cache"
dropletsPath = "droplets"
forwardingRulesPath = "forwarding_rules"
loadBalancersBasePath = "/v2/load_balancers"
Expand All @@ -31,6 +32,7 @@ type LoadBalancersService interface {
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)
PurgeCache(ctx context.Context, lbID string) (*Response, error)
}

// LoadBalancer represents a DigitalOcean load balancer configuration.
Expand Down Expand Up @@ -63,6 +65,9 @@ type LoadBalancer struct {
ProjectID string `json:"project_id,omitempty"`
HTTPIdleTimeoutSeconds *uint64 `json:"http_idle_timeout_seconds,omitempty"`
Firewall *LBFirewall `json:"firewall,omitempty"`
Domains []*LBDomain `json:"domains,omitempty"`
GLBSettings *GLBSettings `json:"glb_settings,omitempty"`
TargetLoadBalancerIDs []string `json:"target_load_balancer_ids,omitempty"`
}

// String creates a human-readable description of a LoadBalancer.
Expand Down Expand Up @@ -90,12 +95,12 @@ func (l LoadBalancer) AsRequest() *LoadBalancerRequest {
RedirectHttpToHttps: l.RedirectHttpToHttps,
EnableProxyProtocol: l.EnableProxyProtocol,
EnableBackendKeepalive: l.EnableBackendKeepalive,
HealthCheck: l.HealthCheck,
asaha2 marked this conversation as resolved.
Show resolved Hide resolved
VPCUUID: l.VPCUUID,
DisableLetsEncryptDNSRecords: l.DisableLetsEncryptDNSRecords,
ValidateOnly: l.ValidateOnly,
ProjectID: l.ProjectID,
HTTPIdleTimeoutSeconds: l.HTTPIdleTimeoutSeconds,
TargetLoadBalancerIDs: append([]string(nil), l.TargetLoadBalancerIDs...),
}

if l.DisableLetsEncryptDNSRecords != nil {
Expand All @@ -106,10 +111,12 @@ func (l LoadBalancer) AsRequest() *LoadBalancerRequest {
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
}
Expand All @@ -118,6 +125,16 @@ func (l LoadBalancer) AsRequest() *LoadBalancerRequest {
r.Firewall = l.Firewall.deepCopy()
}

for _, domain := range l.Domains {
lbDomain := &LBDomain{}
*lbDomain = *domain
r.Domains = append(r.Domains, lbDomain)
}

if l.GLBSettings != nil {
r.GLBSettings = l.GLBSettings.deepCopy()
}

return &r
}

Expand Down Expand Up @@ -216,6 +233,9 @@ type LoadBalancerRequest struct {
ProjectID string `json:"project_id,omitempty"`
HTTPIdleTimeoutSeconds *uint64 `json:"http_idle_timeout_seconds,omitempty"`
Firewall *LBFirewall `json:"firewall,omitempty"`
Domains []*LBDomain `json:"domains,omitempty"`
GLBSettings *GLBSettings `json:"glb_settings,omitempty"`
TargetLoadBalancerIDs []string `json:"target_load_balancer_ids,omitempty"`
}

// String creates a human-readable description of a LoadBalancerRequest.
Expand All @@ -239,6 +259,62 @@ func (l dropletIDsRequest) String() string {
return Stringify(l)
}

// LBDomain defines domain names required to ingress traffic to a Global LB
type LBDomain struct {
// Name defines the domain fqdn
Name string `json:"name"`
// IsManaged indicates if the domain is DO-managed
IsManaged bool `json:"is_managed"`
// CertificateID indicates ID of a TLS certificate
CertificateID string `json:"certificate_id,omitempty"`
// Status indicates the domain validation status
Status string `json:"status,omitempty"`
// ErrorReason indicates any domain validation error
ErrorReason string `json:"error_reason,omitempty"`
}

// String creates a human-readable description of a LBDomain
func (d LBDomain) String() string {
return Stringify(d)
}

// GLBSettings define settings for configuring a Global LB
type GLBSettings struct {
// TargetProtocol is the outgoing traffic protocol.
TargetProtocol string `json:"target_protocol"`
// EntryPort is the outgoing traffic port.
TargetPort uint32 `json:"target_port"`
// CDNSettings is the CDN configurations
CDN *CDNSettings `json:"cdn"`
}

// String creates a human-readable description of a GLBSettings
func (s GLBSettings) String() string {
return Stringify(s)
}

func (s GLBSettings) deepCopy() *GLBSettings {
settings := &GLBSettings{
TargetProtocol: s.TargetProtocol,
TargetPort: s.TargetPort,
}
if s.CDN != nil {
settings.CDN = &CDNSettings{IsEnabled: s.CDN.IsEnabled}
}
return settings
}

// CDNSettings define CDN settings for a Global LB
type CDNSettings struct {
// IsEnabled is the caching enabled flag
IsEnabled bool `json:"is_enabled"`
}

// String creates a human-readable description of a CDNSettings
func (c CDNSettings) String() string {
return Stringify(c)
}

type loadBalancersRoot struct {
LoadBalancers []LoadBalancer `json:"load_balancers"`
Links *Links `json:"links"`
Expand Down Expand Up @@ -394,3 +470,15 @@ func (l *LoadBalancersServiceOp) RemoveForwardingRules(ctx context.Context, lbID

return l.client.Do(ctx, req, nil)
}

// PurgeCache purges the CDN cache of a global load balancer by its identifier.
func (l *LoadBalancersServiceOp) PurgeCache(ctx context.Context, ldID string) (*Response, error) {
path := fmt.Sprintf("%s/%s/%s", loadBalancersBasePath, ldID, cachePath)

req, err := l.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}

return l.client.Do(ctx, req, nil)
}