1
// Copyright 2014 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
7
"github.com/juju/errors"
8
"github.com/juju/schema"
9
"gopkg.in/juju/environschema.v1"
11
"github.com/juju/juju/environs/config"
14
// TODO(ericsnow) While not strictly config-related, we could use some
15
// mechanism by which we can validate the values we've hard-coded in
16
// this provider match up with the external authoritative sources. One
17
// example of this is the data stored in instancetypes.go. Similarly
18
// we should also ensure the cloud-images metadata is correct and
19
// up-to-date, though that is more the responsibility of that team.
20
// Regardless, it may be useful to include a tool somewhere in juju
21
// that we can use to validate this provider's potentially out-of-date
24
var configSchema = environschema.Fields{}
26
// configFields is the spec for each GCE config value's type.
27
var configFields = func() schema.Fields {
28
fs, _, err := configSchema.ValidationSchema()
35
var configImmutableFields = []string{}
37
var configDefaults = schema.Defaults{}
39
type environConfig struct {
41
attrs map[string]interface{}
44
// newConfig builds a new environConfig from the provided Config
45
// filling in default values, if any. It returns an error if the
46
// resulting configuration is not valid.
47
func newConfig(cfg, old *config.Config) (*environConfig, error) {
48
// Ensure that the provided config is valid.
49
if err := config.Validate(cfg, old); err != nil {
50
return nil, errors.Trace(err)
52
attrs, err := cfg.ValidateUnknownAttrs(configFields, configDefaults)
54
return nil, errors.Trace(err)
58
// There's an old configuration. Validate it so that any
59
// default values are correctly coerced for when we check
60
// the old values later.
61
oldEcfg, err := newConfig(old, nil)
63
return nil, errors.Annotatef(err, "invalid base config")
65
for _, attr := range configImmutableFields {
66
oldv, newv := oldEcfg.attrs[attr], attrs[attr]
68
return nil, errors.Errorf(
69
"%s: cannot change from %v to %v",
76
ecfg := &environConfig{