Skip to content

Commit

Permalink
Merge pull request #53 from ddymko/lb-ssl
Browse files Browse the repository at this point in the history
Lb ssl
  • Loading branch information
ddymko committed Mar 10, 2020
2 parents 8b9e6de + 8271c02 commit 4d7aa75
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 4 deletions.
73 changes: 71 additions & 2 deletions load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ type LoadBalancerService interface {
CreateForwardingRule(ctx context.Context, ID int, rule *ForwardingRule) (*ForwardingRule, error)
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)
Create(ctx context.Context, region int, label string, genericInfo *GenericInfo, healthCheck *HealthCheck, rules []ForwardingRule, ssl *SSL) (*LoadBalancers, error)
UpdateGenericInfo(ctx context.Context, ID int, label string, genericInfo *GenericInfo) error
AddSSL(ctx context.Context, ID int, ssl *SSL) error
RemoveSSL(ctx context.Context, ID int) error
}

// LoadBalancerHandler handles interaction with the server methods for the Vultr API
Expand Down Expand Up @@ -98,6 +100,13 @@ type LBConfig struct {
InstanceList
}

// SSL represents valid SSL config
type SSL struct {
PrivateKey string `json:"ssl_private_key"`
Certificate string `json:"ssl_certificate"`
Chain string `json:"chain,omitempty"`
}

// List all load balancer subscriptions on the current account.
func (l *LoadBalancerHandler) List(ctx context.Context) ([]LoadBalancers, error) {
uri := "/v1/loadbalancer/list"
Expand Down Expand Up @@ -442,13 +451,17 @@ func (l *LoadBalancerHandler) HasSSL(ctx context.Context, ID int) (*struct{ SSLI
}

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

values := url.Values{
"DCID": {strconv.Itoa(region)},
}

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

// Check generic info struct
if genericInfo != nil {
if *genericInfo.SSLRedirect == true {
Expand Down Expand Up @@ -480,6 +493,15 @@ func (l *LoadBalancerHandler) Create(ctx context.Context, region int, genericInf
values.Add("forwarding_rules", string(t))
}

if ssl != nil {
values.Add("ssl_private_key", ssl.PrivateKey)
values.Add("ssl_certificate", ssl.Certificate)

if ssl.Chain != "" {
values.Add("ssl_chain", ssl.Chain)
}
}

req, err := l.client.NewRequest(ctx, http.MethodPost, uri, values)
if err != nil {
return nil, err
Expand Down Expand Up @@ -533,3 +555,50 @@ func (l *LoadBalancerHandler) UpdateGenericInfo(ctx context.Context, ID int, lab

return nil
}

// AddSSL will attach an SSL certificate to a given load balancer
func (l *LoadBalancerHandler) AddSSL(ctx context.Context, ID int, ssl *SSL) error {
uri := "/v1/loadbalancer/ssl_add"

values := url.Values{
"SUBID": {strconv.Itoa(ID)},
"ssl_private_key": {ssl.PrivateKey},
"ssl_certificate": {ssl.Certificate},
}

if ssl.Chain != "" {
values.Add("ssl_chain", ssl.Chain)
}

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
}

// RemoveSSL will remove an SSL certificate from a load balancer
func (l *LoadBalancerHandler) RemoveSSL(ctx context.Context, ID int) error {
uri := "/v1/loadbalancer/ssl_remove"

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

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
}
45 changes: 43 additions & 2 deletions load_balancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,14 +377,20 @@ func TestLoadBalancerHandler_Create(t *testing.T) {

rules := []ForwardingRule{
{
FrontendProtocol: "http",
FrontendProtocol: "https",
FrontendPort: 80,
BackendProtocol: "http",
BackendPort: 80,
},
}

lb, err := client.LoadBalancer.Create(ctx, 1, &info, &health, rules)
ssl := SSL{
PrivateKey: "key",
Certificate: "cert",
Chain: "chain",
}

lb, err := client.LoadBalancer.Create(ctx, 1, "label", &info, &health, rules, &ssl)
if err != nil {
t.Errorf("LoadBalancer.Create returned %+v", err)
}
Expand Down Expand Up @@ -421,3 +427,38 @@ func TestLoadBalancerHandler_UpdateGenericInfo(t *testing.T) {
t.Errorf("LoadBalancer.UpdateGenericInfo returned %+v", err)
}
}

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

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

ssl := &SSL{
PrivateKey: "key",
Certificate: "crt",
Chain: "chain",
}
err := client.LoadBalancer.AddSSL(ctx, 12345, ssl)

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

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

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

err := client.LoadBalancer.RemoveSSL(ctx, 12345)

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

0 comments on commit 4d7aa75

Please sign in to comment.