This repository has been archived by the owner on Feb 14, 2023. It is now read-only.
/
helpers.go
191 lines (163 loc) · 4.57 KB
/
helpers.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package cloudstack
import (
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/dgraph-io/ristretto"
"github.com/xanzy/go-cloudstack/v2/cloudstack"
)
func vmCacheKey(name, projectID string) string {
return strings.Join([]string{name, projectID}, "\x00")
}
var ErrVMNotFound = errors.New("vm not found in cloudstack")
type cloudstackManager struct {
client *cloudstack.CloudStackClient
cache *ristretto.Cache
}
func newCloudstackManager(client *cloudstack.CloudStackClient) (*cloudstackManager, error) {
m := &cloudstackManager{
client: client,
}
err := m.initCache()
if err != nil {
return nil, err
}
return m, nil
}
func (m *cloudstackManager) initCache() error {
cache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 100000, // Expected maximum items set to 10000, this value is 10x this number, as recommend by the docs.
MaxCost: 50 << 20, // Maximum cost of cache (50 MB).
BufferItems: 64, // The magic value of 64 is the one recommended by the docs initially.
Cost: func(value interface{}) int64 {
// Approximate in memory size by converting to json
data, _ := json.Marshal(value)
return int64(len(data))
},
})
if err != nil {
return err
}
m.cache = cache
return nil
}
func (m *cloudstackManager) virtualMachineByName(name, projectID string) (*cloudstack.VirtualMachine, error) {
key := vmCacheKey(name, projectID)
if vm, found := m.cache.Get(key); found {
return vm.(*cloudstack.VirtualMachine), nil
}
p := m.client.VirtualMachine.NewListVirtualMachinesParams()
p.SetName(name)
if projectID != "" {
p.SetProjectid(projectID)
}
vmsResponse, err := m.client.VirtualMachine.ListVirtualMachines(p)
if err != nil {
return nil, err
}
if len(vmsResponse.VirtualMachines) == 0 {
return nil, ErrVMNotFound
}
if len(vmsResponse.VirtualMachines) > 1 {
return nil, fmt.Errorf("more than one machine found with name %q in project %q", name, projectID)
}
vm := vmsResponse.VirtualMachines[0]
m.cache.Set(key, vm, 0)
return vm, nil
}
type pageParams interface {
SetPagesize(int)
SetPage(int)
}
type resultWithID interface {
ID() string
Raw() interface{}
}
type vmWrapper struct {
*cloudstack.VirtualMachine
}
func (w vmWrapper) ID() string {
return w.Id
}
func (w vmWrapper) Raw() interface{} {
return w.VirtualMachine
}
type ipWrapper struct {
*cloudstack.PublicIpAddress
}
func (w ipWrapper) ID() string {
return w.Id
}
func (w ipWrapper) Raw() interface{} {
return w.PublicIpAddress
}
func listAllPagesUnsafe(client *cloudstack.CloudStackClient, pageSize int, params pageParams) ([]resultWithID, error) {
params.SetPagesize(pageSize)
page := 1
var result []resultWithID
var totalCount, resultSize int
for {
params.SetPage(page)
switch paramsTyped := params.(type) {
case *cloudstack.ListLoadBalancerRuleInstancesParams:
l, err := client.LoadBalancer.ListLoadBalancerRuleInstances(paramsTyped)
if err != nil {
return nil, err
}
for _, vm := range l.LoadBalancerRuleInstances {
result = append(result, vmWrapper{vm})
}
totalCount = l.Count
resultSize = len(l.LoadBalancerRuleInstances)
case *cloudstack.ListPublicIpAddressesParams:
l, err := client.Address.ListPublicIpAddresses(paramsTyped)
if err != nil {
return nil, err
}
for _, ip := range l.PublicIpAddresses {
result = append(result, ipWrapper{ip})
}
totalCount = l.Count
resultSize = len(l.PublicIpAddresses)
}
if resultSize == 0 || len(result) >= totalCount {
break
}
page++
}
existingSet := make(map[string]struct{})
for i := 0; i < len(result); i++ {
el := result[i]
if _, ok := existingSet[el.ID()]; ok {
result[i] = result[len(result)-1]
result = result[:len(result)-1]
i--
continue
}
existingSet[el.ID()] = struct{}{}
}
return result, nil
}
func listAllLBInstancesPages(client *cloudstack.CloudStackClient, params *cloudstack.ListLoadBalancerRuleInstancesParams) ([]*cloudstack.VirtualMachine, error) {
entries, err := listAllPagesUnsafe(client, 50, params)
if err != nil {
return nil, err
}
result := make([]*cloudstack.VirtualMachine, len(entries))
for i := range entries {
result[i] = entries[i].Raw().(*cloudstack.VirtualMachine)
}
return result, nil
}
func listAllIPPages(client *cloudstack.CloudStackClient, params *cloudstack.ListPublicIpAddressesParams) ([]*cloudstack.PublicIpAddress, error) {
entries, err := listAllPagesUnsafe(client, 50, params)
if err != nil {
return nil, err
}
result := make([]*cloudstack.PublicIpAddress, len(entries))
for i := range entries {
result[i] = entries[i].Raw().(*cloudstack.PublicIpAddress)
}
return result, nil
}