github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/provider/cloudsigma/environ.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package cloudsigma
     5  
     6  import (
     7  	"sync"
     8  
     9  	"github.com/altoros/gosigma"
    10  	"github.com/juju/errors"
    11  	"github.com/juju/version"
    12  
    13  	"github.com/juju/juju/core/instance"
    14  	"github.com/juju/juju/environs"
    15  	"github.com/juju/juju/environs/config"
    16  	"github.com/juju/juju/environs/context"
    17  	"github.com/juju/juju/environs/simplestreams"
    18  	"github.com/juju/juju/provider/common"
    19  )
    20  
    21  const (
    22  	CloudsigmaCloudImagesURLTemplate = "https://%v.cloudsigma.com/"
    23  )
    24  
    25  // This file contains the core of the Environ implementation.
    26  type environ struct {
    27  	name   string
    28  	cloud  environs.CloudSpec
    29  	client *environClient
    30  	lock   sync.Mutex
    31  	ecfg   *environConfig
    32  }
    33  
    34  // Name returns the Environ's name.
    35  func (env *environ) Name() string {
    36  	return env.name
    37  }
    38  
    39  // Provider returns the EnvironProvider that created this Environ.
    40  func (*environ) Provider() environs.EnvironProvider {
    41  	return providerInstance
    42  }
    43  
    44  // SetConfig updates the Environ's configuration.
    45  //
    46  // Calls to SetConfig do not affect the configuration of values previously obtained
    47  // from Storage.
    48  func (env *environ) SetConfig(cfg *config.Config) error {
    49  	env.lock.Lock()
    50  	defer env.lock.Unlock()
    51  
    52  	ecfg, err := validateConfig(cfg, env.ecfg)
    53  	if err != nil {
    54  		return errors.Trace(err)
    55  	}
    56  	env.ecfg = ecfg
    57  
    58  	return nil
    59  }
    60  
    61  // Config returns the configuration data with which the Environ was created.
    62  // Note that this is not necessarily current; the canonical location
    63  // for the configuration data is stored in the state.
    64  func (env *environ) Config() *config.Config {
    65  	return env.ecfg.Config
    66  }
    67  
    68  // PrepareForBootstrap is part of the Environ interface.
    69  func (env *environ) PrepareForBootstrap(ctx environs.BootstrapContext) error {
    70  	logger.Infof("preparing model %q", env.name)
    71  	return nil
    72  }
    73  
    74  // Create is part of the Environ interface.
    75  func (env *environ) Create(context.ProviderCallContext, environs.CreateParams) error {
    76  	return nil
    77  }
    78  
    79  // Bootstrap initializes the state for the environment, possibly
    80  // starting one or more instances.  If the configuration's
    81  // AdminSecret is non-empty, the administrator password on the
    82  // newly bootstrapped state will be set to a hash of it (see
    83  // utils.PasswordHash), When first connecting to the
    84  // environment via the juju package, the password hash will be
    85  // automatically replaced by the real password.
    86  //
    87  // The supplied constraints are used to choose the initial instance
    88  // specification, and will be stored in the new environment's state.
    89  //
    90  // Bootstrap is responsible for selecting the appropriate tools,
    91  // and setting the agent-version configuration attribute prior to
    92  // bootstrapping the environment.
    93  func (env *environ) Bootstrap(ctx environs.BootstrapContext, callCtx context.ProviderCallContext, params environs.BootstrapParams) (*environs.BootstrapResult, error) {
    94  	return common.Bootstrap(ctx, env, callCtx, params)
    95  }
    96  
    97  // ControllerInstances is part of the Environ interface.
    98  func (e *environ) ControllerInstances(ctx context.ProviderCallContext, controllerUUID string) ([]instance.Id, error) {
    99  	return e.client.getControllerIds()
   100  }
   101  
   102  // AdoptResources is part of the Environ interface.
   103  func (e *environ) AdoptResources(ctx context.ProviderCallContext, controllerUUID string, fromVersion version.Number) error {
   104  	// This provider doesn't track instance -> controller.
   105  	return nil
   106  }
   107  
   108  // Destroy shuts down all known machines and destroys the
   109  // rest of the environment. Note that on some providers,
   110  // very recently started instances may not be destroyed
   111  // because they are not yet visible.
   112  //
   113  // When Destroy has been called, any Environ referring to the
   114  // same remote environment may become invalid
   115  func (env *environ) Destroy(ctx context.ProviderCallContext) error {
   116  	// You can probably ignore this method; the common implementation should work.
   117  	return common.Destroy(env, ctx)
   118  }
   119  
   120  // DestroyController implements the Environ interface.
   121  func (env *environ) DestroyController(ctx context.ProviderCallContext, controllerUUID string) error {
   122  	// TODO(wallyworld): destroy hosted model resources
   123  	return env.Destroy(ctx)
   124  }
   125  
   126  // PrecheckInstance performs a preflight check on the specified
   127  // series and constraints, ensuring that they are possibly valid for
   128  // creating an instance in this environment.
   129  //
   130  // PrecheckInstance is best effort, and not guaranteed to eliminate
   131  // all invalid parameters. If PrecheckInstance returns nil, it is not
   132  // guaranteed that the constraints are valid; if a non-nil error is
   133  // returned, then the constraints are definitely invalid.
   134  func (env *environ) PrecheckInstance(context.ProviderCallContext, environs.PrecheckInstanceParams) error {
   135  	return nil
   136  }
   137  
   138  // Region is specified in the HasRegion interface.
   139  func (env *environ) Region() (simplestreams.CloudSpec, error) {
   140  	return simplestreams.CloudSpec{
   141  		Region:   env.cloud.Region,
   142  		Endpoint: env.cloud.Endpoint,
   143  	}, nil
   144  }
   145  
   146  func (env *environ) MetadataLookupParams(region string) (*simplestreams.MetadataLookupParams, error) {
   147  	env.lock.Lock()
   148  	defer env.lock.Unlock()
   149  	return &simplestreams.MetadataLookupParams{
   150  		Region:   region,
   151  		Endpoint: gosigma.ResolveEndpoint(region),
   152  		Series:   config.PreferredSeries(env.ecfg),
   153  	}, nil
   154  }