/
modelmachineswatcher.go
79 lines (71 loc) · 2.76 KB
/
modelmachineswatcher.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
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package common
import (
"fmt"
"time"
apiservererrors "github.com/juju/juju/apiserver/errors"
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/rpc/params"
"github.com/juju/juju/state"
"github.com/juju/juju/state/watcher"
)
// watchMachinesQuiesceInterval specifies the time window for batching together
// changes to life and agent start times when watching the machine collection
// for a particular model. For more information, see the WatchModelMachineStartTimes
// method in state/watcher.go.
const watchMachinesQuiesceInterval = 500 * time.Millisecond
// ModelMachinesWatcher implements a common WatchModelMachines
// method for use by various facades.
type ModelMachinesWatcher struct {
st state.ModelMachinesWatcher
resources facade.Resources
authorizer facade.Authorizer
}
// NewModelMachinesWatcher returns a new ModelMachinesWatcher. The
// GetAuthFunc will be used on each invocation of WatchUnits to
// determine current permissions.
func NewModelMachinesWatcher(st state.ModelMachinesWatcher, resources facade.Resources, authorizer facade.Authorizer) *ModelMachinesWatcher {
return &ModelMachinesWatcher{
st: st,
resources: resources,
authorizer: authorizer,
}
}
// WatchModelMachines returns a StringsWatcher that notifies of
// changes to the life cycles of the top level machines in the current
// model.
func (e *ModelMachinesWatcher) WatchModelMachines() (params.StringsWatchResult, error) {
result := params.StringsWatchResult{}
if !e.authorizer.AuthController() {
return result, apiservererrors.ErrPerm
}
watch := e.st.WatchModelMachines()
// Consume the initial event and forward it to the result.
if changes, ok := <-watch.Changes(); ok {
result.StringsWatcherId = e.resources.Register(watch)
result.Changes = changes
} else {
err := watcher.EnsureErr(watch)
return result, fmt.Errorf("cannot obtain initial model machines: %v", err)
}
return result, nil
}
// WatchModelMachineStartTimes watches the non-container machines in the model
// for changes to the Life or AgentStartTime fields and reports them as a batch.
func (e *ModelMachinesWatcher) WatchModelMachineStartTimes() (params.StringsWatchResult, error) {
result := params.StringsWatchResult{}
if !e.authorizer.AuthController() {
return result, apiservererrors.ErrPerm
}
watch := e.st.WatchModelMachineStartTimes(watchMachinesQuiesceInterval)
// Consume the initial event and forward it to the result.
if changes, ok := <-watch.Changes(); ok {
result.StringsWatcherId = e.resources.Register(watch)
result.Changes = changes
} else {
err := watcher.EnsureErr(watch)
return result, fmt.Errorf("cannot obtain initial model machines: %v", err)
}
return result, nil
}