github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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  	"github.com/juju/schema"
    11  	"gopkg.in/juju/environschema.v1"
    12  
    13  	"github.com/juju/juju/cloud"
    14  	"github.com/juju/juju/environs"
    15  	"github.com/juju/juju/environs/config"
    16  	"github.com/juju/juju/provider/lxd/lxdnames"
    17  )
    18  
    19  type environProvider struct {
    20  	environProviderCredentials
    21  }
    22  
    23  var providerInstance environProvider
    24  
    25  // Open implements environs.EnvironProvider.
    26  func (environProvider) Open(args environs.OpenParams) (environs.Environ, error) {
    27  	if err := validateCloudSpec(args.Cloud); err != nil {
    28  		return nil, errors.Annotate(err, "validating cloud spec")
    29  	}
    30  	// TODO(ericsnow) verify prerequisites (see provider/local/prereq.go)?
    31  	env, err := newEnviron(args.Cloud, args.Config, newRawProvider)
    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 _, err := newValidConfig(cfg); err != nil {
    46  		return nil, errors.Annotate(err, "invalid base config")
    47  	}
    48  	return cfg, nil
    49  }
    50  
    51  // DetectRegions implements environs.CloudRegionDetector.
    52  func (environProvider) DetectRegions() ([]cloud.Region, error) {
    53  	// For now we just return a hard-coded "localhost" region,
    54  	// i.e. the local LXD daemon. We may later want to detect
    55  	// locally-configured remotes.
    56  	return []cloud.Region{{Name: lxdnames.DefaultRegion}}, nil
    57  }
    58  
    59  // Schema returns the configuration schema for an environment.
    60  func (environProvider) Schema() environschema.Fields {
    61  	fields, err := config.Schema(configSchema)
    62  	if err != nil {
    63  		panic(err)
    64  	}
    65  	return fields
    66  }
    67  
    68  func validateCloudSpec(spec environs.CloudSpec) error {
    69  	if err := spec.Validate(); err != nil {
    70  		return errors.Trace(err)
    71  	}
    72  	if spec.Endpoint != "" {
    73  		return errors.NotValidf("non-empty endpoint %q", spec.Endpoint)
    74  	}
    75  	if spec.Credential != nil {
    76  		if authType := spec.Credential.AuthType(); authType != cloud.EmptyAuthType {
    77  			return errors.NotSupportedf("%q auth-type", authType)
    78  		}
    79  	}
    80  	return nil
    81  }
    82  
    83  // ConfigSchema returns extra config attributes specific
    84  // to this provider only.
    85  func (p environProvider) ConfigSchema() schema.Fields {
    86  	return configFields
    87  }
    88  
    89  // ConfigDefaults returns the default values for the
    90  // provider specific config attributes.
    91  func (p environProvider) ConfigDefaults() schema.Defaults {
    92  	return configDefaults
    93  }