/
apiaddressupdater.go
64 lines (52 loc) · 1.81 KB
/
apiaddressupdater.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
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package apiaddressupdater
import (
"fmt"
"github.com/juju/loggo"
"github.com/juju/juju/instance"
"github.com/juju/juju/state/api/watcher"
"github.com/juju/juju/worker"
)
var logger = loggo.GetLogger("juju.worker.apiaddressupdater")
// APIAddressUpdater is responsible for cleaning up the state.
type APIAddressUpdater struct {
addresser APIAddresser
setter APIAddressSetter
}
// APIAddresser is an interface that is provided to NewAPIAddressUpdater
// which can be used to watch for API address changes.
type APIAddresser interface {
APIHostPorts() ([][]instance.HostPort, error)
WatchAPIHostPorts() (watcher.NotifyWatcher, error)
}
// APIAddressSetter is an interface that is provided to NewAPIAddressUpdater
// whose SetAPIHostPorts method will be invoked whenever address changes occur.
type APIAddressSetter interface {
SetAPIHostPorts(servers [][]instance.HostPort) error
}
// NewAPIAddressUpdater returns a worker.Worker that runs state.Cleanup()
// if the CleanupWatcher signals documents marked for deletion.
func NewAPIAddressUpdater(addresser APIAddresser, setter APIAddressSetter) worker.Worker {
return worker.NewNotifyWorker(&APIAddressUpdater{
addresser: addresser,
setter: setter,
})
}
func (c *APIAddressUpdater) SetUp() (watcher.NotifyWatcher, error) {
return c.addresser.WatchAPIHostPorts()
}
func (c *APIAddressUpdater) Handle() error {
addresses, err := c.addresser.APIHostPorts()
if err != nil {
return fmt.Errorf("error getting addresses: %v", err)
}
if err := c.setter.SetAPIHostPorts(addresses); err != nil {
return fmt.Errorf("error setting addresses: %v", err)
}
logger.Infof("API addresses updated to %q", addresses)
return nil
}
func (c *APIAddressUpdater) TearDown() error {
return nil
}