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
|
package vpcgw
import (
"time"
"github.com/scaleway/scaleway-sdk-go/errors"
"github.com/scaleway/scaleway-sdk-go/internal/async"
"github.com/scaleway/scaleway-sdk-go/scw"
)
const (
defaultTimeout = 5 * time.Minute
defaultRetryInterval = 15 * time.Second
)
// WaitForGatewayRequest is used by WaitForGateway method
type WaitForGatewayRequest struct {
GatewayID string
Zone scw.Zone
Timeout *time.Duration
RetryInterval *time.Duration
}
// WaitForGateway waits for the gateway to be in a "terminal state" before returning.
// This function can be used to wait for a gateway to be ready for example.
func (s *API) WaitForGateway(req *WaitForGatewayRequest, opts ...scw.RequestOption) (*Gateway, error) {
timeout := defaultTimeout
if req.Timeout != nil {
timeout = *req.Timeout
}
retryInterval := defaultRetryInterval
if req.RetryInterval != nil {
retryInterval = *req.RetryInterval
}
terminalStatus := map[GatewayStatus]struct{}{
GatewayStatusUnknown: {},
GatewayStatusStopped: {},
GatewayStatusRunning: {},
GatewayStatusFailed: {},
GatewayStatusDeleted: {},
GatewayStatusLocked: {},
}
gateway, err := async.WaitSync(&async.WaitSyncConfig{
Get: func() (interface{}, bool, error) {
ns, err := s.GetGateway(&GetGatewayRequest{
Zone: req.Zone,
GatewayID: req.GatewayID,
}, opts...)
if err != nil {
return nil, false, err
}
_, isTerminal := terminalStatus[ns.Status]
return ns, isTerminal, err
},
Timeout: timeout,
IntervalStrategy: async.LinearIntervalStrategy(retryInterval),
})
if err != nil {
return nil, errors.Wrap(err, "waiting for gateway failed")
}
return gateway.(*Gateway), nil
}
// WaitForGatewayNetworkRequest is used by WaitForGatewayNetwork method
type WaitForGatewayNetworkRequest struct {
GatewayNetworkID string
Zone scw.Zone
Timeout *time.Duration
RetryInterval *time.Duration
}
// WaitForGatewayNetwork waits for the gateway network to be in a "terminal state" before returning.
// This function can be used to wait for a gateway network to be ready for example.
func (s *API) WaitForGatewayNetwork(req *WaitForGatewayNetworkRequest, opts ...scw.RequestOption) (*GatewayNetwork, error) {
timeout := defaultTimeout
if req.Timeout != nil {
timeout = *req.Timeout
}
retryInterval := defaultRetryInterval
if req.RetryInterval != nil {
retryInterval = *req.RetryInterval
}
terminalStatus := map[GatewayNetworkStatus]struct{}{
GatewayNetworkStatusReady: {},
GatewayNetworkStatusUnknown: {},
GatewayNetworkStatusDeleted: {},
}
gatewayNetwork, err := async.WaitSync(&async.WaitSyncConfig{
Get: func() (interface{}, bool, error) {
ns, err := s.GetGatewayNetwork(&GetGatewayNetworkRequest{
Zone: req.Zone,
GatewayNetworkID: req.GatewayNetworkID,
}, opts...)
if err != nil {
return nil, false, err
}
_, isTerminal := terminalStatus[ns.Status]
return ns, isTerminal, err
},
Timeout: timeout,
IntervalStrategy: async.LinearIntervalStrategy(retryInterval),
})
if err != nil {
return nil, errors.Wrap(err, "waiting for gateway network failed")
}
return gatewayNetwork.(*GatewayNetwork), nil
}
// WaitForDHCPEntriesRequest is used by WaitForDHCPEntries method
type WaitForDHCPEntriesRequest struct {
GatewayNetworkID *string
MacAddress string
Zone scw.Zone
Timeout *time.Duration
RetryInterval *time.Duration
}
// WaitForDHCPEntries waits for at least one dhcp entry with the correct mac address.
// This function can be used to wait for an instance to use dhcp
func (s *API) WaitForDHCPEntries(req *WaitForDHCPEntriesRequest, opts ...scw.RequestOption) (*ListDHCPEntriesResponse, error) {
timeout := defaultTimeout
if req.Timeout != nil {
timeout = *req.Timeout
}
retryInterval := defaultRetryInterval
if req.RetryInterval != nil {
retryInterval = *req.RetryInterval
}
dhcpEntries, err := async.WaitSync(&async.WaitSyncConfig{
Get: func() (interface{}, bool, error) {
entries, err := s.ListDHCPEntries(&ListDHCPEntriesRequest{
Zone: req.Zone,
GatewayNetworkID: req.GatewayNetworkID,
MacAddress: &req.MacAddress,
}, opts...)
if err != nil {
return nil, false, err
}
containsMacAddress := false
for _, entry := range entries.DHCPEntries {
if entry.MacAddress == req.MacAddress {
containsMacAddress = true
break
}
}
return entries, containsMacAddress, err
},
Timeout: timeout,
IntervalStrategy: async.LinearIntervalStrategy(retryInterval),
})
if err != nil {
return nil, errors.Wrap(err, "waiting for gateway network failed")
}
return dhcpEntries.(*ListDHCPEntriesResponse), nil
}
|