forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanifold.go
98 lines (83 loc) · 2.38 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
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package caasunitprovisioner
import (
"github.com/juju/errors"
"gopkg.in/juju/worker.v1"
"gopkg.in/juju/worker.v1/dependency"
"github.com/juju/juju/api/base"
"github.com/juju/juju/caas"
)
// Logger represents the methods used by the worker to log details.
type Logger interface {
Debugf(string, ...interface{})
Warningf(string, ...interface{})
Errorf(string, ...interface{})
}
// ManifoldConfig defines a CAAS unit provisioner's dependencies.
type ManifoldConfig struct {
APICallerName string
BrokerName string
NewClient func(base.APICaller) Client
NewWorker func(Config) (worker.Worker, error)
Logger Logger
}
// Validate is called by start to check for bad configuration.
func (config ManifoldConfig) Validate() error {
if config.APICallerName == "" {
return errors.NotValidf("empty APICallerName")
}
if config.BrokerName == "" {
return errors.NotValidf("empty BrokerName")
}
if config.NewClient == nil {
return errors.NotValidf("nil NewClient")
}
if config.NewWorker == nil {
return errors.NotValidf("nil NewWorker")
}
if config.Logger == nil {
return errors.NotValidf("nil Logger")
}
return nil
}
func (config ManifoldConfig) start(context dependency.Context) (worker.Worker, error) {
if err := config.Validate(); err != nil {
return nil, errors.Trace(err)
}
var apiCaller base.APICaller
if err := context.Get(config.APICallerName, &apiCaller); err != nil {
return nil, errors.Trace(err)
}
var broker caas.Broker
if err := context.Get(config.BrokerName, &broker); err != nil {
return nil, errors.Trace(err)
}
client := config.NewClient(apiCaller)
w, err := config.NewWorker(Config{
ApplicationGetter: client,
ApplicationUpdater: client,
ServiceBroker: broker,
ContainerBroker: broker,
ProvisioningInfoGetter: client,
ProvisioningStatusSetter: client,
LifeGetter: client,
UnitUpdater: client,
Logger: config.Logger,
})
if err != nil {
return nil, errors.Trace(err)
}
return w, nil
}
// Manifold creates a manifold that runs a CAAS unit provisioner. See the
// ManifoldConfig type for discussion about how this can/should evolve.
func Manifold(config ManifoldConfig) dependency.Manifold {
return dependency.Manifold{
Inputs: []string{
config.APICallerName,
config.BrokerName,
},
Start: config.start,
}
}