forked from digitalocean/doctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_balancers.go
141 lines (114 loc) · 4.04 KB
/
load_balancers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
Copyright 2018 The Doctl Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package do
import (
"context"
"github.com/digitalocean/godo"
)
// LoadBalancer wraps a godo LoadBalancer.
type LoadBalancer struct {
*godo.LoadBalancer
}
// LoadBalancers is a slice of LoadBalancer.
type LoadBalancers []LoadBalancer
// LoadBalancersService is the godo LoadBalancersService interface.
type LoadBalancersService interface {
Get(lbID string) (*LoadBalancer, error)
List() (LoadBalancers, error)
Create(lbr *godo.LoadBalancerRequest) (*LoadBalancer, error)
Update(lbID string, lbr *godo.LoadBalancerRequest) (*LoadBalancer, error)
Delete(lbID string) error
AddDroplets(lbID string, dIDs ...int) error
RemoveDroplets(lbID string, dIDs ...int) error
AddForwardingRules(lbID string, rules ...godo.ForwardingRule) error
RemoveForwardingRules(lbID string, rules ...godo.ForwardingRule) error
PurgeCache(lbID string) error
}
var _ LoadBalancersService = &loadBalancersService{}
type loadBalancersService struct {
client *godo.Client
}
// NewLoadBalancersService builds an instance of LoadBalancersService.
func NewLoadBalancersService(client *godo.Client) LoadBalancersService {
return &loadBalancersService{
client: client,
}
}
func (lbs *loadBalancersService) Get(lbID string) (*LoadBalancer, error) {
lb, _, err := lbs.client.LoadBalancers.Get(context.TODO(), lbID)
if err != nil {
return nil, err
}
return &LoadBalancer{LoadBalancer: lb}, nil
}
func (lbs *loadBalancersService) List() (LoadBalancers, error) {
f := func(opt *godo.ListOptions) ([]any, *godo.Response, error) {
list, resp, err := lbs.client.LoadBalancers.List(context.TODO(), opt)
if err != nil {
return nil, nil, err
}
si := make([]any, len(list))
for i := range list {
si[i] = list[i]
}
return si, resp, err
}
si, err := PaginateResp(f)
if err != nil {
return nil, err
}
list := make([]LoadBalancer, len(si))
for i := range si {
a := si[i].(godo.LoadBalancer)
list[i] = LoadBalancer{LoadBalancer: &a}
}
return list, nil
}
func (lbs *loadBalancersService) Create(lbr *godo.LoadBalancerRequest) (*LoadBalancer, error) {
lb, _, err := lbs.client.LoadBalancers.Create(context.TODO(), lbr)
if err != nil {
return nil, err
}
return &LoadBalancer{LoadBalancer: lb}, nil
}
func (lbs *loadBalancersService) Update(lbID string, lbr *godo.LoadBalancerRequest) (*LoadBalancer, error) {
lb, _, err := lbs.client.LoadBalancers.Update(context.TODO(), lbID, lbr)
if err != nil {
return nil, err
}
return &LoadBalancer{LoadBalancer: lb}, nil
}
func (lbs *loadBalancersService) Delete(lbID string) error {
_, err := lbs.client.LoadBalancers.Delete(context.TODO(), lbID)
return err
}
func (lbs *loadBalancersService) AddDroplets(lbID string, dIDs ...int) error {
_, err := lbs.client.LoadBalancers.AddDroplets(context.TODO(), lbID, dIDs...)
return err
}
func (lbs *loadBalancersService) RemoveDroplets(lbID string, dIDs ...int) error {
_, err := lbs.client.LoadBalancers.RemoveDroplets(context.TODO(), lbID, dIDs...)
return err
}
func (lbs *loadBalancersService) AddForwardingRules(lbID string, rules ...godo.ForwardingRule) error {
_, err := lbs.client.LoadBalancers.AddForwardingRules(context.TODO(), lbID, rules...)
return err
}
func (lbs *loadBalancersService) RemoveForwardingRules(lbID string, rules ...godo.ForwardingRule) error {
_, err := lbs.client.LoadBalancers.RemoveForwardingRules(context.TODO(), lbID, rules...)
return err
}
func (lbs *loadBalancersService) PurgeCache(lbID string) error {
_, err := lbs.client.LoadBalancers.PurgeCache(context.TODO(), lbID)
return err
}