-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathservice_discovery_aws.go
174 lines (142 loc) · 4.27 KB
/
service_discovery_aws.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
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
package storage
import (
"slices"
"sync"
"github.com/haproxytech/client-native/v6/models"
"github.com/haproxytech/dataplaneapi/storagetype"
)
const (
AWSRegionFileName = "service_discovery/aws.json"
)
type SDAWStorage interface {
AWS
Load() error
Store() error
}
type AWS interface {
// GetAWS does not load the AWSRegions from the storage. Use Load() to load the them if needed.
GetAWSRegions() storagetype.AWSRegions
// AddAWSRegionsAndStore adds a list of AWSRegions to the storage and stores in the storage file.
AddAWSRegionsAndStore(AWS storagetype.AWSRegions) error
// AddAWSRegionAndStore adds a new AWSRegion to the storage and stores in the storage file.
AddAWSRegionAndStore(AWS *models.AwsRegion) error
// RemoveAWSAndStore removes a AWSRegion from the storage and stores in the storage file.
RemoveAWSRegionAndStore(AWS *models.AwsRegion) error
// ReplaceAllAWSRegionsAndStore replaces the list of AWSRegions in the storage and stores in the storage file.
ReplaceAllAWSRegionsAndStore(AWS storagetype.AWSRegions) error
}
type AWSRegionStorageImpl struct {
AWSData storagetype.AWSRegionData
storage Storage[storagetype.AWSRegionData]
mu sync.RWMutex
}
// NewSDAWSRegionStorage creates a new AWSRegionStorageImpl with initial configuration from a file path.
func NewSDAWSRegionStorage(path string) (SDAWStorage, error) {
fs := &fileStorage[storagetype.AWSRegionData]{path}
if err := fs.initFile(); err != nil {
return nil, err
}
AWSData, err := fs.Get()
if err != nil {
return nil, err
}
return &AWSRegionStorageImpl{
storage: fs,
AWSData: AWSData,
}, nil
}
func (cs *AWSRegionStorageImpl) GetAWSRegions() storagetype.AWSRegions {
cs.mu.RLock()
defer cs.mu.RUnlock()
return cs.AWSData.AWSRegions
}
func (cs *AWSRegionStorageImpl) Load() error {
cs.mu.Lock()
defer cs.mu.Unlock()
AWS, err := cs.load()
if err != nil {
return err
}
cs.AWSData = AWS
return nil
}
func (cs *AWSRegionStorageImpl) Store() error {
cs.mu.Lock()
defer cs.mu.Unlock()
if err := cs.store(); err != nil {
return err
}
return nil
}
func (cs *AWSRegionStorageImpl) AddAWSRegionAndStore(region *models.AwsRegion) error {
cs.mu.Lock()
defer cs.mu.Unlock()
cs.addAWSRegion(region)
// In case something went wrong whil storing, delete the AWS from the store
if err := cs.store(); err != nil {
cs.removeAWSRegion(region)
return err
}
return nil
}
func (cs *AWSRegionStorageImpl) AddAWSRegionsAndStore(regions storagetype.AWSRegions) error {
cs.mu.Lock()
defer cs.mu.Unlock()
cs.addAWSRegions(regions)
// In case something went wrong whil storing, delete the AWS from the store
if err := cs.store(); err != nil {
cs.removeAWSRegions(regions)
return err
}
return nil
}
func (cs *AWSRegionStorageImpl) ReplaceAllAWSRegionsAndStore(regions storagetype.AWSRegions) error {
cs.mu.Lock()
defer cs.mu.Unlock()
oldAWS := cs.AWSData.AWSRegions
cs.AWSData.AWSRegions = regions
if err := cs.store(); err != nil {
cs.AWSData.AWSRegions = oldAWS
return err
}
return nil
}
func (cs *AWSRegionStorageImpl) RemoveAWSRegionAndStore(region *models.AwsRegion) error {
cs.mu.Lock()
defer cs.mu.Unlock()
cs.removeAWSRegion(region)
// In case something went wrong while storing, add the AWS to the store
if err := cs.store(); err != nil {
cs.addAWSRegion(region)
return err
}
return nil
}
func (cs *AWSRegionStorageImpl) addAWSRegion(region *models.AwsRegion) {
cs.AWSData.AWSRegions = append(cs.AWSData.AWSRegions, region)
}
func (cs *AWSRegionStorageImpl) addAWSRegions(regions storagetype.AWSRegions) {
cs.AWSData.AWSRegions = append(cs.AWSData.AWSRegions, regions...)
}
func (cs *AWSRegionStorageImpl) removeAWSRegion(region *models.AwsRegion) {
for i, u := range cs.AWSData.AWSRegions {
if u.Name == region.Name {
cs.AWSData.AWSRegions = slices.Delete(cs.AWSData.AWSRegions, i, i+1)
break
}
}
}
func (cs *AWSRegionStorageImpl) removeAWSRegions(regions storagetype.AWSRegions) {
for _, AWS := range cs.AWSData.AWSRegions {
cs.removeAWSRegion(AWS)
}
}
func (cs *AWSRegionStorageImpl) setAWSRegions(regions storagetype.AWSRegions) {
cs.AWSData.AWSRegions = regions
}
func (cs *AWSRegionStorageImpl) store() error {
return cs.storage.Store(cs.AWSData)
}
func (cs *AWSRegionStorageImpl) load() (storagetype.AWSRegionData, error) {
return cs.storage.Get()
}