github.com/mwhudson/juju@v0.0.0-20160512215208-90ff01f3497f/storage/provider/registry/providerregistry.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package registry 5 6 import ( 7 "github.com/juju/errors" 8 9 "github.com/juju/juju/storage" 10 "github.com/juju/juju/storage/provider" 11 ) 12 13 // 14 // A registry of storage providers. 15 // 16 17 // providers maps from provider type to storage.Provider for 18 // each registered provider type. 19 var providers = make(map[storage.ProviderType]storage.Provider) 20 21 // RegisterProvider registers a new storage provider of the given type. 22 // 23 // If the provider is nil, then any previously registered provider with 24 // the same type will be unregistered; this is purely available for 25 // testing. 26 func RegisterProvider(providerType storage.ProviderType, p storage.Provider) { 27 if p == nil { 28 delete(providers, providerType) 29 return 30 } 31 if providers[providerType] != nil { 32 panic(errors.Errorf("juju: duplicate storage provider type %q", providerType)) 33 } 34 providers[providerType] = p 35 } 36 37 // StorageProvider returns the previously registered provider with the given type. 38 func StorageProvider(providerType storage.ProviderType) (storage.Provider, error) { 39 p, ok := providers[providerType] 40 if !ok { 41 return nil, errors.NotFoundf("storage provider %q", providerType) 42 } 43 return p, nil 44 } 45 46 // 47 // A registry of storage provider types which are 48 // valid for a Juju Environ. 49 // 50 51 // supportedEnvironProviders maps from environment type to a slice of 52 // supported ProviderType(s). 53 var supportedEnvironProviders = make(map[string][]storage.ProviderType) 54 55 // ResetEnvironStorageProviders clears out the supported storage providers for 56 // the specified environment type. This is provided for testing purposes. 57 func ResetEnvironStorageProviders(envType string) { 58 delete(supportedEnvironProviders, envType) 59 } 60 61 // RegisterEnvironStorageProviders records which storage provider types 62 // are valid for an environment. 63 // This is to be called from the environ provider's init(). 64 // Also registered will be provider types common to all environments. 65 func RegisterEnvironStorageProviders(envType string, providers ...storage.ProviderType) { 66 existing := supportedEnvironProviders[envType] 67 for _, p := range providers { 68 if IsProviderSupported(envType, p) { 69 continue 70 } 71 existing = append(existing, p) 72 } 73 74 // Add the common providers. 75 for p := range provider.CommonProviders() { 76 if IsProviderSupported(envType, p) { 77 continue 78 } 79 existing = append(existing, p) 80 } 81 supportedEnvironProviders[envType] = existing 82 } 83 84 // Returns true is provider is supported for the environment. 85 func IsProviderSupported(envType string, providerType storage.ProviderType) bool { 86 providerTypes, ok := EnvironStorageProviders(envType) 87 if !ok { 88 return false 89 } 90 for _, p := range providerTypes { 91 if p == providerType { 92 return true 93 } 94 } 95 return false 96 } 97 98 // EnvironStorageProviders returns storage provider types 99 // for the specified environment. 100 func EnvironStorageProviders(envType string) ([]storage.ProviderType, bool) { 101 providerTypes, ok := supportedEnvironProviders[envType] 102 if !ok { 103 return nil, false 104 } 105 return providerTypes, true 106 }