github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/provider/vmware/provider.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package vmware 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/loggo" 9 10 "github.com/juju/juju/environs" 11 "github.com/juju/juju/environs/config" 12 ) 13 14 type environProvider struct{} 15 16 var providerInstance = environProvider{} 17 var _ environs.EnvironProvider = providerInstance 18 19 var logger = loggo.GetLogger("juju.provider.vmware") 20 21 func init() { 22 // This will only happen in binaries that actually import this provider 23 // somewhere. To enable a provider, import it in the "providers/all" 24 // package; please do *not* import individual providers anywhere else, 25 // except in direct tests for that provider. 26 environs.RegisterProvider("vmware", providerInstance) 27 } 28 29 // Open implements environs.EnvironProvider. 30 func (environProvider) Open(cfg *config.Config) (environs.Environ, error) { 31 // The config will have come from either state or from a config 32 // file. In either case, the original config came from the env from 33 // a previous call to the Prepare method. That means there is no 34 // need to update the config, e.g. with defaults and OS env values 35 // before we validate it, so we pass nil. 36 ecfg, err := newValidConfig(cfg, nil) 37 if err != nil { 38 return nil, errors.Annotate(err, "invalid config") 39 } 40 41 env, err := newEnviron(ecfg) 42 return env, errors.Trace(err) 43 } 44 45 // PrepareForBootstrap implements environs.EnvironProvider. 46 func (p environProvider) PrepareForBootstrap(ctx environs.BootstrapContext, cfg *config.Config) (environs.Environ, error) { 47 ecfg, err := prepareConfig(cfg) 48 if err != nil { 49 return nil, errors.Annotate(err, "invalid config") 50 } 51 52 env, err := newEnviron(ecfg) 53 if err != nil { 54 return nil, errors.Trace(err) 55 } 56 57 if ctx.ShouldVerifyCredentials() { 58 } 59 return env, nil 60 } 61 62 // PrepareForCreateEnvironment is specified in the EnvironProvider interface. 63 func (environProvider) PrepareForCreateEnvironment(cfg *config.Config) (*config.Config, error) { 64 return nil, errors.NotImplementedf("PrepareForCreateEnvironment") 65 } 66 67 // RestrictedConfigAttributes is specified in the EnvironProvider interface. 68 func (environProvider) RestrictedConfigAttributes() []string { 69 return []string{ 70 cfgPassword, 71 } 72 } 73 74 // Validate implements environs.EnvironProvider. 75 func (environProvider) Validate(cfg, old *config.Config) (valid *config.Config, err error) { 76 if old == nil { 77 ecfg, err := newValidConfig(cfg, configDefaults) 78 if err != nil { 79 return nil, errors.Annotate(err, "invalid config") 80 } 81 return ecfg.Config, nil 82 } 83 84 // The defaults should be set already, so we pass nil. 85 ecfg, err := newValidConfig(old, nil) 86 if err != nil { 87 return nil, errors.Annotate(err, "invalid base config") 88 } 89 90 if err := ecfg.update(cfg); err != nil { 91 return nil, errors.Annotate(err, "invalid config change") 92 } 93 94 return ecfg.Config, nil 95 } 96 97 // SecretAttrs implements environs.EnvironProvider. 98 func (environProvider) SecretAttrs(cfg *config.Config) (map[string]string, error) { 99 // The defaults should be set already, so we pass nil. 100 ecfg, err := newValidConfig(cfg, nil) 101 if err != nil { 102 return nil, errors.Trace(err) 103 } 104 return ecfg.secret(), nil 105 } 106 107 // BoilerplateConfig implements environs.EnvironProvider. 108 func (environProvider) BoilerplateConfig() string { 109 // boilerplateConfig is kept in config.go, in the hope that people editing 110 // config will keep it up to date. 111 return boilerplateConfig 112 }