-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem.go
480 lines (415 loc) · 12.4 KB
/
mem.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package jujuclient
import (
"sync"
"github.com/juju/collections/set"
"github.com/juju/errors"
cookiejar "github.com/juju/persistent-cookiejar"
"github.com/juju/juju/cloud"
)
// MemStore is an in-memory implementation of ClientStore.
type MemStore struct {
mu sync.Mutex
Controllers map[string]ControllerDetails
CurrentControllerName string
Models map[string]*ControllerModels
Accounts map[string]AccountDetails
Credentials map[string]cloud.CloudCredential
BootstrapConfig map[string]BootstrapConfig
CookieJars map[string]*cookiejar.Jar
}
func NewMemStore() *MemStore {
return &MemStore{
Controllers: make(map[string]ControllerDetails),
Models: make(map[string]*ControllerModels),
Accounts: make(map[string]AccountDetails),
Credentials: make(map[string]cloud.CloudCredential),
BootstrapConfig: make(map[string]BootstrapConfig),
CookieJars: make(map[string]*cookiejar.Jar),
}
}
// AllController implements ControllerGetter.AllController
func (c *MemStore) AllControllers() (map[string]ControllerDetails, error) {
c.mu.Lock()
defer c.mu.Unlock()
result := make(map[string]ControllerDetails)
for name, details := range c.Controllers {
result[name] = details
}
return result, nil
}
// ControllerByName implements ControllerGetter.ControllerByName
func (c *MemStore) ControllerByName(name string) (*ControllerDetails, error) {
c.mu.Lock()
defer c.mu.Unlock()
if err := ValidateControllerName(name); err != nil {
return nil, err
}
if result, ok := c.Controllers[name]; ok {
return &result, nil
}
return nil, errors.NotFoundf("controller %s", name)
}
// ControllerByAPIEndpoints implements ControllersGetter.ControllerByAPIEndpoints
func (c *MemStore) ControllerByAPIEndpoints(endpoints ...string) (*ControllerDetails, string, error) {
c.mu.Lock()
defer c.mu.Unlock()
matchEps := set.NewStrings(endpoints...)
for name, ctrl := range c.Controllers {
if matchEps.Intersection(set.NewStrings(ctrl.APIEndpoints...)).IsEmpty() {
continue
}
return &ctrl, name, nil
}
return nil, "", errors.NotFoundf("controller with API endpoints %v", endpoints)
}
// CurrentController implements ControllerGetter.CurrentController
func (c *MemStore) CurrentController() (string, error) {
c.mu.Lock()
defer c.mu.Unlock()
if c.CurrentControllerName == "" {
return "", errors.NotFoundf("current controller")
}
return c.CurrentControllerName, nil
}
// SetCurrentController implements ControllerUpdater.SetCurrentController
func (c *MemStore) SetCurrentController(name string) error {
c.mu.Lock()
defer c.mu.Unlock()
if err := ValidateControllerName(name); err != nil {
return err
}
if _, ok := c.Controllers[name]; !ok {
return errors.NotFoundf("controller %s", name)
}
c.CurrentControllerName = name
return nil
}
// AddController implements ControllerUpdater.AddController
func (c *MemStore) AddController(name string, one ControllerDetails) error {
c.mu.Lock()
defer c.mu.Unlock()
if err := ValidateControllerName(name); err != nil {
return err
}
if err := ValidateControllerDetails(one); err != nil {
return err
}
if _, ok := c.Controllers[name]; ok {
return errors.AlreadyExistsf("controller with name %s", name)
}
for k, v := range c.Controllers {
if v.ControllerUUID == one.ControllerUUID {
return errors.AlreadyExistsf("controller with UUID %s (%s)",
one.ControllerUUID, k)
}
}
c.Controllers[name] = one
return nil
}
// UpdateController implements ControllerUpdater.UpdateController
func (c *MemStore) UpdateController(name string, one ControllerDetails) error {
c.mu.Lock()
defer c.mu.Unlock()
if err := ValidateControllerName(name); err != nil {
return err
}
if err := ValidateControllerDetails(one); err != nil {
return err
}
if len(c.Controllers) == 0 {
return errors.NotFoundf("controllers")
}
for k, v := range c.Controllers {
if v.ControllerUUID == one.ControllerUUID && k != name {
return errors.AlreadyExistsf("controller %s with UUID %s",
k, v.ControllerUUID)
}
}
if _, ok := c.Controllers[name]; !ok {
return errors.NotFoundf("controller %s", name)
}
c.Controllers[name] = one
return nil
}
// RemoveController implements ControllerRemover.RemoveController
func (c *MemStore) RemoveController(name string) error {
c.mu.Lock()
defer c.mu.Unlock()
if err := ValidateControllerName(name); err != nil {
return err
}
names := set.NewStrings(name)
if namedControllerDetails, ok := c.Controllers[name]; ok {
for name, details := range c.Controllers {
if details.ControllerUUID == namedControllerDetails.ControllerUUID {
names.Add(name)
if name == c.CurrentControllerName {
c.CurrentControllerName = ""
}
}
}
}
for _, name := range names.Values() {
delete(c.Models, name)
delete(c.Accounts, name)
delete(c.BootstrapConfig, name)
delete(c.Controllers, name)
delete(c.CookieJars, name)
}
return nil
}
// UpdateModel implements ModelUpdater.
func (c *MemStore) UpdateModel(controller, model string, details ModelDetails) error {
c.mu.Lock()
defer c.mu.Unlock()
if err := ValidateControllerName(controller); err != nil {
return err
}
if err := ValidateModel(model, details); err != nil {
return err
}
controllerModels, ok := c.Models[controller]
if !ok {
controllerModels = &ControllerModels{
Models: make(map[string]ModelDetails),
}
c.Models[controller] = controllerModels
}
controllerModels.Models[model] = details
return nil
}
// SetModels implements ModelUpdater.
func (c *MemStore) SetModels(controller string, models map[string]ModelDetails) error {
c.mu.Lock()
defer c.mu.Unlock()
if err := ValidateControllerName(controller); err != nil {
return err
}
for modelName, details := range models {
if err := ValidateModel(modelName, details); err != nil {
return errors.Trace(err)
}
}
controllerModels, ok := c.Models[controller]
if !ok {
controllerModels = &ControllerModels{
Models: make(map[string]ModelDetails),
}
c.Models[controller] = controllerModels
}
controllerModels.Models = models
if _, ok := models[controllerModels.CurrentModel]; !ok {
controllerModels.CurrentModel = ""
}
return nil
}
// SetCurrentModel implements ModelUpdater.
func (c *MemStore) SetCurrentModel(controllerName, modelName string) error {
c.mu.Lock()
defer c.mu.Unlock()
if err := ValidateControllerName(controllerName); err != nil {
return errors.Trace(err)
}
controllerModels, ok := c.Models[controllerName]
if !ok {
return errors.NotFoundf("model %s:%s", controllerName, modelName)
}
if modelName == "" {
// We just want to reset
controllerModels.CurrentModel = ""
return nil
}
if err := ValidateModelName(modelName); err != nil {
return errors.Trace(err)
}
if _, ok := controllerModels.Models[modelName]; !ok {
return errors.NotFoundf("model %s:%s", controllerName, modelName)
}
controllerModels.CurrentModel = modelName
return nil
}
// RemoveModel implements ModelRemover.
func (c *MemStore) RemoveModel(controller, model string) error {
c.mu.Lock()
defer c.mu.Unlock()
if err := ValidateControllerName(controller); err != nil {
return err
}
if err := ValidateModelName(model); err != nil {
return err
}
controllerModels, ok := c.Models[controller]
if !ok {
return errors.NotFoundf("models for controller %s", controller)
}
if _, ok := controllerModels.Models[model]; !ok {
return errors.NotFoundf("model %s:%s", controller, model)
}
delete(controllerModels.Models, model)
return nil
}
// AllModels implements ModelGetter.
func (c *MemStore) AllModels(controller string) (map[string]ModelDetails, error) {
c.mu.Lock()
defer c.mu.Unlock()
if err := ValidateControllerName(controller); err != nil {
return nil, err
}
controllerModels, ok := c.Models[controller]
if !ok {
return nil, errors.NotFoundf("models for controller %s", controller)
}
return controllerModels.Models, nil
}
// CurrentModel implements ModelGetter.
func (c *MemStore) CurrentModel(controller string) (string, error) {
c.mu.Lock()
defer c.mu.Unlock()
if err := ValidateControllerName(controller); err != nil {
return "", err
}
controllerModels, ok := c.Models[controller]
if !ok {
return "", errors.NotFoundf("current model for controller %s", controller)
}
if controllerModels.CurrentModel == "" {
return "", errors.NotFoundf("current model for controller %s", controller)
}
return controllerModels.CurrentModel, nil
}
// ModelByName implements ModelGetter.
func (c *MemStore) ModelByName(controller, model string) (*ModelDetails, error) {
c.mu.Lock()
defer c.mu.Unlock()
if err := ValidateControllerName(controller); err != nil {
return nil, err
}
if err := ValidateModelName(model); err != nil {
return nil, err
}
controllerModels, ok := c.Models[controller]
if !ok {
return nil, errors.NotFoundf("model %s:%s", controller, model)
}
details, ok := controllerModels.Models[model]
if !ok {
return nil, errors.NotFoundf("model %s:%s", controller, model)
}
return &details, nil
}
// UpdateAccount implements AccountUpdater.
func (c *MemStore) UpdateAccount(controllerName string, details AccountDetails) error {
c.mu.Lock()
defer c.mu.Unlock()
if err := ValidateControllerName(controllerName); err != nil {
return err
}
if err := ValidateAccountDetails(details); err != nil {
return err
}
oldDetails := c.Accounts[controllerName]
// Only update last known access if it has a value.
if details.LastKnownAccess == "" {
details.LastKnownAccess = oldDetails.LastKnownAccess
}
c.Accounts[controllerName] = details
return nil
}
// AccountDetails implements AccountGetter.
func (c *MemStore) AccountDetails(controllerName string) (*AccountDetails, error) {
c.mu.Lock()
defer c.mu.Unlock()
if err := ValidateControllerName(controllerName); err != nil {
return nil, err
}
details, ok := c.Accounts[controllerName]
if !ok {
return nil, errors.NotFoundf("account for controller %s", controllerName)
}
return &details, nil
}
// RemoveAccount implements AccountRemover.
func (c *MemStore) RemoveAccount(controllerName string) error {
c.mu.Lock()
defer c.mu.Unlock()
if err := ValidateControllerName(controllerName); err != nil {
return err
}
if _, ok := c.Accounts[controllerName]; !ok {
return errors.NotFoundf("account for controller %s", controllerName)
}
delete(c.Accounts, controllerName)
return nil
}
// UpdateCredential implements CredentialsUpdater.
func (c *MemStore) UpdateCredential(cloudName string, details cloud.CloudCredential) error {
c.mu.Lock()
defer c.mu.Unlock()
if len(details.AuthCredentials) > 0 {
c.Credentials[cloudName] = details
} else {
delete(c.Credentials, cloudName)
}
return nil
}
// CredentialForCloud implements CredentialsGetter.
func (c *MemStore) CredentialForCloud(cloudName string) (*cloud.CloudCredential, error) {
c.mu.Lock()
defer c.mu.Unlock()
if result, ok := c.Credentials[cloudName]; ok {
return &result, nil
}
return nil, errors.NotFoundf("credentials for cloud %s", cloudName)
}
// AllCredentials implements CredentialsGetter.
func (c *MemStore) AllCredentials() (map[string]cloud.CloudCredential, error) {
c.mu.Lock()
defer c.mu.Unlock()
result := make(map[string]cloud.CloudCredential)
for k, v := range c.Credentials {
result[k] = v
}
return result, nil
}
// UpdateBootstrapConfig implements BootstrapConfigUpdater.
func (c *MemStore) UpdateBootstrapConfig(controllerName string, cfg BootstrapConfig) error {
c.mu.Lock()
defer c.mu.Unlock()
if err := ValidateControllerName(controllerName); err != nil {
return err
}
if err := ValidateBootstrapConfig(cfg); err != nil {
return err
}
c.BootstrapConfig[controllerName] = cfg
return nil
}
// BootstrapConfigForController implements BootstrapConfigGetter.
func (c *MemStore) BootstrapConfigForController(controllerName string) (*BootstrapConfig, error) {
c.mu.Lock()
defer c.mu.Unlock()
if cfg, ok := c.BootstrapConfig[controllerName]; ok {
return &cfg, nil
}
return nil, errors.NotFoundf("bootstrap config for controller %s", controllerName)
}
func (c *MemStore) CookieJar(controllerName string) (CookieJar, error) {
c.mu.Lock()
defer c.mu.Unlock()
if err := ValidateControllerName(controllerName); err != nil {
return nil, errors.Trace(err)
}
if jar, ok := c.CookieJars[controllerName]; ok {
return jar, nil
}
jar, err := cookiejar.New(&cookiejar.Options{
NoPersist: true,
})
if err != nil {
return nil, errors.Trace(err)
}
c.CookieJars[controllerName] = jar
return jar, nil
}