github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/provider/lxd/provider.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  // +build go1.3
     5  
     6  package lxd
     7  
     8  import (
     9  	"github.com/juju/errors"
    10  	"gopkg.in/juju/environschema.v1"
    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  
    23  // Open implements environs.EnvironProvider.
    24  func (environProvider) Open(cfg *config.Config) (environs.Environ, error) {
    25  	// TODO(ericsnow) verify prerequisites (see provider/local/prereq.go)?
    26  	// TODO(ericsnow) do something similar to correctLocalhostURLs()
    27  	// (in provider/local/environprovider.go)?
    28  
    29  	env, err := newEnviron(cfg, newRawProvider)
    30  	return env, errors.Trace(err)
    31  }
    32  
    33  // BootstrapConfig implements environs.EnvironProvider.
    34  func (p environProvider) BootstrapConfig(args environs.BootstrapConfigParams) (*config.Config, error) {
    35  	return p.PrepareForCreateEnvironment(args.Config)
    36  }
    37  
    38  // PrepareForBootstrap implements environs.EnvironProvider.
    39  func (p environProvider) PrepareForBootstrap(ctx environs.BootstrapContext, cfg *config.Config) (environs.Environ, error) {
    40  	// TODO(ericsnow) Do some of what happens in local provider's
    41  	// PrepareForBootstrap()? Only if "remote" is local host?
    42  
    43  	env, err := newEnviron(cfg, newRawProvider)
    44  	if err != nil {
    45  		return nil, errors.Trace(err)
    46  	}
    47  
    48  	if ctx.ShouldVerifyCredentials() {
    49  		if err := env.verifyCredentials(); err != nil {
    50  			return nil, errors.Trace(err)
    51  		}
    52  	}
    53  	return env, nil
    54  }
    55  
    56  // PrepareForCreateEnvironment is specified in the EnvironProvider interface.
    57  func (environProvider) PrepareForCreateEnvironment(cfg *config.Config) (*config.Config, error) {
    58  	return cfg, nil
    59  }
    60  
    61  // RestrictedConfigAttributes is specified in the EnvironProvider interface.
    62  func (environProvider) RestrictedConfigAttributes() []string {
    63  	return []string{
    64  		"remote-url",
    65  		"client-cert",
    66  		"client-key",
    67  		"server-cert",
    68  	}
    69  }
    70  
    71  // Validate implements environs.EnvironProvider.
    72  func (environProvider) Validate(cfg, old *config.Config) (valid *config.Config, err error) {
    73  	if old == nil {
    74  		ecfg, err := newValidConfig(cfg, configDefaults)
    75  		if err != nil {
    76  			return nil, errors.Annotate(err, "invalid config")
    77  		}
    78  		return ecfg.Config, nil
    79  	}
    80  
    81  	// The defaults should be set already, so we pass nil.
    82  	ecfg, err := newValidConfig(old, nil)
    83  	if err != nil {
    84  		return nil, errors.Annotate(err, "invalid base config")
    85  	}
    86  
    87  	if err := ecfg.update(cfg); err != nil {
    88  		return nil, errors.Annotate(err, "invalid config change")
    89  	}
    90  
    91  	return ecfg.Config, nil
    92  }
    93  
    94  // SecretAttrs implements environs.EnvironProvider.
    95  func (environProvider) SecretAttrs(cfg *config.Config) (map[string]string, error) {
    96  	// The defaults should be set already, so we pass nil.
    97  	ecfg, err := newValidConfig(cfg, nil)
    98  	if err != nil {
    99  		return nil, errors.Trace(err)
   100  	}
   101  	return ecfg.secret(), nil
   102  }
   103  
   104  // DetectRegions implements environs.CloudRegionDetector.
   105  func (environProvider) DetectRegions() ([]cloud.Region, error) {
   106  	// For now we just return a hard-coded "localhost" region,
   107  	// i.e. the local LXD daemon. We may later want to detect
   108  	// locally-configured remotes.
   109  	return []cloud.Region{{Name: "localhost"}}, nil
   110  }
   111  
   112  // Schema returns the configuration schema for an environment.
   113  func (environProvider) Schema() environschema.Fields {
   114  	fields, err := config.Schema(configSchema)
   115  	if err != nil {
   116  		panic(err)
   117  	}
   118  	return fields
   119  }