github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/provider/manual/config.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package manual
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/schema"
    10  
    11  	"github.com/juju/juju/environs/config"
    12  )
    13  
    14  const defaultStoragePort = 8040
    15  
    16  var (
    17  	configFields = schema.Fields{
    18  		"bootstrap-host":    schema.String(),
    19  		"bootstrap-user":    schema.String(),
    20  		"storage-listen-ip": schema.String(),
    21  		"storage-port":      schema.ForceInt(),
    22  		"storage-auth-key":  schema.String(),
    23  		"use-sshstorage":    schema.Bool(),
    24  	}
    25  	configDefaults = schema.Defaults{
    26  		"bootstrap-user":    "",
    27  		"storage-listen-ip": "",
    28  		"storage-port":      defaultStoragePort,
    29  		"use-sshstorage":    true,
    30  	}
    31  )
    32  
    33  type environConfig struct {
    34  	*config.Config
    35  	attrs map[string]interface{}
    36  }
    37  
    38  func newEnvironConfig(config *config.Config, attrs map[string]interface{}) *environConfig {
    39  	return &environConfig{Config: config, attrs: attrs}
    40  }
    41  
    42  func (c *environConfig) useSSHStorage() bool {
    43  	// Prior to 1.17.3, the use-sshstorage attribute
    44  	// did not exist. We take non-existence to be
    45  	// equivalent to false.
    46  	useSSHStorage, _ := c.attrs["use-sshstorage"].(bool)
    47  	return useSSHStorage
    48  }
    49  
    50  func (c *environConfig) bootstrapHost() string {
    51  	return c.attrs["bootstrap-host"].(string)
    52  }
    53  
    54  func (c *environConfig) bootstrapUser() string {
    55  	return c.attrs["bootstrap-user"].(string)
    56  }
    57  
    58  func (c *environConfig) storageListenIPAddress() string {
    59  	return c.attrs["storage-listen-ip"].(string)
    60  }
    61  
    62  func (c *environConfig) storagePort() int {
    63  	switch val := c.attrs["storage-port"].(type) {
    64  	case float64:
    65  		return int(val)
    66  	case int:
    67  		return val
    68  	default:
    69  		panic(fmt.Sprintf("Unexpected %T in storage-port: %#v. Expected float64 or int.", val, val))
    70  	}
    71  }
    72  
    73  func (c *environConfig) storageAuthKey() string {
    74  	return c.attrs["storage-auth-key"].(string)
    75  }
    76  
    77  // storageAddr returns an address for connecting to the
    78  // bootstrap machine's localstorage.
    79  func (c *environConfig) storageAddr() string {
    80  	return fmt.Sprintf("%s:%d", c.bootstrapHost(), c.storagePort())
    81  }
    82  
    83  // storageListenAddr returns an address for the bootstrap
    84  // machine to listen on for its localstorage.
    85  func (c *environConfig) storageListenAddr() string {
    86  	return fmt.Sprintf("%s:%d", c.storageListenIPAddress(), c.storagePort())
    87  }