github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/provider/ec2/config.go (about) 1 // Copyright 2011, 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package ec2 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 "vpc-id": { 17 Description: "Use a specific AWS VPC ID (optional). When not specified, Juju requires a default VPC or EC2-Classic features to be available for the account/region.", 18 Example: "vpc-a1b2c3d4", 19 Type: environschema.Tstring, 20 Group: environschema.AccountGroup, 21 Immutable: true, 22 }, 23 "vpc-id-force": { 24 Description: "Force Juju to use the AWS VPC ID specified with vpc-id, when it fails the minimum validation criteria. Not accepted without vpc-id", 25 Type: environschema.Tbool, 26 Group: environschema.AccountGroup, 27 Immutable: true, 28 }, 29 } 30 31 var configFields = func() schema.Fields { 32 fs, _, err := configSchema.ValidationSchema() 33 if err != nil { 34 panic(err) 35 } 36 return fs 37 }() 38 39 var configDefaults = schema.Defaults{ 40 "vpc-id": "", 41 "vpc-id-force": false, 42 } 43 44 type environConfig struct { 45 *config.Config 46 attrs map[string]interface{} 47 } 48 49 func (c *environConfig) vpcID() string { 50 return c.attrs["vpc-id"].(string) 51 } 52 53 func (c *environConfig) forceVPCID() bool { 54 return c.attrs["vpc-id-force"].(bool) 55 } 56 57 func (p environProvider) newConfig(cfg *config.Config) (*environConfig, error) { 58 valid, err := p.Validate(cfg, nil) 59 if err != nil { 60 return nil, err 61 } 62 return &environConfig{valid, valid.UnknownAttrs()}, nil 63 } 64 65 // Schema returns the configuration schema for an environment. 66 func (environProvider) Schema() environschema.Fields { 67 fields, err := config.Schema(configSchema) 68 if err != nil { 69 panic(err) 70 } 71 return fields 72 } 73 74 // ConfigSchema returns extra config attributes specific 75 // to this provider only. 76 func (p environProvider) ConfigSchema() schema.Fields { 77 return configFields 78 } 79 80 // ConfigDefaults returns the default values for the 81 // provider specific config attributes. 82 func (p environProvider) ConfigDefaults() schema.Defaults { 83 return configDefaults 84 } 85 86 func validateConfig(cfg, old *config.Config) (*environConfig, error) { 87 // Check for valid changes for the base config values. 88 if err := config.Validate(cfg, old); err != nil { 89 return nil, err 90 } 91 validated, err := cfg.ValidateUnknownAttrs(configFields, configDefaults) 92 if err != nil { 93 return nil, err 94 } 95 ecfg := &environConfig{cfg, validated} 96 97 if vpcID := ecfg.vpcID(); isVPCIDSetButInvalid(vpcID) { 98 return nil, fmt.Errorf("vpc-id: %q is not a valid AWS VPC ID", vpcID) 99 } else if !isVPCIDSet(vpcID) && ecfg.forceVPCID() { 100 return nil, fmt.Errorf("cannot use vpc-id-force without specifying vpc-id as well") 101 } 102 103 if old != nil { 104 attrs := old.UnknownAttrs() 105 106 if vpcID, _ := attrs["vpc-id"].(string); vpcID != ecfg.vpcID() { 107 return nil, fmt.Errorf("cannot change vpc-id from %q to %q", vpcID, ecfg.vpcID()) 108 } 109 110 if forceVPCID, _ := attrs["vpc-id-force"].(bool); forceVPCID != ecfg.forceVPCID() { 111 return nil, fmt.Errorf("cannot change vpc-id-force from %v to %v", forceVPCID, ecfg.forceVPCID()) 112 } 113 } 114 115 // ssl-hostname-verification cannot be disabled 116 if !ecfg.SSLHostnameVerification() { 117 return nil, fmt.Errorf("disabling ssh-hostname-verification is not supported") 118 } 119 return ecfg, nil 120 }