github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/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 (environProvider) PrepareForCreateEnvironment(cfg *config.Config) (*config.Config, error) {
    44  	// Make any necessary updates to the config. This needs to happen
    45  	// before any defaults are applied.
    46  	updates, err := parseOSEnv()
    47  	if err != nil {
    48  		return nil, errors.Trace(err)
    49  	}
    50  	cfg, err = cfg.Apply(updates)
    51  	if err != nil {
    52  		return nil, errors.Trace(err)
    53  	}
    54  	return cfg, nil
    55  }
    56  
    57  // RestrictedConfigAttributes is specified in the EnvironProvider interface.
    58  func (environProvider) RestrictedConfigAttributes() []string {
    59  	return []string{
    60  		cfgPrivateKey,
    61  		cfgClientID,
    62  		cfgClientEmail,
    63  		cfgRegion,
    64  		cfgProjectID,
    65  		cfgImageEndpoint,
    66  	}
    67  }
    68  
    69  // Validate implements environs.EnvironProvider.
    70  func (environProvider) Validate(cfg, old *config.Config) (valid *config.Config, err error) {
    71  	if old == nil {
    72  		ecfg, err := newValidConfig(cfg, configDefaults)
    73  		if err != nil {
    74  			return nil, errors.Annotate(err, "invalid config")
    75  		}
    76  		return ecfg.Config, nil
    77  	}
    78  
    79  	// The defaults should be set already, so we pass nil.
    80  	ecfg, err := newValidConfig(old, nil)
    81  	if err != nil {
    82  		return nil, errors.Annotate(err, "invalid base config")
    83  	}
    84  
    85  	if err := ecfg.update(cfg); err != nil {
    86  		return nil, errors.Annotate(err, "invalid config change")
    87  	}
    88  
    89  	return ecfg.Config, nil
    90  }
    91  
    92  // SecretAttrs implements environs.EnvironProvider.
    93  func (environProvider) SecretAttrs(cfg *config.Config) (map[string]string, error) {
    94  	// The defaults should be set already, so we pass nil.
    95  	ecfg, err := newValidConfig(cfg, nil)
    96  	if err != nil {
    97  		return nil, errors.Trace(err)
    98  	}
    99  	return ecfg.secret(), nil
   100  }
   101  
   102  // BoilerplateConfig implements environs.EnvironProvider.
   103  func (environProvider) BoilerplateConfig() string {
   104  	// boilerplateConfig is kept in config.go, in the hope that people editing
   105  	// config will keep it up to date.
   106  	return boilerplateConfig
   107  }