-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifold.go
110 lines (96 loc) · 2.89 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
// Copyright 2020 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package caasapplicationprovisioner
import (
"github.com/juju/clock"
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/worker/v3"
"github.com/juju/worker/v3/dependency"
"github.com/juju/juju/api/base"
apicaasapplicationprovisioner "github.com/juju/juju/api/caasapplicationprovisioner"
caasunitprovisionerapi "github.com/juju/juju/api/caasunitprovisioner"
"github.com/juju/juju/caas"
)
// Logger represents the methods used by the worker to log details.
type Logger interface {
Debugf(string, ...interface{})
Infof(string, ...interface{})
Errorf(string, ...interface{})
Warningf(string, ...interface{})
Tracef(string, ...interface{})
Child(string) loggo.Logger
}
// ManifoldConfig defines a CAAS operator provisioner's dependencies.
type ManifoldConfig struct {
APICallerName string
BrokerName string
ClockName string
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.ClockName == "" {
return errors.NotValidf("empty ClockName")
}
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 clock clock.Clock
if err := context.Get(config.ClockName, &clock); 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)
}
modelTag, ok := apiCaller.ModelTag()
if !ok {
return nil, errors.New("API connection is controller-only (should never happen)")
}
w, err := config.NewWorker(Config{
Facade: apicaasapplicationprovisioner.NewClient(apiCaller),
Broker: broker,
ModelTag: modelTag,
Clock: clock,
Logger: config.Logger,
NewAppWorker: NewAppWorker,
UnitFacade: caasunitprovisionerapi.NewClient(apiCaller),
})
if err != nil {
return nil, errors.Trace(err)
}
return w, nil
}
// Manifold creates a manifold that runs a CAAS operator 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,
config.ClockName,
},
Start: config.start,
}
}