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
|
package function
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 (
waitForNamespaceDefaultTimeout = 15 * time.Minute
waitForCronDefaultTimeout = 15 * time.Minute
waitForDomainDefaultTimeout = 15 * time.Minute
waitForFunctionDefaultTimeout = 15 * time.Minute
waitForTriggerDefaultTimeout = 15 * time.Minute
defaultRetryInterval = 5 * time.Second
)
// WaitForNamespaceRequest is used by WaitForNamespace method.
type WaitForNamespaceRequest struct {
NamespaceID string
Region scw.Region
Timeout *time.Duration
RetryInterval *time.Duration
}
// WaitForNamespace waits for the Namespace to be in a ready state before returning.
func (s *API) WaitForNamespace(req *WaitForNamespaceRequest, opts ...scw.RequestOption) (*Namespace, error) {
timeout := waitForNamespaceDefaultTimeout
if req.Timeout != nil {
timeout = *req.Timeout
}
retryInterval := defaultRetryInterval
if req.RetryInterval != nil {
retryInterval = *req.RetryInterval
}
terminalStatus := map[NamespaceStatus]struct{}{
NamespaceStatusError: {},
NamespaceStatusReady: {},
NamespaceStatusLocked: {},
}
namespace, err := async.WaitSync(&async.WaitSyncConfig{
Get: func() (interface{}, bool, error) {
namespace, err := s.GetNamespace(&GetNamespaceRequest{
NamespaceID: req.NamespaceID,
Region: req.Region,
}, opts...)
if err != nil {
return nil, false, err
}
_, isTerminal := terminalStatus[namespace.Status]
return namespace, isTerminal, nil
},
Timeout: timeout,
IntervalStrategy: async.LinearIntervalStrategy(retryInterval),
})
if err != nil {
return nil, errors.Wrap(err, "waiting for Namespace failed")
}
return namespace.(*Namespace), nil
}
// WaitForFunctionRequest is used by WaitForNamespace method.
type WaitForFunctionRequest struct {
FunctionID string
Region scw.Region
Timeout *time.Duration
RetryInterval *time.Duration
}
// WaitForFunction waits for the Function to be in a ready state before returning.
func (s *API) WaitForFunction(req *WaitForFunctionRequest, opts ...scw.RequestOption) (*Function, error) {
timeout := waitForFunctionDefaultTimeout
if req.Timeout != nil {
timeout = *req.Timeout
}
retryInterval := defaultRetryInterval
if req.RetryInterval != nil {
retryInterval = *req.RetryInterval
}
terminalStatus := map[FunctionStatus]struct{}{
FunctionStatusCreated: {},
FunctionStatusError: {},
FunctionStatusLocked: {},
FunctionStatusReady: {},
}
function, err := async.WaitSync(&async.WaitSyncConfig{
Get: func() (interface{}, bool, error) {
function, err := s.GetFunction(&GetFunctionRequest{
FunctionID: req.FunctionID,
Region: req.Region,
}, opts...)
if err != nil {
return nil, false, err
}
_, isTerminal := terminalStatus[function.Status]
return function, isTerminal, nil
},
Timeout: timeout,
IntervalStrategy: async.LinearIntervalStrategy(retryInterval),
})
if err != nil {
return nil, errors.Wrap(err, "waiting for function failed")
}
return function.(*Function), nil
}
// WaitForCronRequest is used by WaitForNamespace method.
type WaitForCronRequest struct {
CronID string
Region scw.Region
Timeout *time.Duration
RetryInterval *time.Duration
}
// WaitForCron waits for the Cron to be in a ready state before returning.
func (s *API) WaitForCron(req *WaitForCronRequest, opts ...scw.RequestOption) (*Cron, error) {
timeout := waitForCronDefaultTimeout
if req.Timeout != nil {
timeout = *req.Timeout
}
retryInterval := defaultRetryInterval
if req.RetryInterval != nil {
retryInterval = *req.RetryInterval
}
terminalStatus := map[CronStatus]struct{}{
CronStatusError: {},
CronStatusReady: {},
CronStatusLocked: {},
}
cron, err := async.WaitSync(&async.WaitSyncConfig{
Get: func() (interface{}, bool, error) {
cron, err := s.GetCron(&GetCronRequest{
CronID: req.CronID,
Region: req.Region,
}, opts...)
if err != nil {
return nil, false, err
}
_, isTerminal := terminalStatus[cron.Status]
return cron, isTerminal, nil
},
Timeout: timeout,
IntervalStrategy: async.LinearIntervalStrategy(retryInterval),
})
if err != nil {
return nil, errors.Wrap(err, "waiting for Cron failed")
}
return cron.(*Cron), nil
}
// WaitForDomainRequest waits for the Domain to be in a ready state before returning.
type WaitForDomainRequest struct {
DomainID string
Region scw.Region
Timeout *time.Duration
RetryInterval *time.Duration
}
// WaitForDomain waits for the Domain to be in a ready state before returning.
func (s *API) WaitForDomain(req *WaitForDomainRequest, opts ...scw.RequestOption) (*Domain, error) {
timeout := waitForDomainDefaultTimeout
if req.Timeout != nil {
timeout = *req.Timeout
}
retryInterval := defaultRetryInterval
if req.RetryInterval != nil {
retryInterval = *req.RetryInterval
}
terminalStatus := map[DomainStatus]struct{}{
DomainStatusError: {},
DomainStatusReady: {},
}
domain, err := async.WaitSync(&async.WaitSyncConfig{
Get: func() (interface{}, bool, error) {
domain, err := s.GetDomain(&GetDomainRequest{
DomainID: req.DomainID,
Region: req.Region,
}, opts...)
if err != nil {
return nil, false, err
}
_, isTerminal := terminalStatus[domain.Status]
return domain, isTerminal, nil
},
Timeout: timeout,
IntervalStrategy: async.LinearIntervalStrategy(retryInterval),
})
if err != nil {
return nil, errors.Wrap(err, "waiting for Domain failed")
}
return domain.(*Domain), nil
}
// WaitForTriggerRequest waits for the Trigger to be in a ready state before returning.
type WaitForTriggerRequest struct {
TriggerID string
Region scw.Region
Timeout *time.Duration
RetryInterval *time.Duration
}
// WaitForTrigger waits for the Trigger to be in a ready state before returning.
func (s *API) WaitForTrigger(req *WaitForTriggerRequest, opts ...scw.RequestOption) (*Trigger, error) {
timeout := waitForTriggerDefaultTimeout
if req.Timeout != nil {
timeout = *req.Timeout
}
retryInterval := defaultRetryInterval
if req.RetryInterval != nil {
retryInterval = *req.RetryInterval
}
terminalStatus := map[TriggerStatus]struct{}{
TriggerStatusError: {},
TriggerStatusReady: {},
}
trigger, err := async.WaitSync(&async.WaitSyncConfig{
Get: func() (interface{}, bool, error) {
trigger, err := s.GetTrigger(&GetTriggerRequest{
TriggerID: req.TriggerID,
Region: req.Region,
}, opts...)
if err != nil {
return nil, false, err
}
_, isTerminal := terminalStatus[trigger.Status]
return trigger, isTerminal, nil
},
Timeout: timeout,
IntervalStrategy: async.LinearIntervalStrategy(retryInterval),
})
if err != nil {
return nil, errors.Wrap(err, "waiting for Trigger failed")
}
return trigger.(*Trigger), nil
}
|