@@ -13,60 +13,6 @@ const (
1313 defaultTimeout = 5 * time .Minute
1414)
1515
16- // WaitForAlertRequest is used by WaitForAlert method.
17- type WaitForAlertRequest struct {
18- Region scw.Region
19- AlertID string
20- Timeout * time.Duration
21- RetryInterval * time.Duration
22- }
23-
24- // WaitForAlert waits for the alert to be in a "terminal state" before returning.
25- // This function can be used to wait for an alert to be enabled or disabled.
26- func (s * RegionalAPI ) WaitForAlert (req * WaitForAlertRequest , opts ... scw.RequestOption ) (* Alert , error ) {
27- timeout := defaultTimeout
28- if req .Timeout != nil {
29- timeout = * req .Timeout
30- }
31- retryInterval := defaultRetryInterval
32- if req .RetryInterval != nil {
33- retryInterval = * req .RetryInterval
34- }
35-
36- terminalStatus := map [AlertStatus ]struct {}{
37- AlertStatusEnabled : {},
38- AlertStatusDisabled : {},
39- }
40-
41- alert , err := async .WaitSync (& async.WaitSyncConfig {
42- Get : func () (any , bool , error ) {
43- // List all alerts and find the one with matching ID
44- res , err := s .ListAlerts (& RegionalAPIListAlertsRequest {
45- Region : req .Region ,
46- }, opts ... )
47- if err != nil {
48- return nil , false , err
49- }
50-
51- // Find the alert by ID
52- for _ , alert := range res .Alerts {
53- if alert .ID == req .AlertID {
54- _ , isTerminal := terminalStatus [alert .RuleStatus ]
55- return alert , isTerminal , nil
56- }
57- }
58-
59- return nil , false , errors .New ("alert not found" )
60- },
61- Timeout : timeout ,
62- IntervalStrategy : async .LinearIntervalStrategy (retryInterval ),
63- })
64- if err != nil {
65- return nil , errors .Wrap (err , "waiting for alert failed" )
66- }
67- return alert .(* Alert ), nil
68- }
69-
7016// WaitForPreconfiguredAlertsRequest is used by WaitForPreconfiguredAlerts method.
7117type WaitForPreconfiguredAlertsRequest struct {
7218 Region scw.Region
@@ -90,17 +36,14 @@ func (s *RegionalAPI) WaitForPreconfiguredAlerts(req *WaitForPreconfiguredAlerts
9036 retryInterval = * req .RetryInterval
9137 }
9238
93- // For enabling/disabling, accept transitional states as terminal
9439 var terminalStatus map [AlertStatus ]struct {}
9540 switch req .TargetStatus {
9641 case AlertStatusEnabled :
97- // Accept both enabled and enabling as terminal states
9842 terminalStatus = map [AlertStatus ]struct {}{
9943 AlertStatusEnabled : {},
10044 AlertStatusEnabling : {},
10145 }
10246 case AlertStatusDisabled :
103- // Accept both disabled and disabling as terminal states
10447 terminalStatus = map [AlertStatus ]struct {}{
10548 AlertStatusDisabled : {},
10649 AlertStatusDisabling : {},
@@ -113,7 +56,6 @@ func (s *RegionalAPI) WaitForPreconfiguredAlerts(req *WaitForPreconfiguredAlerts
11356
11457 result , err := async .WaitSync (& async.WaitSyncConfig {
11558 Get : func () (any , bool , error ) {
116- // List all preconfigured alerts for the project
11759 res , err := s .ListAlerts (& RegionalAPIListAlertsRequest {
11860 Region : req .Region ,
11961 ProjectID : req .ProjectID ,
@@ -123,15 +65,13 @@ func (s *RegionalAPI) WaitForPreconfiguredAlerts(req *WaitForPreconfiguredAlerts
12365 return nil , false , err
12466 }
12567
126- // Create a map of rule ID to alert
12768 alertsByRuleID := make (map [string ]* Alert )
12869 for _ , alert := range res .Alerts {
12970 if alert .PreconfiguredData != nil && alert .PreconfiguredData .PreconfiguredRuleID != "" {
13071 alertsByRuleID [alert .PreconfiguredData .PreconfiguredRuleID ] = alert
13172 }
13273 }
13374
134- // Check if all requested alerts are in terminal state
13575 var matchedAlerts []* Alert
13676 for _ , ruleID := range req .PreconfiguredRules {
13777 alert , found := alertsByRuleID [ruleID ]
@@ -141,14 +81,12 @@ func (s *RegionalAPI) WaitForPreconfiguredAlerts(req *WaitForPreconfiguredAlerts
14181
14282 _ , isTerminal := terminalStatus [alert .RuleStatus ]
14383 if ! isTerminal {
144- // At least one alert is not in terminal state, continue waiting
14584 return nil , false , nil
14685 }
14786
14887 matchedAlerts = append (matchedAlerts , alert )
14988 }
15089
151- // All alerts are in terminal state
15290 return matchedAlerts , true , nil
15391 },
15492 Timeout : timeout ,
@@ -159,4 +97,3 @@ func (s *RegionalAPI) WaitForPreconfiguredAlerts(req *WaitForPreconfiguredAlerts
15997 }
16098 return result .([]* Alert ), nil
16199}
162-
0 commit comments