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