-
-
Notifications
You must be signed in to change notification settings - Fork 233
/
incus_network_load_balancers.go
132 lines (107 loc) · 3.87 KB
/
incus_network_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
package incus
import (
"github.com/lxc/incus/v6/shared/api"
)
// GetNetworkLoadBalancerAddresses returns a list of network load balancer listen addresses.
func (r *ProtocolIncus) GetNetworkLoadBalancerAddresses(networkName string) ([]string, error) {
err := r.CheckExtension("network_load_balancer")
if err != nil {
return nil, err
}
// Fetch the raw URL values.
urls := []string{}
u := api.NewURL().Path("networks", networkName, "load-balancers")
_, err = r.queryStruct("GET", u.String(), nil, "", &urls)
if err != nil {
return nil, err
}
// Parse it.
return urlsToResourceNames(u.String(), urls...)
}
// GetNetworkLoadBalancers returns a list of Network load balancer structs.
func (r *ProtocolIncus) GetNetworkLoadBalancers(networkName string) ([]api.NetworkLoadBalancer, error) {
err := r.CheckExtension("network_load_balancer")
if err != nil {
return nil, err
}
loadBalancers := []api.NetworkLoadBalancer{}
// Fetch the raw value.
u := api.NewURL().Path("networks", networkName, "load-balancers").WithQuery("recursion", "1")
_, err = r.queryStruct("GET", u.String(), nil, "", &loadBalancers)
if err != nil {
return nil, err
}
return loadBalancers, nil
}
// GetNetworkLoadBalancer returns a Network load balancer entry for the provided network and listen address.
func (r *ProtocolIncus) GetNetworkLoadBalancer(networkName string, listenAddress string) (*api.NetworkLoadBalancer, string, error) {
err := r.CheckExtension("network_load_balancer")
if err != nil {
return nil, "", err
}
loadBalancer := api.NetworkLoadBalancer{}
// Fetch the raw value.
u := api.NewURL().Path("networks", networkName, "load-balancers", listenAddress)
etag, err := r.queryStruct("GET", u.String(), nil, "", &loadBalancer)
if err != nil {
return nil, "", err
}
return &loadBalancer, etag, nil
}
// CreateNetworkLoadBalancer defines a new network load balancer using the provided struct.
func (r *ProtocolIncus) CreateNetworkLoadBalancer(networkName string, loadBalancer api.NetworkLoadBalancersPost) error {
err := r.CheckExtension("network_load_balancer")
if err != nil {
return err
}
// Send the request.
u := api.NewURL().Path("networks", networkName, "load-balancers")
_, _, err = r.query("POST", u.String(), loadBalancer, "")
if err != nil {
return err
}
return nil
}
// UpdateNetworkLoadBalancer updates the network load balancer to match the provided struct.
func (r *ProtocolIncus) UpdateNetworkLoadBalancer(networkName string, listenAddress string, loadBalancer api.NetworkLoadBalancerPut, ETag string) error {
err := r.CheckExtension("network_load_balancer")
if err != nil {
return err
}
// Send the request.
u := api.NewURL().Path("networks", networkName, "load-balancers", listenAddress)
_, _, err = r.query("PUT", u.String(), loadBalancer, ETag)
if err != nil {
return err
}
return nil
}
// DeleteNetworkLoadBalancer deletes an existing network load balancer.
func (r *ProtocolIncus) DeleteNetworkLoadBalancer(networkName string, listenAddress string) error {
err := r.CheckExtension("network_load_balancer")
if err != nil {
return err
}
// Send the request.
u := api.NewURL().Path("networks", networkName, "load-balancers", listenAddress)
_, _, err = r.query("DELETE", u.String(), nil, "")
if err != nil {
return err
}
return nil
}
// GetNetworkLoadBalancerState returns a Network load balancer state for the provided network and listen address.
func (r *ProtocolIncus) GetNetworkLoadBalancerState(networkName string, listenAddress string) (*api.NetworkLoadBalancerState, error) {
err := r.CheckExtension("network_load_balancer_state")
if err != nil {
return nil, err
}
lbState := api.NetworkLoadBalancerState{}
// Fetch the raw value.
u := api.NewURL().Path("networks", networkName, "load-balancers", listenAddress, "state")
_, err = r.queryStruct("GET", u.String(), nil, "", &lbState)
if err != nil {
return nil, err
}
return &lbState, nil
}