-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenviron.go
55 lines (45 loc) · 1.68 KB
/
environ.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
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package environs
import (
"context"
"github.com/juju/errors"
environscloudspec "github.com/juju/juju/environs/cloudspec"
"github.com/juju/juju/environs/config"
)
// EnvironConfigGetter exposes a model configuration to its clients.
type EnvironConfigGetter interface {
ModelConfig() (*config.Config, error)
CloudSpec() (environscloudspec.CloudSpec, error)
}
// NewEnvironFunc is the type of a function that, given a model config,
// returns an Environ. This will typically be environs.New.
type NewEnvironFunc func(context.Context, OpenParams) (Environ, error)
// GetEnviron returns the environs.Environ ("provider") associated
// with the model.
func GetEnviron(st EnvironConfigGetter, newEnviron NewEnvironFunc) (Environ, error) {
env, _, err := GetEnvironAndCloud(st, newEnviron)
return env, err
}
// GetEnvironAndCloud returns the environs.Environ ("provider") and cloud associated
// with the model.
func GetEnvironAndCloud(st EnvironConfigGetter, newEnviron NewEnvironFunc) (Environ, *environscloudspec.CloudSpec, error) {
modelConfig, err := st.ModelConfig()
if err != nil {
return nil, nil, errors.Annotate(err, "retrieving model config")
}
cloudSpec, err := st.CloudSpec()
if err != nil {
return nil, nil, errors.Annotatef(
err, "retrieving cloud spec for model %q (%s)", modelConfig.Name(), modelConfig.UUID())
}
env, err := newEnviron(context.TODO(), OpenParams{
Cloud: cloudSpec,
Config: modelConfig,
})
if err != nil {
return nil, nil, errors.Annotatef(
err, "creating environ for model %q (%s)", modelConfig.Name(), modelConfig.UUID())
}
return env, &cloudSpec, nil
}