1package linodego
2
3import (
4	"context"
5	"encoding/json"
6	"fmt"
7	"time"
8
9	"github.com/linode/linodego/internal/parseabletime"
10)
11
12// NodeBalancer represents a NodeBalancer object
13type NodeBalancer struct {
14	// This NodeBalancer's unique ID.
15	ID int `json:"id"`
16	// This NodeBalancer's label. These must be unique on your Account.
17	Label *string `json:"label"`
18	// The Region where this NodeBalancer is located. NodeBalancers only support backends in the same Region.
19	Region string `json:"region"`
20	// This NodeBalancer's hostname, ending with .nodebalancer.linode.com
21	Hostname *string `json:"hostname"`
22	// This NodeBalancer's public IPv4 address.
23	IPv4 *string `json:"ipv4"`
24	// This NodeBalancer's public IPv6 address.
25	IPv6 *string `json:"ipv6"`
26	// Throttle connections per second (0-20). Set to 0 (zero) to disable throttling.
27	ClientConnThrottle int `json:"client_conn_throttle"`
28	// Information about the amount of transfer this NodeBalancer has had so far this month.
29	Transfer NodeBalancerTransfer `json:"transfer"`
30
31	// An array of tags applied to this object. Tags are for organizational purposes only.
32	Tags []string `json:"tags"`
33
34	Created *time.Time `json:"-"`
35	Updated *time.Time `json:"-"`
36}
37
38// NodeBalancerTransfer contains information about the amount of transfer a NodeBalancer has had in the current month
39type NodeBalancerTransfer struct {
40	// The total transfer, in MB, used by this NodeBalancer this month.
41	Total *float64 `json:"total"`
42	// The total inbound transfer, in MB, used for this NodeBalancer this month.
43	Out *float64 `json:"out"`
44	// The total outbound transfer, in MB, used for this NodeBalancer this month.
45	In *float64 `json:"in"`
46}
47
48// NodeBalancerCreateOptions are the options permitted for CreateNodeBalancer
49type NodeBalancerCreateOptions struct {
50	Label              *string                            `json:"label,omitempty"`
51	Region             string                             `json:"region,omitempty"`
52	ClientConnThrottle *int                               `json:"client_conn_throttle,omitempty"`
53	Configs            []*NodeBalancerConfigCreateOptions `json:"configs,omitempty"`
54	Tags               []string                           `json:"tags"`
55}
56
57// NodeBalancerUpdateOptions are the options permitted for UpdateNodeBalancer
58type NodeBalancerUpdateOptions struct {
59	Label              *string   `json:"label,omitempty"`
60	ClientConnThrottle *int      `json:"client_conn_throttle,omitempty"`
61	Tags               *[]string `json:"tags,omitempty"`
62}
63
64// UnmarshalJSON implements the json.Unmarshaler interface
65func (i *NodeBalancer) UnmarshalJSON(b []byte) error {
66	type Mask NodeBalancer
67
68	p := struct {
69		*Mask
70		Created *parseabletime.ParseableTime `json:"created"`
71		Updated *parseabletime.ParseableTime `json:"updated"`
72	}{
73		Mask: (*Mask)(i),
74	}
75
76	if err := json.Unmarshal(b, &p); err != nil {
77		return err
78	}
79
80	i.Created = (*time.Time)(p.Created)
81	i.Updated = (*time.Time)(p.Updated)
82
83	return nil
84}
85
86// GetCreateOptions converts a NodeBalancer to NodeBalancerCreateOptions for use in CreateNodeBalancer
87func (i NodeBalancer) GetCreateOptions() NodeBalancerCreateOptions {
88	return NodeBalancerCreateOptions{
89		Label:              i.Label,
90		Region:             i.Region,
91		ClientConnThrottle: &i.ClientConnThrottle,
92		Tags:               i.Tags,
93	}
94}
95
96// GetUpdateOptions converts a NodeBalancer to NodeBalancerUpdateOptions for use in UpdateNodeBalancer
97func (i NodeBalancer) GetUpdateOptions() NodeBalancerUpdateOptions {
98	return NodeBalancerUpdateOptions{
99		Label:              i.Label,
100		ClientConnThrottle: &i.ClientConnThrottle,
101		Tags:               &i.Tags,
102	}
103}
104
105// NodeBalancersPagedResponse represents a paginated NodeBalancer API response
106type NodeBalancersPagedResponse struct {
107	*PageOptions
108	Data []NodeBalancer `json:"data"`
109}
110
111func (NodeBalancersPagedResponse) endpoint(c *Client) string {
112	endpoint, err := c.NodeBalancers.Endpoint()
113	if err != nil {
114		panic(err)
115	}
116	return endpoint
117}
118
119func (resp *NodeBalancersPagedResponse) appendData(r *NodeBalancersPagedResponse) {
120	resp.Data = append(resp.Data, r.Data...)
121}
122
123// ListNodeBalancers lists NodeBalancers
124func (c *Client) ListNodeBalancers(ctx context.Context, opts *ListOptions) ([]NodeBalancer, error) {
125	response := NodeBalancersPagedResponse{}
126	err := c.listHelper(ctx, &response, opts)
127	if err != nil {
128		return nil, err
129	}
130	return response.Data, nil
131}
132
133// GetNodeBalancer gets the NodeBalancer with the provided ID
134func (c *Client) GetNodeBalancer(ctx context.Context, id int) (*NodeBalancer, error) {
135	e, err := c.NodeBalancers.Endpoint()
136	if err != nil {
137		return nil, err
138	}
139	e = fmt.Sprintf("%s/%d", e, id)
140	r, err := coupleAPIErrors(c.R(ctx).
141		SetResult(&NodeBalancer{}).
142		Get(e))
143	if err != nil {
144		return nil, err
145	}
146	return r.Result().(*NodeBalancer), nil
147}
148
149// CreateNodeBalancer creates a NodeBalancer
150func (c *Client) CreateNodeBalancer(ctx context.Context, nodebalancer NodeBalancerCreateOptions) (*NodeBalancer, error) {
151	var body string
152	e, err := c.NodeBalancers.Endpoint()
153	if err != nil {
154		return nil, err
155	}
156
157	req := c.R(ctx).SetResult(&NodeBalancer{})
158
159	if bodyData, err := json.Marshal(nodebalancer); err == nil {
160		body = string(bodyData)
161	} else {
162		return nil, NewError(err)
163	}
164
165	r, err := coupleAPIErrors(req.
166		SetHeader("Content-Type", "application/json").
167		SetBody(body).
168		Post(e))
169	if err != nil {
170		return nil, err
171	}
172	return r.Result().(*NodeBalancer), nil
173}
174
175// UpdateNodeBalancer updates the NodeBalancer with the specified id
176func (c *Client) UpdateNodeBalancer(ctx context.Context, id int, updateOpts NodeBalancerUpdateOptions) (*NodeBalancer, error) {
177	var body string
178	e, err := c.NodeBalancers.Endpoint()
179	if err != nil {
180		return nil, err
181	}
182	e = fmt.Sprintf("%s/%d", e, id)
183
184	req := c.R(ctx).SetResult(&NodeBalancer{})
185
186	if bodyData, err := json.Marshal(updateOpts); err == nil {
187		body = string(bodyData)
188	} else {
189		return nil, NewError(err)
190	}
191
192	r, err := coupleAPIErrors(req.
193		SetBody(body).
194		Put(e))
195	if err != nil {
196		return nil, err
197	}
198	return r.Result().(*NodeBalancer), nil
199}
200
201// DeleteNodeBalancer deletes the NodeBalancer with the specified id
202func (c *Client) DeleteNodeBalancer(ctx context.Context, id int) error {
203	e, err := c.NodeBalancers.Endpoint()
204	if err != nil {
205		return err
206	}
207	e = fmt.Sprintf("%s/%d", e, id)
208
209	_, err = coupleAPIErrors(c.R(ctx).Delete(e))
210
211	return err
212}
213