Skip to content

Commit

Permalink
Merge pull request #43 from ddymko/udpate-info
Browse files Browse the repository at this point in the history
Update Generic Info
  • Loading branch information
ddymko authored Feb 20, 2020
2 parents 3b44a32 + c878174 commit 94af024
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 deletions.
46 changes: 44 additions & 2 deletions load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type LoadBalancerService interface {
GetFullConfig(ctx context.Context, ID int) (*LBConfig, error)
HasSSL(ctx context.Context, ID int) (*struct{ SSLInfo bool `json:"has_ssl"` }, error)
Create(ctx context.Context, region int, genericInfo *GenericInfo, healthCheck *HealthCheck, rules []ForwardingRule) (*LoadBalancers, error)
UpdateGenericInfo(ctx context.Context, ID int, label string, genericInfo *GenericInfo) error
}

// LoadBalancerHandler handles interaction with the server methods for the Vultr API
Expand Down Expand Up @@ -64,7 +65,7 @@ type HealthCheck struct {
// GenericInfo represents generic configuration of your load balancer
type GenericInfo struct {
BalancingAlgorithm string `json:"balancing_algorithm"`
SSLRedirect bool `json:"ssl_redirect"`
SSLRedirect *bool `json:"ssl_redirect,omitempty"`
StickySessions *StickySessions `json:"sticky_sessions"`
}

Expand Down Expand Up @@ -440,6 +441,7 @@ func (l *LoadBalancerHandler) HasSSL(ctx context.Context, ID int) (*struct{ SSLI
return ssl, nil
}

// Create a load balancer
func (l *LoadBalancerHandler) Create(ctx context.Context, region int, genericInfo *GenericInfo, healthCheck *HealthCheck, rules []ForwardingRule) (*LoadBalancers, error) {
uri := "/v1/loadbalancer/create"

Expand All @@ -449,7 +451,7 @@ func (l *LoadBalancerHandler) Create(ctx context.Context, region int, genericInf

// Check generic info struct
if genericInfo != nil {
if genericInfo.SSLRedirect == true {
if *genericInfo.SSLRedirect == true {
values.Add("config_ssl_redirect", "true")
}

Expand Down Expand Up @@ -491,3 +493,43 @@ func (l *LoadBalancerHandler) Create(ctx context.Context, region int, genericInf

return &lb, nil
}

// UpdateGenericInfo will update portions of your generic info section
func (l *LoadBalancerHandler) UpdateGenericInfo(ctx context.Context, ID int, label string, genericInfo *GenericInfo) error {
uri := "/v1/loadbalancer/generic_update"

values := url.Values{
"SUBID": {strconv.Itoa(ID)},
}

if label != "" {
values.Add("label", label)
}

if genericInfo != nil {
if genericInfo.StickySessions != nil {
values.Add("sticky_sessions", genericInfo.StickySessions.StickySessionsEnabled)
values.Add("cookie_name", genericInfo.StickySessions.CookieName)
}

if genericInfo.SSLRedirect != nil {
values.Add("ssl_redirect", strconv.FormatBool(*genericInfo.SSLRedirect))
}

if genericInfo.BalancingAlgorithm != "" {
values.Add("balancing_algorithm", genericInfo.BalancingAlgorithm)
}
}

req, err := l.client.NewRequest(ctx, http.MethodPost, uri, values)
if err != nil {
return err
}

err = l.client.DoWithContext(ctx, req, nil)
if err != nil {
return err
}

return nil
}
33 changes: 30 additions & 3 deletions load_balancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,10 @@ func TestLoadBalancerHandler_GetGenericInfo(t *testing.T) {
t.Errorf("LoadBalancer.GetGenericInfo returned %+v", err)
}

redirect := false
expected := &GenericInfo{
BalancingAlgorithm: "roundrobin",
SSLRedirect: false,
SSLRedirect: &redirect,
StickySessions: &StickySessions{
CookieName: "test",
},
Expand Down Expand Up @@ -290,10 +291,11 @@ func TestLoadBalancerHandler_GetFullConfig(t *testing.T) {
t.Errorf("LoadBalancer.GetFullConfig returned %+v", err)
}

redirect := true
expected := &LBConfig{
GenericInfo: GenericInfo{
BalancingAlgorithm: "roundrobin",
SSLRedirect: true,
SSLRedirect: &redirect,
StickySessions: &StickySessions{CookieName: "cookiename"},
},
HealthCheck: HealthCheck{
Expand Down Expand Up @@ -353,9 +355,10 @@ func TestLoadBalancerHandler_Create(t *testing.T) {
fmt.Fprintf(writer, response)
})

redirect := true
info := GenericInfo{
BalancingAlgorithm: "roundrobin",
SSLRedirect: true,
SSLRedirect: &redirect,
StickySessions: &StickySessions{
StickySessionsEnabled: "on",
CookieName: "cookie",
Expand Down Expand Up @@ -394,3 +397,27 @@ func TestLoadBalancerHandler_Create(t *testing.T) {
t.Errorf("LoadBalancer.Create returned %+v, expected %+v", lb, expected)
}
}

func TestLoadBalancerHandler_UpdateGenericInfo(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/v1/loadbalancer/generic_update", func(writer http.ResponseWriter, request *http.Request) {
fmt.Fprint(writer)
})

redirect := true
info := GenericInfo{
BalancingAlgorithm: "roundrobin",
SSLRedirect: &redirect,
StickySessions: &StickySessions{
StickySessionsEnabled: "on",
CookieName: "cookie_name",
},
}
err := client.LoadBalancer.UpdateGenericInfo(ctx, 12345, "label", &info)

if err != nil {
t.Errorf("LoadBalancer.UpdateGenericInfo returned %+v", err)
}
}

0 comments on commit 94af024

Please sign in to comment.