1 package linodego
2 3 import (
4 "context"
5 "encoding/json"
6 "time"
7 8 "github.com/linode/linodego/internal/parseabletime"
9 )
10 11 // NodeBalancer represents a NodeBalancer object
12 type NodeBalancer struct {
13 // This NodeBalancer's unique ID.
14 ID int `json:"id"`
15 // This NodeBalancer's label. These must be unique on your Account.
16 Label *string `json:"label"`
17 // The Region where this NodeBalancer is located. NodeBalancers only support backends in the same Region.
18 Region string `json:"region"`
19 // This NodeBalancer's hostname, ending with .nodebalancer.linode.com
20 Hostname *string `json:"hostname"`
21 // This NodeBalancer's public IPv4 address.
22 IPv4 *string `json:"ipv4"`
23 // This NodeBalancer's public IPv6 address.
24 IPv6 *string `json:"ipv6"`
25 // Throttle connections per second (0-20). Set to 0 (zero) to disable throttling.
26 ClientConnThrottle int `json:"client_conn_throttle"`
27 28 // ClientUDPSessThrottle throttles UDP sessions per second. Set to 0 (zero) to disable throttling.
29 // NOTE: ClientUDPSessThrottle may not currently be available to all users.
30 ClientUDPSessThrottle int `json:"client_udp_sess_throttle"`
31 32 // Information about the amount of transfer this NodeBalancer has had so far this month.
33 Transfer NodeBalancerTransfer `json:"transfer"`
34 // This NodeBalancer's plan Type
35 Type NodeBalancerPlanType `json:"type"`
36 37 // An array of tags applied to this object. Tags are for organizational purposes only.
38 Tags []string `json:"tags"`
39 40 Created *time.Time `json:"-"`
41 Updated *time.Time `json:"-"`
42 }
43 44 // NodeBalancerTransfer contains information about the amount of transfer a NodeBalancer has had in the current month
45 type NodeBalancerTransfer struct {
46 // The total transfer, in MB, used by this NodeBalancer this month.
47 Total *float64 `json:"total"`
48 // The total inbound transfer, in MB, used for this NodeBalancer this month.
49 Out *float64 `json:"out"`
50 // The total outbound transfer, in MB, used for this NodeBalancer this month.
51 In *float64 `json:"in"`
52 }
53 54 type NodeBalancerVPCOptions struct {
55 IPv4Range string `json:"ipv4_range,omitempty"`
56 IPv6Range string `json:"ipv6_range,omitempty"`
57 SubnetID int `json:"subnet_id"`
58 IPv4RangeAutoAssign bool `json:"ipv4_range_auto_assign,omitempty"`
59 }
60 61 // NodeBalancerCreateOptions are the options permitted for CreateNodeBalancer
62 type NodeBalancerCreateOptions struct {
63 Label *string `json:"label,omitempty"`
64 Region string `json:"region,omitempty"`
65 ClientConnThrottle *int `json:"client_conn_throttle,omitempty"`
66 67 // NOTE: ClientUDPSessThrottle may not currently be available to all users.
68 ClientUDPSessThrottle *int `json:"client_udp_sess_throttle,omitempty"`
69 70 Configs []*NodeBalancerConfigCreateOptions `json:"configs,omitempty"`
71 Tags []string `json:"tags"`
72 FirewallID int `json:"firewall_id,omitempty"`
73 Type NodeBalancerPlanType `json:"type,omitempty"`
74 VPCs []NodeBalancerVPCOptions `json:"vpcs,omitempty"`
75 IPv4 *string `json:"ipv4,omitempty"`
76 }
77 78 // NodeBalancerUpdateOptions are the options permitted for UpdateNodeBalancer
79 type NodeBalancerUpdateOptions struct {
80 Label *string `json:"label,omitempty"`
81 ClientConnThrottle *int `json:"client_conn_throttle,omitempty"`
82 83 // NOTE: ClientUDPSessThrottle may not currently be available to all users.
84 ClientUDPSessThrottle *int `json:"client_udp_sess_throttle,omitempty"`
85 86 Tags *[]string `json:"tags,omitempty"`
87 }
88 89 // NodeBalancerPlanType constants start with NBType and include Linode API NodeBalancer's plan types
90 type NodeBalancerPlanType string
91 92 // NodeBalancerPlanType constants reflect the plan type used by a NodeBalancer Config
93 const (
94 NBTypePremium NodeBalancerPlanType = "premium"
95 NBTypePremium40GB NodeBalancerPlanType = "premium_40gb"
96 NBTypeCommon NodeBalancerPlanType = "common"
97 )
98 99 // UnmarshalJSON implements the json.Unmarshaler interface
100 func (i *NodeBalancer) UnmarshalJSON(b []byte) error {
101 type Mask NodeBalancer
102 103 p := struct {
104 *Mask
105 106 Created *parseabletime.ParseableTime `json:"created"`
107 Updated *parseabletime.ParseableTime `json:"updated"`
108 }{
109 Mask: (*Mask)(i),
110 }
111 112 if err := json.Unmarshal(b, &p); err != nil {
113 return err
114 }
115 116 i.Created = (*time.Time)(p.Created)
117 i.Updated = (*time.Time)(p.Updated)
118 119 return nil
120 }
121 122 // GetCreateOptions converts a NodeBalancer to NodeBalancerCreateOptions for use in CreateNodeBalancer
123 func (i NodeBalancer) GetCreateOptions() NodeBalancerCreateOptions {
124 return NodeBalancerCreateOptions{
125 Label: i.Label,
126 Region: i.Region,
127 ClientConnThrottle: &i.ClientConnThrottle,
128 ClientUDPSessThrottle: &i.ClientUDPSessThrottle,
129 Type: i.Type,
130 Tags: i.Tags,
131 }
132 }
133 134 // GetUpdateOptions converts a NodeBalancer to NodeBalancerUpdateOptions for use in UpdateNodeBalancer
135 func (i NodeBalancer) GetUpdateOptions() NodeBalancerUpdateOptions {
136 return NodeBalancerUpdateOptions{
137 Label: i.Label,
138 ClientConnThrottle: &i.ClientConnThrottle,
139 ClientUDPSessThrottle: &i.ClientUDPSessThrottle,
140 Tags: &i.Tags,
141 }
142 }
143 144 // ListNodeBalancers lists NodeBalancers
145 func (c *Client) ListNodeBalancers(ctx context.Context, opts *ListOptions) ([]NodeBalancer, error) {
146 return getPaginatedResults[NodeBalancer](ctx, c, "nodebalancers", opts)
147 }
148 149 // GetNodeBalancer gets the NodeBalancer with the provided ID
150 func (c *Client) GetNodeBalancer(ctx context.Context, nodebalancerID int) (*NodeBalancer, error) {
151 e := formatAPIPath("nodebalancers/%d", nodebalancerID)
152 return doGETRequest[NodeBalancer](ctx, c, e)
153 }
154 155 // CreateNodeBalancer creates a NodeBalancer
156 func (c *Client) CreateNodeBalancer(ctx context.Context, opts NodeBalancerCreateOptions) (*NodeBalancer, error) {
157 return doPOSTRequest[NodeBalancer](ctx, c, "nodebalancers", opts)
158 }
159 160 // UpdateNodeBalancer updates the NodeBalancer with the specified id
161 func (c *Client) UpdateNodeBalancer(ctx context.Context, nodebalancerID int, opts NodeBalancerUpdateOptions) (*NodeBalancer, error) {
162 e := formatAPIPath("nodebalancers/%d", nodebalancerID)
163 return doPUTRequest[NodeBalancer](ctx, c, e, opts)
164 }
165 166 // DeleteNodeBalancer deletes the NodeBalancer with the specified id
167 func (c *Client) DeleteNodeBalancer(ctx context.Context, nodebalancerID int) error {
168 e := formatAPIPath("nodebalancers/%d", nodebalancerID)
169 return doDELETERequest(ctx, c, e)
170 }
171