github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/provider/vsphere/provider.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 // +build !gccgo 5 6 package vsphere 7 8 import ( 9 "github.com/juju/errors" 10 "github.com/juju/loggo" 11 12 "github.com/juju/juju/cloud" 13 "github.com/juju/juju/environs" 14 "github.com/juju/juju/environs/config" 15 ) 16 17 type environProvider struct { 18 environProviderCredentials 19 } 20 21 var providerInstance = environProvider{} 22 var _ environs.EnvironProvider = providerInstance 23 24 var logger = loggo.GetLogger("juju.provider.vmware") 25 26 // Open implements environs.EnvironProvider. 27 func (environProvider) Open(args environs.OpenParams) (environs.Environ, error) { 28 if err := validateCloudSpec(args.Cloud); err != nil { 29 return nil, errors.Annotate(err, "validating cloud spec") 30 } 31 env, err := newEnviron(args.Cloud, args.Config) 32 return env, errors.Trace(err) 33 } 34 35 // PrepareConfig implements environs.EnvironProvider. 36 func (p environProvider) PrepareConfig(args environs.PrepareConfigParams) (*config.Config, error) { 37 if err := validateCloudSpec(args.Cloud); err != nil { 38 return nil, errors.Annotate(err, "validating cloud spec") 39 } 40 return args.Config, nil 41 } 42 43 // Validate implements environs.EnvironProvider. 44 func (environProvider) Validate(cfg, old *config.Config) (valid *config.Config, err error) { 45 if old == nil { 46 ecfg, err := newValidConfig(cfg, configDefaults) 47 if err != nil { 48 return nil, errors.Annotate(err, "invalid config") 49 } 50 return ecfg.Config, nil 51 } 52 53 // The defaults should be set already, so we pass nil. 54 ecfg, err := newValidConfig(old, nil) 55 if err != nil { 56 return nil, errors.Annotate(err, "invalid base config") 57 } 58 59 if err := ecfg.update(cfg); err != nil { 60 return nil, errors.Annotate(err, "invalid config change") 61 } 62 63 return ecfg.Config, nil 64 } 65 66 func validateCloudSpec(spec environs.CloudSpec) error { 67 if err := spec.Validate(); err != nil { 68 return errors.Trace(err) 69 } 70 // TODO(axw) add validation of endpoint/region. 71 if spec.Credential == nil { 72 return errors.NotValidf("missing credential") 73 } 74 if authType := spec.Credential.AuthType(); authType != cloud.UserPassAuthType { 75 return errors.NotSupportedf("%q auth-type", authType) 76 } 77 return nil 78 }