github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/environs/environ.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package environs
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"gopkg.in/juju/names.v2"
     9  
    10  	"github.com/juju/juju/environs/config"
    11  )
    12  
    13  // EnvironConfigGetter exposes a model configuration to its clients.
    14  type EnvironConfigGetter interface {
    15  	ModelConfig() (*config.Config, error)
    16  	CloudSpec(names.ModelTag) (CloudSpec, error)
    17  }
    18  
    19  // NewEnvironFunc is the type of a function that, given a model config,
    20  // returns an Environ. This will typically be environs.New.
    21  type NewEnvironFunc func(OpenParams) (Environ, error)
    22  
    23  // GetEnviron returns the environs.Environ ("provider") associated
    24  // with the model.
    25  func GetEnviron(st EnvironConfigGetter, newEnviron NewEnvironFunc) (Environ, error) {
    26  	modelConfig, err := st.ModelConfig()
    27  	if err != nil {
    28  		return nil, errors.Trace(err)
    29  	}
    30  	cloudSpec, err := st.CloudSpec(names.NewModelTag(modelConfig.UUID()))
    31  	if err != nil {
    32  		return nil, errors.Trace(err)
    33  	}
    34  	env, err := newEnviron(OpenParams{
    35  		Cloud:  cloudSpec,
    36  		Config: modelConfig,
    37  	})
    38  	if err != nil {
    39  		return nil, errors.Trace(err)
    40  	}
    41  	return env, nil
    42  }