github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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/cloud" 10 "github.com/juju/juju/environs" 11 "github.com/juju/juju/environs/config" 12 ) 13 14 type environProvider struct { 15 environProviderCredentials 16 } 17 18 var providerInstance environProvider 19 20 // Open implements environs.EnvironProvider. 21 func (environProvider) Open(cfg *config.Config) (environs.Environ, error) { 22 env, err := newEnviron(cfg) 23 return env, errors.Trace(err) 24 } 25 26 // BootstrapConfig implements environs.EnvironProvider. 27 func (p environProvider) BootstrapConfig(args environs.BootstrapConfigParams) (*config.Config, error) { 28 // Add credentials to the configuration. 29 cfg := args.Config 30 switch authType := args.Credentials.AuthType(); authType { 31 case cloud.JSONFileAuthType: 32 var err error 33 filename := args.Credentials.Attributes()["file"] 34 args.Credentials, err = parseJSONAuthFile(filename) 35 if err != nil { 36 return nil, errors.Trace(err) 37 } 38 fallthrough 39 case cloud.OAuth2AuthType: 40 credentialAttrs := args.Credentials.Attributes() 41 var err error 42 cfg, err = args.Config.Apply(map[string]interface{}{ 43 "project-id": credentialAttrs["project-id"], 44 "client-id": credentialAttrs["client-id"], 45 "client-email": credentialAttrs["client-email"], 46 "private-key": credentialAttrs["private-key"], 47 }) 48 if err != nil { 49 return nil, errors.Trace(err) 50 } 51 default: 52 return nil, errors.NotSupportedf("%q auth-type", authType) 53 } 54 return p.PrepareForCreateEnvironment(cfg) 55 } 56 57 // PrepareForBootstrap implements environs.EnvironProvider. 58 func (p environProvider) PrepareForBootstrap(ctx environs.BootstrapContext, cfg *config.Config) (environs.Environ, error) { 59 env, err := newEnviron(cfg) 60 if err != nil { 61 return nil, errors.Trace(err) 62 } 63 if ctx.ShouldVerifyCredentials() { 64 if err := env.gce.VerifyCredentials(); err != nil { 65 return nil, errors.Trace(err) 66 } 67 } 68 return env, nil 69 } 70 71 // PrepareForCreateEnvironment is specified in the EnvironProvider interface. 72 func (p environProvider) PrepareForCreateEnvironment(cfg *config.Config) (*config.Config, error) { 73 return configWithDefaults(cfg) 74 } 75 76 // UpgradeModelConfig is specified in the ModelConfigUpgrader interface. 77 func (environProvider) UpgradeConfig(cfg *config.Config) (*config.Config, error) { 78 return configWithDefaults(cfg) 79 } 80 81 func configWithDefaults(cfg *config.Config) (*config.Config, error) { 82 defaults := make(map[string]interface{}) 83 if _, ok := cfg.StorageDefaultBlockSource(); !ok { 84 // Set the default block source. 85 defaults[config.StorageDefaultBlockSourceKey] = storageProviderType 86 } 87 if len(defaults) == 0 { 88 return cfg, nil 89 } 90 return cfg.Apply(defaults) 91 } 92 93 // RestrictedConfigAttributes is specified in the EnvironProvider interface. 94 func (environProvider) RestrictedConfigAttributes() []string { 95 return []string{ 96 cfgPrivateKey, 97 cfgClientID, 98 cfgClientEmail, 99 cfgRegion, 100 cfgProjectID, 101 cfgImageEndpoint, 102 } 103 } 104 105 // Validate implements environs.EnvironProvider. 106 func (environProvider) Validate(cfg, old *config.Config) (valid *config.Config, err error) { 107 if old == nil { 108 ecfg, err := newValidConfig(cfg, configDefaults) 109 if err != nil { 110 return nil, errors.Annotate(err, "invalid config") 111 } 112 return ecfg.Config, nil 113 } 114 115 // The defaults should be set already, so we pass nil. 116 ecfg, err := newValidConfig(old, nil) 117 if err != nil { 118 return nil, errors.Annotate(err, "invalid base config") 119 } 120 121 if err := ecfg.update(cfg); err != nil { 122 return nil, errors.Annotate(err, "invalid config change") 123 } 124 125 return ecfg.Config, nil 126 } 127 128 // SecretAttrs implements environs.EnvironProvider. 129 func (environProvider) SecretAttrs(cfg *config.Config) (map[string]string, error) { 130 // The defaults should be set already, so we pass nil. 131 ecfg, err := newValidConfig(cfg, nil) 132 if err != nil { 133 return nil, errors.Trace(err) 134 } 135 return ecfg.secret(), nil 136 }