forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
83 lines (69 loc) · 1.87 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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package lxd
import (
"github.com/juju/errors"
"github.com/juju/schema"
"gopkg.in/juju/environschema.v1"
"github.com/juju/juju/environs/config"
)
var configSchema = environschema.Fields{
"project": {
Description: "The LXD project name to use for Juju's resources.",
Type: environschema.Tstring,
},
}
var configDefaults = schema.Defaults{
"project": "default",
}
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{}
}
// newConfig builds a new environConfig from the provided Config and
// returns it.
func newConfig(cfg *config.Config) *environConfig {
return &environConfig{
Config: cfg,
attrs: cfg.UnknownAttrs(),
}
}
// newValidConfig builds a new environConfig from the provided Config
// and returns it. This includes applying the provided defaults
// values, if any. The resulting config values are validated.
func newValidConfig(cfg *config.Config) (*environConfig, error) {
// Ensure that the provided config is valid.
if err := config.Validate(cfg, nil); err != nil {
return nil, errors.Trace(err)
}
// Build the config.
ecfg := newConfig(cfg)
// Do final (more complex, provider-specific) validation.
if err := ecfg.validate(); err != nil {
return nil, errors.Trace(err)
}
return ecfg, nil
}
// validate validates LXD-specific configuration.
func (c *environConfig) validate() error {
_, err := c.ValidateUnknownAttrs(configFields, configDefaults)
if err != nil {
return errors.Trace(err)
}
// There are currently no known extra fields for LXD
return nil
}
func (c *environConfig) project() string {
project := c.attrs["project"]
if project == nil {
return ""
}
return project.(string)
}