/
environmachineswatcher.go
51 lines (45 loc) · 1.58 KB
/
environmachineswatcher.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
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package common
import (
"fmt"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/state"
"github.com/juju/juju/state/watcher"
)
// EnvironMachinesWatcher implements a common WatchEnvironMachines
// method for use by various facades.
type EnvironMachinesWatcher struct {
st state.EnvironMachinesWatcher
resources *Resources
authorizer Authorizer
}
// NewEnvironMachinesWatcher returns a new EnvironMachinesWatcher. The
// GetAuthFunc will be used on each invocation of WatchUnits to
// determine current permissions.
func NewEnvironMachinesWatcher(st state.EnvironMachinesWatcher, resources *Resources, authorizer Authorizer) *EnvironMachinesWatcher {
return &EnvironMachinesWatcher{
st: st,
resources: resources,
authorizer: authorizer,
}
}
// WatchEnvironMachines returns a StringsWatcher that notifies of
// changes to the life cycles of the top level machines in the current
// environment.
func (e *EnvironMachinesWatcher) WatchEnvironMachines() (params.StringsWatchResult, error) {
result := params.StringsWatchResult{}
if !e.authorizer.AuthEnvironManager() {
return result, ErrPerm
}
watch := e.st.WatchEnvironMachines()
// 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 environment machines: %v", err)
}
return result, nil
}