-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
107 lines (91 loc) · 2.83 KB
/
provider.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
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package gce
import (
"github.com/juju/errors"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/config"
)
type environProvider struct{}
var providerInstance environProvider
// Open implements environs.EnvironProvider.
func (environProvider) Open(cfg *config.Config) (environs.Environ, error) {
env, err := newEnviron(cfg)
return env, errors.Trace(err)
}
// PrepareForBootstrap implements environs.EnvironProvider.
func (p environProvider) PrepareForBootstrap(ctx environs.BootstrapContext, cfg *config.Config) (environs.Environ, error) {
cfg, err := p.PrepareForCreateEnvironment(cfg)
if err != nil {
return nil, errors.Trace(err)
}
env, err := newEnviron(cfg)
if err != nil {
return nil, errors.Trace(err)
}
if ctx.ShouldVerifyCredentials() {
if err := env.gce.VerifyCredentials(); err != nil {
return nil, errors.Trace(err)
}
}
return env, nil
}
// PrepareForCreateEnvironment is specified in the EnvironProvider interface.
func (environProvider) PrepareForCreateEnvironment(cfg *config.Config) (*config.Config, error) {
// Make any necessary updates to the config. This needs to happen
// before any defaults are applied.
updates, err := parseOSEnv()
if err != nil {
return nil, errors.Trace(err)
}
cfg, err = cfg.Apply(updates)
if err != nil {
return nil, errors.Trace(err)
}
return cfg, nil
}
// RestrictedConfigAttributes is specified in the EnvironProvider interface.
func (environProvider) RestrictedConfigAttributes() []string {
return []string{
cfgPrivateKey,
cfgClientID,
cfgClientEmail,
cfgRegion,
cfgProjectID,
cfgImageEndpoint,
}
}
// Validate implements environs.EnvironProvider.
func (environProvider) Validate(cfg, old *config.Config) (valid *config.Config, err error) {
if old == nil {
ecfg, err := newValidConfig(cfg, configDefaults)
if err != nil {
return nil, errors.Annotate(err, "invalid config")
}
return ecfg.Config, nil
}
// The defaults should be set already, so we pass nil.
ecfg, err := newValidConfig(old, nil)
if err != nil {
return nil, errors.Annotate(err, "invalid base config")
}
if err := ecfg.update(cfg); err != nil {
return nil, errors.Annotate(err, "invalid config change")
}
return ecfg.Config, nil
}
// SecretAttrs implements environs.EnvironProvider.
func (environProvider) SecretAttrs(cfg *config.Config) (map[string]string, error) {
// The defaults should be set already, so we pass nil.
ecfg, err := newValidConfig(cfg, nil)
if err != nil {
return nil, errors.Trace(err)
}
return ecfg.secret(), nil
}
// BoilerplateConfig implements environs.EnvironProvider.
func (environProvider) BoilerplateConfig() string {
// boilerplateConfig is kept in config.go, in the hope that people editing
// config will keep it up to date.
return boilerplateConfig
}