github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/common/cloud.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package common 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/loggo" 9 10 "github.com/juju/juju/cloud" 11 "github.com/juju/juju/environs" 12 ) 13 14 var logger = loggo.GetLogger("juju.cmd.juju.cloud") 15 16 // CloudOrProvider finds and returns cloud or provider. 17 func CloudOrProvider(cloudName string, cloudByNameFunc func(string) (*cloud.Cloud, error)) (cloud *cloud.Cloud, err error) { 18 if cloud, err = cloudByNameFunc(cloudName); err != nil { 19 if !errors.IsNotFound(err) { 20 return nil, err 21 } 22 builtInProviders := BuiltInProviders() 23 if builtIn, ok := builtInProviders[cloudName]; !ok { 24 return nil, errors.NotValidf("cloud %v", cloudName) 25 } else { 26 cloud = &builtIn 27 } 28 } 29 return cloud, nil 30 } 31 32 // BuiltInProviders returns cloud information for those 33 // providers which are built in to Juju. 34 func BuiltInProviders() map[string]cloud.Cloud { 35 builtIn := make(map[string]cloud.Cloud) 36 for _, name := range cloud.BuiltInProviderNames { 37 provider, err := environs.Provider(name) 38 if err != nil { 39 // Should never happen but it will on go 1.2 40 // because lxd provider is not built. 41 logger.Warningf("cloud %q not available on this platform", name) 42 continue 43 } 44 var regions []cloud.Region 45 if detector, ok := provider.(environs.CloudRegionDetector); ok { 46 regions, err = detector.DetectRegions() 47 if err != nil && !errors.IsNotFound(err) { 48 logger.Warningf("could not detect regions for %q: %v", name, err) 49 } 50 } 51 aCloud := cloud.Cloud{ 52 Type: name, 53 Regions: regions, 54 } 55 schema := provider.CredentialSchemas() 56 for authType := range schema { 57 if authType == cloud.EmptyAuthType { 58 continue 59 } 60 aCloud.AuthTypes = append(aCloud.AuthTypes, authType) 61 } 62 builtIn[name] = aCloud 63 } 64 return builtIn 65 }