-
Notifications
You must be signed in to change notification settings - Fork 492
/
manifold.go
74 lines (65 loc) · 2.21 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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package apiaddressupdater
import (
"github.com/juju/errors"
"github.com/juju/names/v5"
"github.com/juju/worker/v3"
"github.com/juju/worker/v3/dependency"
"github.com/juju/juju/agent"
"github.com/juju/juju/api/agent/caasoperator"
"github.com/juju/juju/api/agent/machiner"
"github.com/juju/juju/api/agent/uniter"
"github.com/juju/juju/api/base"
"github.com/juju/juju/cmd/jujud/agent/engine"
)
// Logger represents the methods used for logging messages.
type Logger interface {
Errorf(string, ...interface{})
Infof(string, ...interface{})
Debugf(string, ...interface{})
Warningf(string, ...interface{})
}
// ManifoldConfig defines the names of the manifolds on which a Manifold will depend.
type ManifoldConfig struct {
AgentName string
APICallerName string
Logger Logger
}
// Manifold returns a dependency manifold that runs an API address updater worker,
// using the resource names defined in the supplied config.
func Manifold(config ManifoldConfig) dependency.Manifold {
typedConfig := engine.AgentAPIManifoldConfig{
AgentName: config.AgentName,
APICallerName: config.APICallerName,
}
return engine.AgentAPIManifold(typedConfig, config.newWorker)
}
// newWorker trivially wraps NewAPIAddressUpdater for use in a engine.AgentAPIManifold.
// It's not tested at the moment, because the scaffolding necessary is too
// unwieldy/distracting to introduce at this point.
func (config ManifoldConfig) newWorker(a agent.Agent, apiCaller base.APICaller) (worker.Worker, error) {
tag := a.CurrentConfig().Tag()
// TODO(fwereade): use appropriate facade!
var facade APIAddresser
switch apiTag := tag.(type) {
case names.UnitTag:
facade = uniter.NewState(apiCaller, apiTag)
case names.ApplicationTag:
facade = caasoperator.NewClient(apiCaller)
case names.MachineTag:
facade = machiner.NewState(apiCaller)
default:
return nil, errors.Errorf("expected a unit or machine tag; got %q", tag)
}
setter := agent.APIHostPortsSetter{Agent: a}
w, err := NewAPIAddressUpdater(Config{
Addresser: facade,
Setter: setter,
Logger: config.Logger,
})
if err != nil {
return nil, errors.Trace(err)
}
return w, nil
}