github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/provider/vsphere/environ.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  // +build !gccgo
     5  
     6  package vsphere
     7  
     8  import (
     9  	"sync"
    10  
    11  	"github.com/juju/errors"
    12  
    13  	"github.com/juju/juju/environs"
    14  	"github.com/juju/juju/environs/config"
    15  	"github.com/juju/juju/provider/common"
    16  )
    17  
    18  // TODO(ericsnow) All imports from github.com/juju/govmomi should change
    19  // back to github.com/vmware/govmomi once our min Go is 1.3 or higher.
    20  
    21  // Note: This provider/environment does *not* implement storage.
    22  
    23  type environ struct {
    24  	common.SupportsUnitPlacementPolicy
    25  
    26  	name   string
    27  	ecfg   *environConfig
    28  	client *client
    29  
    30  	lock     sync.Mutex
    31  	archLock sync.Mutex
    32  
    33  	supportedArchitectures []string
    34  }
    35  
    36  func newEnviron(cfg *config.Config) (*environ, error) {
    37  	ecfg, err := newValidConfig(cfg, configDefaults)
    38  	if err != nil {
    39  		return nil, errors.Annotate(err, "invalid config")
    40  	}
    41  
    42  	client, err := newClient(ecfg)
    43  	if err != nil {
    44  		return nil, errors.Annotatef(err, "failed to create new client")
    45  	}
    46  
    47  	env := &environ{
    48  		name:   ecfg.Name(),
    49  		ecfg:   ecfg,
    50  		client: client,
    51  	}
    52  	return env, nil
    53  }
    54  
    55  // Name returns the name of the environment.
    56  func (env *environ) Name() string {
    57  	return env.name
    58  }
    59  
    60  // Provider returns the environment provider that created this env.
    61  func (*environ) Provider() environs.EnvironProvider {
    62  	return providerInstance
    63  }
    64  
    65  // SetConfig updates the env's configuration.
    66  func (env *environ) SetConfig(cfg *config.Config) error {
    67  	env.lock.Lock()
    68  	defer env.lock.Unlock()
    69  
    70  	if env.ecfg == nil {
    71  		return errors.New("cannot set config on uninitialized env")
    72  	}
    73  
    74  	if err := env.ecfg.update(cfg); err != nil {
    75  		return errors.Annotate(err, "invalid config change")
    76  	}
    77  	return nil
    78  }
    79  
    80  // getSnapshot returns a copy of the environment. This is useful for
    81  // ensuring the env you are using does not get changed by other code
    82  // while you are using it.
    83  func (env environ) getSnapshot() *environ {
    84  	return &env
    85  }
    86  
    87  // Config returns the configuration data with which the env was created.
    88  func (env *environ) Config() *config.Config {
    89  	return env.getSnapshot().ecfg.Config
    90  }
    91  
    92  //this variable is exported, because it has to be rewritten in external unit tests
    93  var Bootstrap = common.Bootstrap
    94  
    95  // Bootstrap creates a new instance, chosing the series and arch out of
    96  // available tools. The series and arch are returned along with a func
    97  // that must be called to finalize the bootstrap process by transferring
    98  // the tools and installing the initial juju state server.
    99  func (env *environ) Bootstrap(ctx environs.BootstrapContext, params environs.BootstrapParams) (arch, series string, _ environs.BootstrapFinalizer, _ error) {
   100  	return Bootstrap(ctx, env, params)
   101  }
   102  
   103  //this variable is exported, because it has to be rewritten in external unit tests
   104  var DestroyEnv = common.Destroy
   105  
   106  // Destroy shuts down all known machines and destroys the rest of the
   107  // known environment.
   108  func (env *environ) Destroy() error {
   109  	return DestroyEnv(env)
   110  }