/
manifold.go
123 lines (105 loc) · 3.57 KB
/
manifold.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package instancepoller
import (
"github.com/juju/clock"
"github.com/juju/errors"
"github.com/juju/names/v5"
"github.com/juju/worker/v3"
"github.com/juju/worker/v3/dependency"
"github.com/juju/juju/api/base"
"github.com/juju/juju/api/controller/instancepoller"
"github.com/juju/juju/core/instance"
"github.com/juju/juju/core/network"
"github.com/juju/juju/core/watcher"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/context"
"github.com/juju/juju/environs/instances"
"github.com/juju/juju/worker/common"
)
// Logger represents the methods used by the worker to log details.
type Logger interface {
Tracef(string, ...interface{})
Debugf(string, ...interface{})
Infof(string, ...interface{})
Warningf(string, ...interface{})
Errorf(string, ...interface{})
}
// facadeShim wraps an instancepoller API instance and allows us to provide
// methods that return interfaces which we can easily mock in our tests.
type facadeShim struct {
api *instancepoller.API
}
func (s facadeShim) Machine(tag names.MachineTag) (Machine, error) { return s.api.Machine(tag) }
func (s facadeShim) WatchModelMachines() (watcher.StringsWatcher, error) {
return s.api.WatchModelMachines()
}
var errNetworkingNotSupported = errors.NotSupportedf("networking")
// environWithoutNetworking wraps a environs.Environ instance that does not
// support environs.Networking so that calls to NetworkInterfaces always
// return a NotSupported error.
type environWithoutNetworking struct {
env environs.Environ
}
func (e environWithoutNetworking) Instances(ctx context.ProviderCallContext, ids []instance.Id) ([]instances.Instance, error) {
return e.env.Instances(ctx, ids)
}
func (e environWithoutNetworking) NetworkInterfaces(context.ProviderCallContext, []instance.Id) ([]network.InterfaceInfos, error) {
return nil, errNetworkingNotSupported
}
// ManifoldConfig describes the resources used by the instancepoller worker.
type ManifoldConfig struct {
APICallerName string
ClockName string
EnvironName string
Logger Logger
NewCredentialValidatorFacade func(base.APICaller) (common.CredentialAPI, error)
}
func (config ManifoldConfig) start(context dependency.Context) (worker.Worker, error) {
var clock clock.Clock
if err := context.Get(config.ClockName, &clock); err != nil {
return nil, errors.Trace(err)
}
var environ environs.Environ
if err := context.Get(config.EnvironName, &environ); err != nil {
return nil, errors.Trace(err)
}
// If the current environment does not support networking use a shim
// whose NetworkInterfaces method always returns a NotSupported error.
netEnv, supported := environ.(Environ)
if !supported {
netEnv = &environWithoutNetworking{env: environ}
}
var apiCaller base.APICaller
if err := context.Get(config.APICallerName, &apiCaller); err != nil {
return nil, errors.Trace(err)
}
credentialAPI, err := config.NewCredentialValidatorFacade(apiCaller)
if err != nil {
return nil, errors.Trace(err)
}
w, err := NewWorker(Config{
Clock: clock,
Facade: facadeShim{
api: instancepoller.NewAPI(apiCaller),
},
Environ: netEnv,
Logger: config.Logger,
CredentialAPI: credentialAPI,
})
if err != nil {
return nil, errors.Trace(err)
}
return w, nil
}
// Manifold returns a Manifold that encapsulates the instancepoller worker.
func Manifold(config ManifoldConfig) dependency.Manifold {
return dependency.Manifold{
Inputs: []string{
config.APICallerName,
config.EnvironName,
config.ClockName,
},
Start: config.start,
}
}