github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/provider/gce/provider.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package gce 5 6 import ( 7 "github.com/juju/errors" 8 9 "github.com/juju/juju/environs" 10 "github.com/juju/juju/environs/config" 11 ) 12 13 type environProvider struct{} 14 15 var providerInstance environProvider 16 17 // Open implements environs.EnvironProvider. 18 func (environProvider) Open(cfg *config.Config) (environs.Environ, error) { 19 env, err := newEnviron(cfg) 20 return env, errors.Trace(err) 21 } 22 23 // PrepareForBootstrap implements environs.EnvironProvider. 24 func (p environProvider) PrepareForBootstrap(ctx environs.BootstrapContext, cfg *config.Config) (environs.Environ, error) { 25 cfg, err := p.PrepareForCreateEnvironment(cfg) 26 if err != nil { 27 return nil, errors.Trace(err) 28 } 29 env, err := newEnviron(cfg) 30 if err != nil { 31 return nil, errors.Trace(err) 32 } 33 34 if ctx.ShouldVerifyCredentials() { 35 if err := env.gce.VerifyCredentials(); err != nil { 36 return nil, errors.Trace(err) 37 } 38 } 39 return env, nil 40 } 41 42 // PrepareForCreateEnvironment is specified in the EnvironProvider interface. 43 func (p environProvider) PrepareForCreateEnvironment(cfg *config.Config) (*config.Config, error) { 44 return configWithDefaults(cfg) 45 } 46 47 // UpgradeEnvironConfig is specified in the EnvironConfigUpgrader interface. 48 func (environProvider) UpgradeConfig(cfg *config.Config) (*config.Config, error) { 49 return configWithDefaults(cfg) 50 } 51 52 func configWithDefaults(cfg *config.Config) (*config.Config, error) { 53 defaults := make(map[string]interface{}) 54 if _, ok := cfg.StorageDefaultBlockSource(); !ok { 55 // Set the default block source. 56 defaults[config.StorageDefaultBlockSourceKey] = storageProviderType 57 } 58 if len(defaults) == 0 { 59 return cfg, nil 60 } 61 return cfg.Apply(defaults) 62 } 63 64 // RestrictedConfigAttributes is specified in the EnvironProvider interface. 65 func (environProvider) RestrictedConfigAttributes() []string { 66 return []string{ 67 cfgPrivateKey, 68 cfgClientID, 69 cfgClientEmail, 70 cfgRegion, 71 cfgProjectID, 72 cfgImageEndpoint, 73 } 74 } 75 76 // Validate implements environs.EnvironProvider. 77 func (environProvider) Validate(cfg, old *config.Config) (valid *config.Config, err error) { 78 if old == nil { 79 ecfg, err := newValidConfig(cfg, configDefaults) 80 if err != nil { 81 return nil, errors.Annotate(err, "invalid config") 82 } 83 return ecfg.Config, nil 84 } 85 86 // The defaults should be set already, so we pass nil. 87 ecfg, err := newValidConfig(old, nil) 88 if err != nil { 89 return nil, errors.Annotate(err, "invalid base config") 90 } 91 92 if err := ecfg.update(cfg); err != nil { 93 return nil, errors.Annotate(err, "invalid config change") 94 } 95 96 return ecfg.Config, nil 97 } 98 99 // SecretAttrs implements environs.EnvironProvider. 100 func (environProvider) SecretAttrs(cfg *config.Config) (map[string]string, error) { 101 // The defaults should be set already, so we pass nil. 102 ecfg, err := newValidConfig(cfg, nil) 103 if err != nil { 104 return nil, errors.Trace(err) 105 } 106 return ecfg.secret(), nil 107 } 108 109 // BoilerplateConfig implements environs.EnvironProvider. 110 func (environProvider) BoilerplateConfig() string { 111 // boilerplateConfig is kept in config.go, in the hope that people editing 112 // config will keep it up to date. 113 return boilerplateConfig 114 }