github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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  	client *client
    28  
    29  	archLock               sync.Mutex // archLock protects access to the following fields.
    30  	supportedArchitectures []string
    31  
    32  	lock sync.Mutex // lock protects access the following fields.
    33  	ecfg *environConfig
    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  // Config returns the configuration data with which the env was created.
    81  func (env *environ) Config() *config.Config {
    82  	env.lock.Lock()
    83  	cfg := env.ecfg.Config
    84  	env.lock.Unlock()
    85  	return cfg
    86  }
    87  
    88  //this variable is exported, because it has to be rewritten in external unit tests
    89  var Bootstrap = common.Bootstrap
    90  
    91  // Bootstrap creates a new instance, chosing the series and arch out of
    92  // available tools. The series and arch are returned along with a func
    93  // that must be called to finalize the bootstrap process by transferring
    94  // the tools and installing the initial juju controller.
    95  func (env *environ) Bootstrap(ctx environs.BootstrapContext, params environs.BootstrapParams) (*environs.BootstrapResult, error) {
    96  	return Bootstrap(ctx, env, params)
    97  }
    98  
    99  //this variable is exported, because it has to be rewritten in external unit tests
   100  var DestroyEnv = common.Destroy
   101  
   102  // Destroy shuts down all known machines and destroys the rest of the
   103  // known environment.
   104  func (env *environ) Destroy() error {
   105  	return DestroyEnv(env)
   106  }