github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/provider/openstack/config.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package openstack
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/schema"
    10  	"gopkg.in/juju/environschema.v1"
    11  
    12  	"github.com/juju/juju/environs/config"
    13  )
    14  
    15  var configSchema = environschema.Fields{
    16  	"use-floating-ip": {
    17  		Description: "Whether a floating IP address is required to give the nodes a public IP address. Some installations assign public IP addresses by default without requiring a floating IP address.",
    18  		Type:        environschema.Tbool,
    19  	},
    20  	"use-default-secgroup": {
    21  		Description: `Whether new machine instances should have the "default" Openstack security group assigned.`,
    22  		Type:        environschema.Tbool,
    23  	},
    24  	"network": {
    25  		Description: "The network label or UUID to bring machines up on when multiple networks exist.",
    26  		Type:        environschema.Tstring,
    27  	},
    28  }
    29  
    30  var configDefaults = schema.Defaults{
    31  	"use-floating-ip":      false,
    32  	"use-default-secgroup": false,
    33  	"network":              "",
    34  }
    35  
    36  var configFields = func() schema.Fields {
    37  	fs, _, err := configSchema.ValidationSchema()
    38  	if err != nil {
    39  		panic(err)
    40  	}
    41  	return fs
    42  }()
    43  
    44  type environConfig struct {
    45  	*config.Config
    46  	attrs map[string]interface{}
    47  }
    48  
    49  func (c *environConfig) useFloatingIP() bool {
    50  	return c.attrs["use-floating-ip"].(bool)
    51  }
    52  
    53  func (c *environConfig) useDefaultSecurityGroup() bool {
    54  	return c.attrs["use-default-secgroup"].(bool)
    55  }
    56  
    57  func (c *environConfig) network() string {
    58  	return c.attrs["network"].(string)
    59  }
    60  
    61  type AuthMode string
    62  
    63  const (
    64  	AuthKeyPair  AuthMode = "keypair"
    65  	AuthLegacy   AuthMode = "legacy"
    66  	AuthUserPass AuthMode = "userpass"
    67  )
    68  
    69  // Schema returns the configuration schema for an environment.
    70  func (EnvironProvider) Schema() environschema.Fields {
    71  	fields, err := config.Schema(configSchema)
    72  	if err != nil {
    73  		panic(err)
    74  	}
    75  	return fields
    76  }
    77  
    78  // ConfigSchema returns extra config attributes specific
    79  // to this provider only.
    80  func (p EnvironProvider) ConfigSchema() schema.Fields {
    81  	return configFields
    82  }
    83  
    84  // ConfigDefaults returns the default values for the
    85  // provider specific config attributes.
    86  func (p EnvironProvider) ConfigDefaults() schema.Defaults {
    87  	return configDefaults
    88  }
    89  
    90  func (p EnvironProvider) Validate(cfg, old *config.Config) (valid *config.Config, err error) {
    91  	// Check for valid changes for the base config values.
    92  	if err := config.Validate(cfg, old); err != nil {
    93  		return nil, err
    94  	}
    95  
    96  	validated, err := cfg.ValidateUnknownAttrs(configFields, p.Configurator.GetConfigDefaults())
    97  	if err != nil {
    98  		return nil, err
    99  	}
   100  	ecfg := &environConfig{cfg, validated}
   101  
   102  	// Check for deprecated fields and log a warning. We also print to stderr to ensure the user sees the message
   103  	// even if they are not running with --debug.
   104  	cfgAttrs := cfg.AllAttrs()
   105  	if defaultImageId := cfgAttrs["default-image-id"]; defaultImageId != nil && defaultImageId.(string) != "" {
   106  		msg := fmt.Sprintf(
   107  			"Config attribute %q (%v) is deprecated and ignored.\n"+
   108  				"Your cloud provider should have set up image metadata to provide the correct image id\n"+
   109  				"for your chosen series and architecture. If this is a private Openstack deployment without\n"+
   110  				"existing image metadata, please run 'juju-metadata help' to see how suitable image"+
   111  				"metadata can be generated.",
   112  			"default-image-id", defaultImageId)
   113  		logger.Warningf(msg)
   114  	}
   115  	if defaultInstanceType := cfgAttrs["default-instance-type"]; defaultInstanceType != nil && defaultInstanceType.(string) != "" {
   116  		msg := fmt.Sprintf(
   117  			"Config attribute %q (%v) is deprecated and ignored.\n"+
   118  				"The correct instance flavor is determined using constraints, globally specified\n"+
   119  				"when an model is bootstrapped, or individually when a charm is deployed.\n"+
   120  				"See 'juju help bootstrap' or 'juju help deploy'.",
   121  			"default-instance-type", defaultInstanceType)
   122  		logger.Warningf(msg)
   123  	}
   124  	// Construct a new config with the deprecated attributes removed.
   125  	for _, attr := range []string{"default-image-id", "default-instance-type"} {
   126  		delete(cfgAttrs, attr)
   127  		delete(ecfg.attrs, attr)
   128  	}
   129  	for k, v := range ecfg.attrs {
   130  		cfgAttrs[k] = v
   131  	}
   132  	return config.New(config.NoDefaults, cfgAttrs)
   133  }