Skip to content

Commit

Permalink
add support for creating,deleting,updating nodebalancers (configs and…
Browse files Browse the repository at this point in the history
… nodes)
  • Loading branch information
displague committed Jun 26, 2018
1 parent e3dfe12 commit b8b19d4
Show file tree
Hide file tree
Showing 4 changed files with 271 additions and 3 deletions.
5 changes: 4 additions & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ func moreExamples_authenticated() {
if err != nil {
log.Fatalln("* While creating instance: ", err)
}

linode, err = linodeClient.UpdateInstance(linode.ID, &linodego.InstanceUpdateOptions{Label: linode.Label + "-renamed"})
if err != nil {
log.Fatalln("* While renaming instance: ", err)
}
fmt.Println("## Created Instance\n", linode)
event, err := linodeClient.WaitForEventFinished(linode.ID, linodego.EntityLinode, linodego.ActionLinodeCreate, *linode.Created, 240)
if err != nil {
Expand Down
90 changes: 88 additions & 2 deletions nodebalancer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package linodego

import (
"encoding/json"
"fmt"
"time"

Expand All @@ -22,6 +23,16 @@ type NodeBalancer struct {
Updated *time.Time `json:"-"`
}

// NodeBalancerCreateOptions are the options permitted for CreateNodeBalancer
type NodeBalancerCreateOptions struct {
Label string `json:"label"`
Region string `json:"region"`
ClientConnThrottle int `json:"client_conn_throttle"`
}

// NodeBalancerUpdateOptions are the options permitted for UpdateNodeBalancer
type NodeBalancerUpdateOptions NodeBalancerCreateOptions

// NodeBalancersPagedResponse represents a paginated NodeBalancer API response
type NodeBalancersPagedResponse struct {
*PageOptions
Expand Down Expand Up @@ -54,13 +65,19 @@ func (c *Client) ListNodeBalancers(opts *ListOptions) ([]*NodeBalancer, error) {
return response.Data, nil
}

func (n *NodeBalancer) fixDates() *NodeBalancer {
n.Created, _ = parseDates(n.CreatedStr)
n.Updated, _ = parseDates(n.UpdatedStr)
return n
}

// GetNodeBalancer gets the NodeBalancer with the provided ID
func (c *Client) GetNodeBalancer(id string) (*NodeBalancer, error) {
func (c *Client) GetNodeBalancer(id int) (*NodeBalancer, error) {
e, err := c.NodeBalancers.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%s", e, id)
e = fmt.Sprintf("%s/%d", e, id)
r, err := c.R().
SetResult(&NodeBalancer{}).
Get(e)
Expand All @@ -69,3 +86,72 @@ func (c *Client) GetNodeBalancer(id string) (*NodeBalancer, error) {
}
return r.Result().(*NodeBalancer), nil
}

// CreateNodeBalancer creates a NodeBalancer
func (c *Client) CreateNodeBalancer(nodebalancer *NodeBalancerCreateOptions) (*NodeBalancer, error) {
var body string
e, err := c.NodeBalancers.Endpoint()
if err != nil {
return nil, err
}

req := c.R().SetResult(&NodeBalancer{})

if bodyData, err := json.Marshal(nodebalancer); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}

r, err := coupleAPIErrors(req.
SetHeader("Content-Type", "application/json").
SetBody(body).
Post(e))

if err != nil {
return nil, err
}
return r.Result().(*NodeBalancer).fixDates(), nil
}

// UpdateNodeBalancer updates the NodeBalancer with the specified id
func (c *Client) UpdateNodeBalancer(id int, updateOpts NodeBalancerUpdateOptions) (*NodeBalancer, error) {
var body string
e, err := c.NodeBalancers.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d", e, id)

req := c.R().SetResult(&NodeBalancer{})

if bodyData, err := json.Marshal(updateOpts); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}

r, err := coupleAPIErrors(req.
SetBody(body).
Put(e))

if err != nil {
return nil, err
}
return r.Result().(*NodeBalancer).fixDates(), nil
}

// DeleteNodeBalancer deletes the NodeBalancer with the specified id
func (c *Client) DeleteNodeBalancer(id int) error {
e, err := c.NodeBalancers.Endpoint()
if err != nil {
return err
}
e = fmt.Sprintf("%s/%d", e, id)

if _, err := coupleAPIErrors(c.R().Delete(e)); err != nil {
return err
}

return nil
}
86 changes: 86 additions & 0 deletions nodebalancer_config_nodes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package linodego

import (
"encoding/json"
"fmt"

"github.com/go-resty/resty"
Expand All @@ -17,6 +18,22 @@ type NodeBalancerNode struct {
NodeBalancerID int `json:"nodebalancer_id"`
}

type NodeBalancerNodeCreateOptions struct {
Address string `json:"address"`
Label string `json:"label"`
Weight int `json:"weight"`
Mode string `json:"mode"`
ConfigID int `json:"config_id"`
NodeBalancerID int `json:"nodebalancer_id"`
}

type NodeBalancerNodeUpdateOptions struct {
Address string `json:"address"`
Label string `json:"label"`
Weight int `json:"weight"`
Mode string `json:"mode"`
}

// NodeBalancerNodesPagedResponse represents a paginated NodeBalancerNode API response
type NodeBalancerNodesPagedResponse struct {
*PageOptions
Expand Down Expand Up @@ -73,3 +90,72 @@ func (c *Client) GetNodeBalancerNode(nodebalancerID int, configID int, nodeID in
}
return r.Result().(*NodeBalancerNode).fixDates(), nil
}

// CreateNodeBalancerNode creates a NodeBalancerNode
func (c *Client) CreateNodeBalancerNode(nodebalancerID int, configID int, createOpts *NodeBalancerNodeCreateOptions) (*NodeBalancerNode, error) {
var body string
e, err := c.NodeBalancerNodes.endpointWithID(nodebalancerID)
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d/nodes/", e, configID)

req := c.R().SetResult(&NodeBalancerNode{})

if bodyData, err := json.Marshal(createOpts); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}

r, err := coupleAPIErrors(req.
SetBody(body).
Post(e))

if err != nil {
return nil, err
}
return r.Result().(*NodeBalancerNode).fixDates(), nil
}

// UpdateNodeBalancerNode updates the NodeBalancerNode with the specified id
func (c *Client) UpdateNodeBalancerNode(id int, nodeId int, updateOpts NodeBalancerNodeUpdateOptions) (*NodeBalancerNode, error) {
var body string
e, err := c.NodeBalancerNodes.endpointWithID(id)
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/nodes/%d", e, nodeId)

req := c.R().SetResult(&NodeBalancerNode{})

if bodyData, err := json.Marshal(updateOpts); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}

r, err := coupleAPIErrors(req.
SetBody(body).
Put(e))

if err != nil {
return nil, err
}
return r.Result().(*NodeBalancerNode).fixDates(), nil
}

// DeleteNodeBalancerNode deletes the NodeBalancerNode with the specified id
func (c *Client) DeleteNodeBalancerNode(id int) error {
e, err := c.NodeBalancerNodes.Endpoint()
if err != nil {
return err
}
e = fmt.Sprintf("%s/%d", e, id)

if _, err := coupleAPIErrors(c.R().Delete(e)); err != nil {
return err
}

return nil
}
93 changes: 93 additions & 0 deletions nodebalancer_configs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package linodego

import (
"encoding/json"
"fmt"

"github.com/go-resty/resty"
Expand Down Expand Up @@ -32,6 +33,29 @@ type NodeBalancerNodeStatus struct {
Down int
}

// NodeBalancerConfigUpdateOptions are permitted by CreateNodeBalancerConfig
type NodeBalancerConfigCreateOptions struct {
Port int
Protocol string
Algorithm string
Stickiness string
Check string
CheckInterval int `json:"check_interval"`
CheckAttempts int `json:"check_attempts"`
CheckPath string `json:"check_path"`
CheckBody string `json:"check_body"`
CheckPassive bool `json:"check_passive"`
CipherSuite string `json:"cipher_suite"`
NodeBalancerID int `json:"nodebalancer_id"`
SSLCommonName string `json:"ssl_commonname"`
SSLFingerprint string `json:"ssl_fingerprint"`
SSLCert string `json:"ssl_cert"`
SSLKey string `json:"ssl_key"`
}

// NodeBalancerConfigUpdateOptions are permitted by UpdateNodeBalancerConfig
type NodeBalancerConfigUpdateOptions NodeBalancerConfigCreateOptions

// NodeBalancerConfigsPagedResponse represents a paginated NodeBalancerConfig API response
type NodeBalancerConfigsPagedResponse struct {
*PageOptions
Expand Down Expand Up @@ -88,3 +112,72 @@ func (c *Client) GetNodeBalancerConfig(nodebalancerID int, configID int) (*NodeB
}
return r.Result().(*NodeBalancerConfig).fixDates(), nil
}

// CreateNodeBalancerConfig creates a NodeBalancerConfig
func (c *Client) CreateNodeBalancerConfig(nodebalancerConfig *NodeBalancerConfigCreateOptions) (*NodeBalancerConfig, error) {
var body string
e, err := c.NodeBalancerConfigs.Endpoint()
if err != nil {
return nil, err
}

req := c.R().SetResult(&NodeBalancerConfig{})

if bodyData, err := json.Marshal(nodebalancerConfig); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}

r, err := coupleAPIErrors(req.
SetHeader("Content-Type", "application/json").
SetBody(body).
Post(e))

if err != nil {
return nil, err
}
return r.Result().(*NodeBalancerConfig).fixDates(), nil
}

// UpdateNodeBalancerConfig updates the NodeBalancerConfig with the specified id
func (c *Client) UpdateNodeBalancerConfig(id int, configId int, updateOpts NodeBalancerConfigUpdateOptions) (*NodeBalancerConfig, error) {
var body string
e, err := c.NodeBalancerConfigs.endpointWithID(id)
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/configs/%d", e, id, configId)

req := c.R().SetResult(&NodeBalancerConfig{})

if bodyData, err := json.Marshal(updateOpts); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}

r, err := coupleAPIErrors(req.
SetBody(body).
Put(e))

if err != nil {
return nil, err
}
return r.Result().(*NodeBalancerConfig).fixDates(), nil
}

// DeleteNodeBalancerConfig deletes the NodeBalancerConfig with the specified id
func (c *Client) DeleteNodeBalancerConfig(id int) error {
e, err := c.NodeBalancerConfigs.Endpoint()
if err != nil {
return err
}
e = fmt.Sprintf("%s/%d", e, id)

if _, err := coupleAPIErrors(c.R().Delete(e)); err != nil {
return err
}

return nil
}

0 comments on commit b8b19d4

Please sign in to comment.