forked from juju/juju
/
config.go
142 lines (122 loc) · 4.38 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package openstack
import (
"fmt"
"github.com/juju/schema"
"gopkg.in/juju/environschema.v1"
"github.com/juju/juju/environs/config"
)
var configSchema = environschema.Fields{
"use-floating-ip": {
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.",
Type: environschema.Tbool,
},
"use-default-secgroup": {
Description: `Whether new machine instances should have the "default" Openstack security group assigned.`,
Type: environschema.Tbool,
},
"network": {
Description: "The network label or UUID to bring machines up on when multiple networks exist.",
Type: environschema.Tstring,
},
"external-network": {
Description: "The network label or UUID to create floating IP addresses on when multiple external networks exist.",
Type: environschema.Tstring,
},
}
var configDefaults = schema.Defaults{
"use-floating-ip": false,
"use-default-secgroup": false,
"network": "",
"external-network": "",
}
var configFields = func() schema.Fields {
fs, _, err := configSchema.ValidationSchema()
if err != nil {
panic(err)
}
return fs
}()
type environConfig struct {
*config.Config
attrs map[string]interface{}
}
func (c *environConfig) useFloatingIP() bool {
return c.attrs["use-floating-ip"].(bool)
}
func (c *environConfig) useDefaultSecurityGroup() bool {
return c.attrs["use-default-secgroup"].(bool)
}
func (c *environConfig) network() string {
return c.attrs["network"].(string)
}
func (c *environConfig) externalNetwork() string {
return c.attrs["external-network"].(string)
}
type AuthMode string
const (
AuthKeyPair AuthMode = "keypair"
AuthLegacy AuthMode = "legacy"
AuthUserPass AuthMode = "userpass"
)
// 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 (p EnvironProvider) Validate(cfg, old *config.Config) (valid *config.Config, err 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, p.Configurator.GetConfigDefaults())
if err != nil {
return nil, err
}
ecfg := &environConfig{cfg, validated}
// Check for deprecated fields and log a warning. We also print to stderr to ensure the user sees the message
// even if they are not running with --debug.
cfgAttrs := cfg.AllAttrs()
if defaultImageId := cfgAttrs["default-image-id"]; defaultImageId != nil && defaultImageId.(string) != "" {
msg := fmt.Sprintf(
"Config attribute %q (%v) is deprecated and ignored.\n"+
"Your cloud provider should have set up image metadata to provide the correct image id\n"+
"for your chosen series and architecture. If this is a private Openstack deployment without\n"+
"existing image metadata, please run 'juju-metadata help' to see how suitable image"+
"metadata can be generated.",
"default-image-id", defaultImageId)
logger.Warningf(msg)
}
if defaultInstanceType := cfgAttrs["default-instance-type"]; defaultInstanceType != nil && defaultInstanceType.(string) != "" {
msg := fmt.Sprintf(
"Config attribute %q (%v) is deprecated and ignored.\n"+
"The correct instance flavor is determined using constraints, globally specified\n"+
"when an model is bootstrapped, or individually when a charm is deployed.\n"+
"See 'juju help bootstrap' or 'juju help deploy'.",
"default-instance-type", defaultInstanceType)
logger.Warningf(msg)
}
// Construct a new config with the deprecated attributes removed.
for _, attr := range []string{"default-image-id", "default-instance-type"} {
delete(cfgAttrs, attr)
delete(ecfg.attrs, attr)
}
for k, v := range ecfg.attrs {
cfgAttrs[k] = v
}
return config.New(config.NoDefaults, cfgAttrs)
}