1
// Copyright 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
7
"launchpad.net/juju-core/state"
8
"launchpad.net/juju-core/state/api/params"
9
"launchpad.net/juju-core/state/watcher"
12
// AgentEntityWatcher implements a common Watch method for use by
14
type AgentEntityWatcher struct {
17
getCanWatch GetAuthFunc
20
// NewAgentEntityWatcher returns a new AgentEntityWatcher. The
21
// GetAuthFunc will be used on each invocation of Watch to determine
22
// current permissions.
23
func NewAgentEntityWatcher(st state.EntityFinder, resources *Resources, getCanWatch GetAuthFunc) *AgentEntityWatcher {
24
return &AgentEntityWatcher{
27
getCanWatch: getCanWatch,
31
func (a *AgentEntityWatcher) watchEntity(tag string) (string, error) {
32
entity0, err := a.st.FindEntity(tag)
36
entity, ok := entity0.(state.NotifyWatcherFactory)
38
return "", NotSupportedError(tag, "watching")
40
watch := entity.Watch()
41
// Consume the initial event. Technically, API
42
// calls to Watch 'transmit' the initial event
43
// in the Watch response. But NotifyWatchers
44
// have no state to transmit.
45
if _, ok := <-watch.Changes(); ok {
46
return a.resources.Register(watch), nil
48
return "", watcher.MustErr(watch)
51
// Watch starts an NotifyWatcher for each given entity.
52
func (a *AgentEntityWatcher) Watch(args params.Entities) (params.NotifyWatchResults, error) {
53
result := params.NotifyWatchResults{
54
Results: make([]params.NotifyWatchResult, len(args.Entities)),
56
if len(args.Entities) == 0 {
59
canWatch, err := a.getCanWatch()
61
return params.NotifyWatchResults{}, err
63
for i, entity := range args.Entities {
66
if canWatch(entity.Tag) {
67
watcherId, err = a.watchEntity(entity.Tag)
69
result.Results[i].NotifyWatcherId = watcherId
70
result.Results[i].Error = ServerError(err)