github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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  	"github.com/juju/jsonschema"
     9  	"github.com/juju/schema"
    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  	"github.com/juju/juju/environs/context"
    16  )
    17  
    18  const (
    19  	// provider version 1 introduces labels for disks,
    20  	// for associating them with a model and controller.
    21  	providerVersion1 = 1
    22  
    23  	currentProviderVersion = providerVersion1
    24  )
    25  
    26  type environProvider struct {
    27  	environProviderCredentials
    28  }
    29  
    30  var providerInstance environProvider
    31  
    32  // Version is part of the EnvironProvider interface.
    33  func (environProvider) Version() int {
    34  	return currentProviderVersion
    35  }
    36  
    37  // Open implements environs.EnvironProvider.
    38  func (environProvider) Open(args environs.OpenParams) (environs.Environ, error) {
    39  	if err := validateCloudSpec(args.Cloud); err != nil {
    40  		return nil, errors.Annotate(err, "validating cloud spec")
    41  	}
    42  	env, err := newEnviron(args.Cloud, args.Config)
    43  	return env, errors.Trace(err)
    44  }
    45  
    46  // CloudSchema returns the schema used to validate input for add-cloud.  Since
    47  // this provider does not support custom clouds, this always returns nil.
    48  func (p environProvider) CloudSchema() *jsonschema.Schema {
    49  	return nil
    50  }
    51  
    52  // Ping tests the connection to the cloud, to verify the endpoint is valid.
    53  func (p environProvider) Ping(ctx context.ProviderCallContext, endpoint string) error {
    54  	return errors.NotImplementedf("Ping")
    55  }
    56  
    57  // PrepareConfig implements environs.EnvironProvider.
    58  func (p environProvider) PrepareConfig(args environs.PrepareConfigParams) (*config.Config, error) {
    59  	if err := validateCloudSpec(args.Cloud); err != nil {
    60  		return nil, errors.Annotate(err, "validating cloud spec")
    61  	}
    62  	return configWithDefaults(args.Config)
    63  }
    64  
    65  func validateCloudSpec(spec environs.CloudSpec) error {
    66  	if err := spec.Validate(); err != nil {
    67  		return errors.Trace(err)
    68  	}
    69  	if spec.Credential == nil {
    70  		return errors.NotValidf("missing credential")
    71  	}
    72  	switch authType := spec.Credential.AuthType(); authType {
    73  	case cloud.OAuth2AuthType, cloud.JSONFileAuthType:
    74  	default:
    75  		return errors.NotSupportedf("%q auth-type", authType)
    76  	}
    77  	return nil
    78  }
    79  
    80  // Schema returns the configuration schema for an environment.
    81  func (environProvider) Schema() environschema.Fields {
    82  	fields, err := config.Schema(configSchema)
    83  	if err != nil {
    84  		panic(err)
    85  	}
    86  	return fields
    87  }
    88  
    89  // ConfigSchema returns extra config attributes specific
    90  // to this provider only.
    91  func (p environProvider) ConfigSchema() schema.Fields {
    92  	return configFields
    93  }
    94  
    95  // ConfigDefaults returns the default values for the
    96  // provider specific config attributes.
    97  func (p environProvider) ConfigDefaults() schema.Defaults {
    98  	return configDefaults
    99  }
   100  
   101  // UpgradeModelConfig is specified in the ModelConfigUpgrader interface.
   102  func (environProvider) UpgradeConfig(cfg *config.Config) (*config.Config, error) {
   103  	return configWithDefaults(cfg)
   104  }
   105  
   106  func configWithDefaults(cfg *config.Config) (*config.Config, error) {
   107  	defaults := make(map[string]interface{})
   108  	if _, ok := cfg.StorageDefaultBlockSource(); !ok {
   109  		// Set the default block source.
   110  		defaults[config.StorageDefaultBlockSourceKey] = storageProviderType
   111  	}
   112  	if len(defaults) == 0 {
   113  		return cfg, nil
   114  	}
   115  	return cfg.Apply(defaults)
   116  }
   117  
   118  // Validate implements environs.EnvironProvider.Validate.
   119  func (environProvider) Validate(cfg, old *config.Config) (*config.Config, error) {
   120  	newCfg, err := newConfig(cfg, old)
   121  	if err != nil {
   122  		return nil, errors.Annotate(err, "invalid config")
   123  	}
   124  	return newCfg.config, nil
   125  }