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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
package lb
import (
"time"
"github.com/scaleway/scaleway-sdk-go/internal/async"
"github.com/scaleway/scaleway-sdk-go/internal/errors"
"github.com/scaleway/scaleway-sdk-go/scw"
)
const (
defaultRetryInterval = 2 * time.Second
defaultTimeout = 5 * time.Minute
)
// WaitForLBRequest is used by WaitForLb method.
type WaitForLBRequest struct {
LBID string
Region scw.Region
Timeout *time.Duration
RetryInterval *time.Duration
}
// WaitForLb waits for the lb to be in a "terminal state" before returning.
// This function can be used to wait for a lb to be ready for example.
func (s *API) WaitForLb(req *WaitForLBRequest, opts ...scw.RequestOption) (*LB, error) {
return waitForLb(req.Timeout, req.RetryInterval, func() (*LB, error) {
return s.GetLB(&GetLBRequest{
Region: req.Region,
LBID: req.LBID,
}, opts...)
})
}
// ZonedAPIWaitForLBRequest is used by WaitForLb method.
type ZonedAPIWaitForLBRequest struct {
LBID string
Zone scw.Zone
Timeout *time.Duration
RetryInterval *time.Duration
}
// WaitForLb waits for the lb to be in a "terminal state" before returning.
// This function can be used to wait for a lb to be ready for example.
func (s *ZonedAPI) WaitForLb(req *ZonedAPIWaitForLBRequest, opts ...scw.RequestOption) (*LB, error) {
return waitForLb(req.Timeout, req.RetryInterval, func() (*LB, error) {
return s.GetLB(&ZonedAPIGetLBRequest{
Zone: req.Zone,
LBID: req.LBID,
}, opts...)
})
}
func waitForLb(timeout *time.Duration, retryInterval *time.Duration, getLB func() (*LB, error)) (*LB, error) {
t := defaultTimeout
if timeout != nil {
t = *timeout
}
r := defaultRetryInterval
if retryInterval != nil {
r = *retryInterval
}
terminalStatus := map[LBStatus]struct{}{
LBStatusReady: {},
LBStatusStopped: {},
LBStatusError: {},
LBStatusLocked: {},
}
lb, err := async.WaitSync(&async.WaitSyncConfig{
Get: func() (interface{}, bool, error) {
res, err := getLB()
if err != nil {
return nil, false, err
}
_, isTerminal := terminalStatus[res.Status]
return res, isTerminal, nil
},
Timeout: t,
IntervalStrategy: async.LinearIntervalStrategy(r),
})
if err != nil {
return nil, errors.Wrap(err, "waiting for lb failed")
}
return lb.(*LB), nil
}
// ZonedAPIWaitForLBInstancesRequest is used by WaitForLb method.
type ZonedAPIWaitForLBInstancesRequest struct {
LBID string
Zone scw.Zone
Timeout *time.Duration
RetryInterval *time.Duration
}
// WaitForLbInstances waits for the lb to be in a "terminal state" and the attached instances before returning.
func (s *ZonedAPI) WaitForLbInstances(req *ZonedAPIWaitForLBInstancesRequest, opts ...scw.RequestOption) (*LB, error) {
return waitForLbInstances(req.Timeout, req.RetryInterval, func() (*LB, error) {
return s.GetLB(&ZonedAPIGetLBRequest{
Zone: req.Zone,
LBID: req.LBID,
}, opts...)
})
}
func waitForLbInstances(timeout *time.Duration, retryInterval *time.Duration, getLB func() (*LB, error)) (*LB, error) {
t := defaultTimeout
if timeout != nil {
t = *timeout
}
r := defaultRetryInterval
if retryInterval != nil {
r = *retryInterval
}
terminalLBStatus := map[LBStatus]struct{}{
LBStatusReady: {},
LBStatusStopped: {},
LBStatusError: {},
LBStatusLocked: {},
}
terminalInstanceStatus := map[InstanceStatus]struct{}{
InstanceStatusReady: {},
InstanceStatusStopped: {},
InstanceStatusError: {},
InstanceStatusLocked: {},
}
lb, err := async.WaitSync(&async.WaitSyncConfig{
Get: func() (interface{}, bool, error) {
res, err := getLB()
if err != nil {
return nil, false, err
}
_, isTerminal := terminalLBStatus[res.Status]
for _, i := range res.Instances {
_, isInstanceTerminal := terminalInstanceStatus[i.Status]
if !isInstanceTerminal {
return res, isTerminal, nil
}
}
return res, isTerminal, nil
},
Timeout: t,
IntervalStrategy: async.LinearIntervalStrategy(r),
})
if err != nil {
return nil, errors.Wrap(err, "waiting for lb failed")
}
return lb.(*LB), nil
}
// ZonedAPIWaitForLBPNRequest is used by WaitForLBPN method.
type ZonedAPIWaitForLBPNRequest struct {
LBID string
Zone scw.Zone
Timeout *time.Duration
RetryInterval *time.Duration
}
func waitForPNLb(timeout *time.Duration, retryInterval *time.Duration, getPNs func() ([]*PrivateNetwork, error)) ([]*PrivateNetwork, error) {
t := defaultTimeout
if timeout != nil {
t = *timeout
}
r := defaultRetryInterval
if retryInterval != nil {
r = *retryInterval
}
terminalStatus := map[PrivateNetworkStatus]struct{}{
PrivateNetworkStatusReady: {},
PrivateNetworkStatusError: {},
}
pn, err := async.WaitSync(&async.WaitSyncConfig{
Get: func() (interface{}, bool, error) {
pns, err := getPNs()
for _, pn := range pns {
if err != nil {
return nil, false, err
}
//wait at the first not terminal state
_, isTerminal := terminalStatus[pn.Status]
if !isTerminal {
return pns, isTerminal, nil
}
}
return pns, true, nil
},
Timeout: t,
IntervalStrategy: async.LinearIntervalStrategy(r),
})
if err != nil {
return nil, errors.Wrap(err, "waiting for attachment failed")
}
return pn.([]*PrivateNetwork), nil
}
// WaitForLBPN waits for the private_network attached status on a load balancer
// to be in a "terminal state" before returning.
// This function can be used to wait for an attached private_network to be ready for example.
func (s *ZonedAPI) WaitForLBPN(req *ZonedAPIWaitForLBPNRequest, opts ...scw.RequestOption) ([]*PrivateNetwork, error) {
return waitForPNLb(req.Timeout, req.RetryInterval, func() ([]*PrivateNetwork, error) {
lbPNs, err := s.ListLBPrivateNetworks(&ZonedAPIListLBPrivateNetworksRequest{
Zone: req.Zone,
LBID: req.LBID,
}, opts...)
if err != nil {
return nil, err
}
return lbPNs.PrivateNetwork, nil
})
}
// ZonedAPIWaitForLBCertificateRequest is used by WaitForLbCertificate method.
type ZonedAPIWaitForLBCertificateRequest struct {
CertID string
Zone scw.Zone
Timeout *time.Duration
RetryInterval *time.Duration
}
// WaitForLBCertificate waits for the certificate to be in a "terminal state"
func (s *ZonedAPI) WaitForLBCertificate(req *ZonedAPIWaitForLBCertificateRequest, opts ...scw.RequestOption) (*Certificate, error) {
return waitForLBCertificate(req.Timeout, req.RetryInterval, func() (*Certificate, error) {
return s.GetCertificate(&ZonedAPIGetCertificateRequest{
Zone: req.Zone,
CertificateID: req.CertID,
}, opts...)
})
}
func waitForLBCertificate(timeout *time.Duration, retryInterval *time.Duration, getCertificate func() (*Certificate, error)) (*Certificate, error) {
t := defaultTimeout
if timeout != nil {
t = *timeout
}
r := defaultRetryInterval
if retryInterval != nil {
r = *retryInterval
}
terminalStatus := map[CertificateStatus]struct{}{
CertificateStatusError: {},
CertificateStatusReady: {},
}
crt, err := async.WaitSync(&async.WaitSyncConfig{
Get: func() (interface{}, bool, error) {
res, err := getCertificate()
if err != nil {
return nil, false, err
}
_, isTerminal := terminalStatus[res.Status]
return res, isTerminal, nil
},
Timeout: t,
IntervalStrategy: async.LinearIntervalStrategy(r),
})
if err != nil {
return nil, errors.Wrap(err, "waiting for lb failed")
}
return crt.(*Certificate), nil
}
|