-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterface_manager_options.go
105 lines (92 loc) · 3.09 KB
/
interface_manager_options.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
package stableinterfaces
import (
"errors"
"fmt"
"time"
)
type (
InterfaceManagerOption func(manager *InterfaceManager) error
)
var (
ErrInterfaceNotWithConnect = errors.New("the interface did not implement StableInterfaceWithConnect, check if you are missing the OnConnect handler, , or need to use the WithConnect setting")
ErrInterfaceNotWithAlarm = errors.New("the interface did not implement StableInterfaceWithAlarm, check if you are missing the Alarm handler, or need to use the WithAlarm setting")
ErrInterfaceManagerNotWithAlarm = errors.New("the interface manager does not have WithAlarm()")
)
const (
withAlarmTestInstanceID = "withAlarmTestInstanceID"
withConnectTestInstanceID = "withConnectTestInstanceID"
)
func WithConnect() InterfaceManagerOption {
return func(manager *InterfaceManager) error {
// Spawn a test interface (this doesn't even belong on this host)
testInterface, err := manager.getOrMakeInstance(withConnectTestInstanceID)
if err != nil {
return fmt.Errorf("error in getOrMakeInstance: %w", err)
}
defer manager.destroyInstanceIfExists(withConnectTestInstanceID)
if _, ok := testInterface.stableInterface.(StableInterfaceWithConnect); !ok {
return ErrInterfaceNotWithConnect
}
// We are good otherwise
manager.withConnect = true
return nil
}
}
func WithAlarm(alarmManager AlarmManager) InterfaceManagerOption {
return func(manager *InterfaceManager) error {
// Spawn a test interface (this doesn't even belong on this host)
testInterface, err := manager.getOrMakeInstance(withAlarmTestInstanceID)
if err != nil {
return fmt.Errorf("error in getOrMakeInstance: %w", err)
}
defer manager.destroyInstanceIfExists(withAlarmTestInstanceID)
if _, ok := testInterface.stableInterface.(StableInterfaceWithAlarm); !ok {
return ErrInterfaceNotWithAlarm
}
// We are good otherwise
manager.alarmManager = alarmManager
return nil
}
}
func WithAlarmCheckInterval(duration time.Duration) InterfaceManagerOption {
return func(manager *InterfaceManager) error {
manager.alarmCheckInterval = &duration
return nil
}
}
func WithGetAlarmsTimeout(duration time.Duration) InterfaceManagerOption {
return func(manager *InterfaceManager) error {
manager.getAlarmsTimeout = &duration
return nil
}
}
func WithOnAlarmTimeout(duration time.Duration) InterfaceManagerOption {
return func(manager *InterfaceManager) error {
manager.onAlarmTimeout = &duration
return nil
}
}
func WithModifyAlarmTimeout(duration time.Duration) InterfaceManagerOption {
return func(manager *InterfaceManager) error {
manager.modifyAlarmTimeout = &duration
return nil
}
}
func WithAlarmRetryBackoff(duration time.Duration) InterfaceManagerOption {
return func(manager *InterfaceManager) error {
manager.alarmRetryBackoff = &duration
return nil
}
}
func WithMaxAlarmAttempts(attempts int) InterfaceManagerOption {
return func(manager *InterfaceManager) error {
manager.maxAlarmAttempts = attempts
return nil
}
}
func WithLogger(logger Logger) InterfaceManagerOption {
return func(manager *InterfaceManager) error {
manager.logger = logger
return nil
}
}