github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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  // BootstrapMessage is part of the Environ interface.
    97  func (env *environ) BootstrapMessage() string {
    98  	return ""
    99  }
   100  
   101  func (e *environ) ControllerInstances(controllerUUID string) ([]instance.Id, error) {
   102  	return e.client.getControllerIds()
   103  }
   104  
   105  // Destroy shuts down all known machines and destroys the
   106  // rest of the environment. Note that on some providers,
   107  // very recently started instances may not be destroyed
   108  // because they are not yet visible.
   109  //
   110  // When Destroy has been called, any Environ referring to the
   111  // same remote environment may become invalid
   112  func (env *environ) Destroy() error {
   113  	// You can probably ignore this method; the common implementation should work.
   114  	return common.Destroy(env)
   115  }
   116  
   117  // DestroyController implements the Environ interface.
   118  func (env *environ) DestroyController(controllerUUID string) error {
   119  	// TODO(wallyworld): destroy hosted model resources
   120  	return env.Destroy()
   121  }
   122  
   123  // PrecheckInstance performs a preflight check on the specified
   124  // series and constraints, ensuring that they are possibly valid for
   125  // creating an instance in this environment.
   126  //
   127  // PrecheckInstance is best effort, and not guaranteed to eliminate
   128  // all invalid parameters. If PrecheckInstance returns nil, it is not
   129  // guaranteed that the constraints are valid; if a non-nil error is
   130  // returned, then the constraints are definitely invalid.
   131  func (env *environ) PrecheckInstance(series string, cons constraints.Value, placement string) error {
   132  	return nil
   133  }
   134  
   135  // Region is specified in the HasRegion interface.
   136  func (env *environ) Region() (simplestreams.CloudSpec, error) {
   137  	return simplestreams.CloudSpec{
   138  		Region:   env.cloud.Region,
   139  		Endpoint: env.cloud.Endpoint,
   140  	}, nil
   141  }
   142  
   143  func (env *environ) MetadataLookupParams(region string) (*simplestreams.MetadataLookupParams, error) {
   144  	env.lock.Lock()
   145  	defer env.lock.Unlock()
   146  	return &simplestreams.MetadataLookupParams{
   147  		Region:   region,
   148  		Endpoint: gosigma.ResolveEndpoint(region),
   149  		Series:   config.PreferredSeries(env.ecfg),
   150  	}, nil
   151  }