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