-
Notifications
You must be signed in to change notification settings - Fork 0
/
whitelist.go
60 lines (52 loc) · 1.8 KB
/
whitelist.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
// Copyright 2019 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package cloud
import (
"fmt"
"sort"
"strings"
"github.com/juju/collections/set"
"github.com/juju/errors"
"github.com/juju/juju/provider/lxd/lxdnames"
)
// WhiteList contains a cloud compatibility matrix:
// if controller was bootstrapped on a particular cloud type,
// what other cloud types can be added to it.
type WhiteList struct {
matrix map[string]set.Strings
}
// String constructs user friendly white list representation.
func (w *WhiteList) String() string {
if len(w.matrix) == 0 {
return "empty whitelist"
}
sorted := []string{}
for one := range w.matrix {
sorted = append(sorted, one)
}
sort.Strings(sorted)
result := []string{}
for _, one := range sorted {
result = append(result, fmt.Sprintf(" - controller cloud type %q supports %v", one, w.matrix[one].SortedValues()))
}
return strings.Join(result, "\n")
}
// Check will err out if 'existing' controller cloud type is
// not compatible with a 'new' cloud type according to this white list.
func (w *WhiteList) Check(existing, new string) error {
if list, ok := w.matrix[existing]; ok {
if !list.Contains(new) {
return errors.Errorf("cloud type %q is not whitelisted for controller cloud type %q, current whitelist: %v", new, existing, list.SortedValues())
}
return nil
}
return errors.Errorf("controller cloud type %q is not whitelisted, current whitelist: \n%v", existing, w)
}
// CurrentWhiteList returns current clouds whitelist supported by Juju.
func CurrentWhiteList() *WhiteList {
return &WhiteList{map[string]set.Strings{
lxdnames.ProviderType: set.NewStrings(lxdnames.ProviderType, "maas", "openstack"),
"maas": set.NewStrings("maas", "openstack"),
"openstack": set.NewStrings("openstack"),
}}
}