/
manifold.go
58 lines (51 loc) · 1.63 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
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package fanconfigurer
import (
"github.com/juju/clock"
"github.com/juju/errors"
"github.com/juju/worker/v3"
"github.com/juju/worker/v3/dependency"
apifanconfigurer "github.com/juju/juju/api/agent/fanconfigurer"
"github.com/juju/juju/api/base"
)
// ManifoldConfig defines the names of the manifolds on which a
// Manifold will depend.
type ManifoldConfig struct {
// These are the dependency resource names.
APICallerName string
Clock clock.Clock
}
// Manifold returns a dependency manifold that runs a fan configurer
// worker, using the resource names defined in the supplied config.
func Manifold(config ManifoldConfig) dependency.Manifold {
return dependency.Manifold{
Inputs: []string{
config.APICallerName,
},
Output: func(in worker.Worker, out interface{}) error {
inWorker, _ := in.(*FanConfigurer)
if inWorker == nil {
return errors.Errorf("in should be a %T; got %T", inWorker, in)
}
switch outPointer := out.(type) {
case *bool:
*outPointer = true
default:
return errors.Errorf("out should be *bool; got %T", out)
}
return nil
},
Start: func(context dependency.Context) (worker.Worker, error) {
var apiCaller base.APICaller
if err := context.Get(config.APICallerName, &apiCaller); err != nil {
return nil, errors.Trace(err)
}
facade := apifanconfigurer.NewFacade(apiCaller)
fanconfigurer, err := NewFanConfigurer(FanConfigurerConfig{
Facade: facade,
}, config.Clock)
return fanconfigurer, errors.Annotate(err, "creating fanconfigurer orchestrator")
},
}
}