github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/core/base/validate.go (about) 1 // Copyright 2019 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package base 5 6 import ( 7 "github.com/juju/errors" 8 ) 9 10 // ValidateBase attempts to validate a base if one is found, otherwise it 11 // uses the fallback base and validates that one. 12 // Returns the base it validated against or an error if one is found. 13 // Note: the selected base will be returned if there is an error to help use 14 // that for a fallback during error scenarios. 15 func ValidateBase(supportedBases []Base, base, fallbackPreferredBase Base) (Base, error) { 16 // Validate the requested base. 17 // Attempt to do the validation in one place, so it makes it easier to 18 // reason about where the validation happens. This only happens for IAAS 19 // models, as CAAS can't take base as an argument. 20 var requestedBase Base 21 if !base.Empty() { 22 requestedBase = base 23 } else { 24 // If no bootstrap base is supplied, go and get that information from 25 // the fallback. We should still validate the fallback value to ensure 26 // that we also work with that base. 27 requestedBase = fallbackPreferredBase 28 } 29 30 var found bool 31 for _, supportedBase := range supportedBases { 32 if supportedBase.IsCompatible(requestedBase) { 33 found = true 34 break 35 } 36 } 37 if !found { 38 return requestedBase, errors.NotSupportedf("%s", requestedBase.String()) 39 } 40 return requestedBase, nil 41 }