github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/cloud/whitelist.go (about)

     1  // Copyright 2019 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package cloud
     5  
     6  import (
     7  	"fmt"
     8  	"sort"
     9  	"strings"
    10  
    11  	"github.com/juju/collections/set"
    12  	"github.com/juju/errors"
    13  
    14  	"github.com/juju/juju/provider/lxd/lxdnames"
    15  )
    16  
    17  // WhiteList contains a cloud compatibility matrix:
    18  // if controller was bootstrapped on a particular cloud type,
    19  // what other cloud types can be added to it.
    20  type WhiteList struct {
    21  	matrix map[string]set.Strings
    22  }
    23  
    24  // String constructs user friendly white list representation.
    25  func (w *WhiteList) String() string {
    26  	if len(w.matrix) == 0 {
    27  		return "empty whitelist"
    28  	}
    29  	sorted := []string{}
    30  	for one := range w.matrix {
    31  		sorted = append(sorted, one)
    32  	}
    33  	sort.Strings(sorted)
    34  	result := []string{}
    35  	for _, one := range sorted {
    36  		result = append(result, fmt.Sprintf(" - controller cloud type %q supports %v", one, w.matrix[one].SortedValues()))
    37  	}
    38  	return strings.Join(result, "\n")
    39  }
    40  
    41  // Check will err out if 'existing' controller cloud type is
    42  // not compatible with a 'new' cloud type according to this white list.
    43  func (w *WhiteList) Check(existing, new string) error {
    44  	if list, ok := w.matrix[existing]; ok {
    45  		if !list.Contains(new) {
    46  			return errors.Errorf("cloud type %q is not whitelisted for controller cloud type %q, current whitelist: %v", new, existing, list.SortedValues())
    47  		}
    48  		return nil
    49  	}
    50  	return errors.Errorf("controller cloud type %q is not whitelisted, current whitelist: \n%v", existing, w)
    51  }
    52  
    53  // CurrentWhiteList returns current clouds whitelist supported by Juju.
    54  func CurrentWhiteList() *WhiteList {
    55  	return &WhiteList{map[string]set.Strings{
    56  		"kubernetes":          set.NewStrings(lxdnames.ProviderType, "maas", "openstack"),
    57  		lxdnames.ProviderType: set.NewStrings(lxdnames.ProviderType, "maas", "openstack"),
    58  		"maas":                set.NewStrings("maas", "openstack"),
    59  		"openstack":           set.NewStrings("openstack"),
    60  	}}
    61  }