Skip to content

Commit

Permalink
Add session_affinity_attributes field to LoadBalancer (cloudflare#470)
Browse files Browse the repository at this point in the history
* Add session_affinity_attributes field to LoadBalancer

session_affinity_attributes is a new field to set the samesite and
secure attributes in a load balancer session affinity cookie. Also fixed
broken links in load balancer API reference.

* s/SessionAffinityAttrs/SessionAffinityAttributes

Co-authored-by: Patryk Szczygłowski <[email protected]>
  • Loading branch information
tingwai and patryk authored May 28, 2020
1 parent 65ec18a commit e158cab
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 29 deletions.
53 changes: 30 additions & 23 deletions load_balancing.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,21 @@ type LoadBalancerMonitor struct {

// LoadBalancer represents a load balancer's properties.
type LoadBalancer struct {
ID string `json:"id,omitempty"`
CreatedOn *time.Time `json:"created_on,omitempty"`
ModifiedOn *time.Time `json:"modified_on,omitempty"`
Description string `json:"description"`
Name string `json:"name"`
TTL int `json:"ttl,omitempty"`
FallbackPool string `json:"fallback_pool"`
DefaultPools []string `json:"default_pools"`
RegionPools map[string][]string `json:"region_pools"`
PopPools map[string][]string `json:"pop_pools"`
Proxied bool `json:"proxied"`
Enabled *bool `json:"enabled,omitempty"`
Persistence string `json:"session_affinity,omitempty"`
PersistenceTTL int `json:"session_affinity_ttl,omitempty"`
ID string `json:"id,omitempty"`
CreatedOn *time.Time `json:"created_on,omitempty"`
ModifiedOn *time.Time `json:"modified_on,omitempty"`
Description string `json:"description"`
Name string `json:"name"`
TTL int `json:"ttl,omitempty"`
FallbackPool string `json:"fallback_pool"`
DefaultPools []string `json:"default_pools"`
RegionPools map[string][]string `json:"region_pools"`
PopPools map[string][]string `json:"pop_pools"`
Proxied bool `json:"proxied"`
Enabled *bool `json:"enabled,omitempty"`
Persistence string `json:"session_affinity,omitempty"`
PersistenceTTL int `json:"session_affinity_ttl,omitempty"`
SessionAffinityAttributes *SessionAffinityAttributes `json:"session_affinity_attributes,omitempty"`

// SteeringPolicy controls pool selection logic.
// "off" select pools in DefaultPools order
Expand All @@ -80,6 +81,12 @@ type LoadBalancer struct {
SteeringPolicy string `json:"steering_policy,omitempty"`
}

// SessionAffinityAttributes represents the fields used to set attributes in a load balancer session affinity cookie.
type SessionAffinityAttributes struct {
SameSite string `json:"samesite,omitempty"`
Secure string `json:"secure,omitempty"`
}

// LoadBalancerOriginHealth represents the health of the origin.
type LoadBalancerOriginHealth struct {
Healthy bool `json:"healthy,omitempty"`
Expand Down Expand Up @@ -147,7 +154,7 @@ type loadBalancerPoolHealthResponse struct {

// CreateLoadBalancerPool creates a new load balancer pool.
//
// API reference: https://api.cloudflare.com/#load-balancer-pools-create-a-pool
// API reference: https://api.cloudflare.com/#load-balancer-pools-create-pool
func (api *API) CreateLoadBalancerPool(pool LoadBalancerPool) (LoadBalancerPool, error) {
uri := api.userBaseURL("/user") + "/load_balancers/pools"
res, err := api.makeRequest("POST", uri, pool)
Expand Down Expand Up @@ -195,7 +202,7 @@ func (api *API) LoadBalancerPoolDetails(poolID string) (LoadBalancerPool, error)

// DeleteLoadBalancerPool disables and deletes a load balancer pool.
//
// API reference: https://api.cloudflare.com/#load-balancer-pools-delete-a-pool
// API reference: https://api.cloudflare.com/#load-balancer-pools-delete-pool
func (api *API) DeleteLoadBalancerPool(poolID string) error {
uri := api.userBaseURL("/user") + "/load_balancers/pools/" + poolID
if _, err := api.makeRequest("DELETE", uri, nil); err != nil {
Expand All @@ -206,7 +213,7 @@ func (api *API) DeleteLoadBalancerPool(poolID string) error {

// ModifyLoadBalancerPool modifies a configured load balancer pool.
//
// API reference: https://api.cloudflare.com/#load-balancer-pools-modify-a-pool
// API reference: https://api.cloudflare.com/#load-balancer-pools-update-pool
func (api *API) ModifyLoadBalancerPool(pool LoadBalancerPool) (LoadBalancerPool, error) {
uri := api.userBaseURL("/user") + "/load_balancers/pools/" + pool.ID
res, err := api.makeRequest("PUT", uri, pool)
Expand All @@ -222,7 +229,7 @@ func (api *API) ModifyLoadBalancerPool(pool LoadBalancerPool) (LoadBalancerPool,

// CreateLoadBalancerMonitor creates a new load balancer monitor.
//
// API reference: https://api.cloudflare.com/#load-balancer-monitors-create-a-monitor
// API reference: https://api.cloudflare.com/#load-balancer-monitors-create-monitor
func (api *API) CreateLoadBalancerMonitor(monitor LoadBalancerMonitor) (LoadBalancerMonitor, error) {
uri := api.userBaseURL("/user") + "/load_balancers/monitors"
res, err := api.makeRequest("POST", uri, monitor)
Expand Down Expand Up @@ -270,7 +277,7 @@ func (api *API) LoadBalancerMonitorDetails(monitorID string) (LoadBalancerMonito

// DeleteLoadBalancerMonitor disables and deletes a load balancer monitor.
//
// API reference: https://api.cloudflare.com/#load-balancer-monitors-delete-a-monitor
// API reference: https://api.cloudflare.com/#load-balancer-monitors-delete-monitor
func (api *API) DeleteLoadBalancerMonitor(monitorID string) error {
uri := api.userBaseURL("/user") + "/load_balancers/monitors/" + monitorID
if _, err := api.makeRequest("DELETE", uri, nil); err != nil {
Expand All @@ -281,7 +288,7 @@ func (api *API) DeleteLoadBalancerMonitor(monitorID string) error {

// ModifyLoadBalancerMonitor modifies a configured load balancer monitor.
//
// API reference: https://api.cloudflare.com/#load-balancer-monitors-modify-a-monitor
// API reference: https://api.cloudflare.com/#load-balancer-monitors-update-monitor
func (api *API) ModifyLoadBalancerMonitor(monitor LoadBalancerMonitor) (LoadBalancerMonitor, error) {
uri := api.userBaseURL("/user") + "/load_balancers/monitors/" + monitor.ID
res, err := api.makeRequest("PUT", uri, monitor)
Expand All @@ -297,7 +304,7 @@ func (api *API) ModifyLoadBalancerMonitor(monitor LoadBalancerMonitor) (LoadBala

// CreateLoadBalancer creates a new load balancer.
//
// API reference: https://api.cloudflare.com/#load-balancers-create-a-load-balancer
// API reference: https://api.cloudflare.com/#load-balancers-create-load-balancer
func (api *API) CreateLoadBalancer(zoneID string, lb LoadBalancer) (LoadBalancer, error) {
uri := "/zones/" + zoneID + "/load_balancers"
res, err := api.makeRequest("POST", uri, lb)
Expand Down Expand Up @@ -345,7 +352,7 @@ func (api *API) LoadBalancerDetails(zoneID, lbID string) (LoadBalancer, error) {

// DeleteLoadBalancer disables and deletes a load balancer.
//
// API reference: https://api.cloudflare.com/#load-balancers-delete-a-load-balancer
// API reference: https://api.cloudflare.com/#load-balancers-delete-load-balancer
func (api *API) DeleteLoadBalancer(zoneID, lbID string) error {
uri := "/zones/" + zoneID + "/load_balancers/" + lbID
if _, err := api.makeRequest("DELETE", uri, nil); err != nil {
Expand All @@ -356,7 +363,7 @@ func (api *API) DeleteLoadBalancer(zoneID, lbID string) error {

// ModifyLoadBalancer modifies a configured load balancer.
//
// API reference: https://api.cloudflare.com/#load-balancers-modify-a-load-balancer
// API reference: https://api.cloudflare.com/#load-balancers-update-load-balancer
func (api *API) ModifyLoadBalancer(zoneID string, lb LoadBalancer) (LoadBalancer, error) {
uri := "/zones/" + zoneID + "/load_balancers/" + lb.ID
res, err := api.makeRequest("PUT", uri, lb)
Expand Down
44 changes: 38 additions & 6 deletions load_balancing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -820,8 +820,12 @@ func TestCreateLoadBalancer(t *testing.T) {
},
"proxied": true,
"session_affinity": "cookie",
"session_affinity_ttl": 5000
}`, string(b))
"session_affinity_ttl": 5000,
"session_affinity_attributes": {
"samesite": "Strict",
"secure": "Always"
}
}`, string(b))
}
fmt.Fprint(w, `{
"success": true,
Expand Down Expand Up @@ -864,7 +868,11 @@ func TestCreateLoadBalancer(t *testing.T) {
},
"proxied": true,
"session_affinity": "cookie",
"session_affinity_ttl": 5000
"session_affinity_ttl": 5000,
"session_affinity_attributes": {
"samesite": "Strict",
"secure": "Always"
}
}
}`)
}
Expand Down Expand Up @@ -910,6 +918,10 @@ func TestCreateLoadBalancer(t *testing.T) {
Proxied: true,
Persistence: "cookie",
PersistenceTTL: 5000,
SessionAffinityAttributes: &SessionAffinityAttributes{
SameSite: "Strict",
Secure: "Always",
},
}
request := LoadBalancer{
Description: "Load Balancer for www.example.com",
Expand Down Expand Up @@ -946,6 +958,10 @@ func TestCreateLoadBalancer(t *testing.T) {
Proxied: true,
Persistence: "cookie",
PersistenceTTL: 5000,
SessionAffinityAttributes: &SessionAffinityAttributes{
SameSite: "Strict",
Secure: "Always",
},
}

actual, err := client.CreateLoadBalancer("199d98642c564d2e855e9661899b7252", request)
Expand Down Expand Up @@ -1224,8 +1240,12 @@ func TestModifyLoadBalancer(t *testing.T) {
]
},
"proxied": true,
"session_affinity": "none"
}`, string(b))
"session_affinity": "none",
"session_affinity_attributes": {
"samesite": "Strict",
"secure": "Always"
}
}`, string(b))
}
fmt.Fprint(w, `{
"success": true,
Expand Down Expand Up @@ -1262,7 +1282,11 @@ func TestModifyLoadBalancer(t *testing.T) {
]
},
"proxied": true,
"session_affinity": "none"
"session_affinity": "none",
"session_affinity_attributes": {
"samesite": "Strict",
"secure": "Always"
}
}
}`)
}
Expand Down Expand Up @@ -1302,6 +1326,10 @@ func TestModifyLoadBalancer(t *testing.T) {
},
Proxied: true,
Persistence: "none",
SessionAffinityAttributes: &SessionAffinityAttributes{
SameSite: "Strict",
Secure: "Always",
},
}
request := LoadBalancer{
ID: "699d98642c564d2e855e9661899b7252",
Expand Down Expand Up @@ -1333,6 +1361,10 @@ func TestModifyLoadBalancer(t *testing.T) {
},
Proxied: true,
Persistence: "none",
SessionAffinityAttributes: &SessionAffinityAttributes{
SameSite: "Strict",
Secure: "Always",
},
}

actual, err := client.ModifyLoadBalancer("199d98642c564d2e855e9661899b7252", request)
Expand Down

0 comments on commit e158cab

Please sign in to comment.