github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/provider/maas/environprovider.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package maas
     5  
     6  import (
     7  	"errors"
     8  
     9  	"github.com/juju/loggo"
    10  	"github.com/juju/utils"
    11  
    12  	"github.com/juju/juju/environs"
    13  	"github.com/juju/juju/environs/config"
    14  )
    15  
    16  // Logger for the MAAS provider.
    17  var logger = loggo.GetLogger("juju.provider.maas")
    18  
    19  type maasEnvironProvider struct{}
    20  
    21  var _ environs.EnvironProvider = (*maasEnvironProvider)(nil)
    22  
    23  var providerInstance maasEnvironProvider
    24  
    25  func init() {
    26  	environs.RegisterProvider("maas", maasEnvironProvider{})
    27  }
    28  
    29  func (maasEnvironProvider) Open(cfg *config.Config) (environs.Environ, error) {
    30  	logger.Debugf("opening environment %q.", cfg.Name())
    31  	env, err := NewEnviron(cfg)
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  	return env, nil
    36  }
    37  
    38  var errAgentNameAlreadySet = errors.New(
    39  	"maas-agent-name is already set; this should not be set by hand")
    40  
    41  func (p maasEnvironProvider) Prepare(ctx environs.BootstrapContext, cfg *config.Config) (environs.Environ, error) {
    42  	attrs := cfg.UnknownAttrs()
    43  	oldName, found := attrs["maas-agent-name"]
    44  	if found && oldName != "" {
    45  		return nil, errAgentNameAlreadySet
    46  	}
    47  	uuid, err := utils.NewUUID()
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	attrs["maas-agent-name"] = uuid.String()
    52  	cfg, err = cfg.Apply(attrs)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  	return p.Open(cfg)
    57  }
    58  
    59  // Boilerplate config YAML.  Don't mess with the indentation or add newlines!
    60  var boilerplateYAML = `
    61  # https://juju.ubuntu.com/docs/config-maas.html
    62  maas:
    63      type: maas
    64  
    65      # maas-server specifies the location of the MAAS server. It must
    66      # specify the base path.
    67      #
    68      maas-server: 'http://192.168.1.1/MAAS/'
    69  
    70      # maas-oauth holds the OAuth credentials from MAAS.
    71      #
    72      maas-oauth: '<add your OAuth credentials from MAAS here>'
    73  
    74  `[1:]
    75  
    76  // BoilerplateConfig is specified in the EnvironProvider interface.
    77  func (maasEnvironProvider) BoilerplateConfig() string {
    78  	return boilerplateYAML
    79  }
    80  
    81  // SecretAttrs is specified in the EnvironProvider interface.
    82  func (prov maasEnvironProvider) SecretAttrs(cfg *config.Config) (map[string]string, error) {
    83  	secretAttrs := make(map[string]string)
    84  	maasCfg, err := prov.newConfig(cfg)
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  	secretAttrs["maas-oauth"] = maasCfg.maasOAuth()
    89  	return secretAttrs, nil
    90  }