LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2013 Canonical Ltd. | 1 // Copyright 2013 Canonical Ltd. |
2 // Licensed under the AGPLv3, see LICENCE file for details. | 2 // Licensed under the AGPLv3, see LICENCE file for details. |
3 | 3 |
4 package common | 4 package common |
5 | 5 |
6 import ( | 6 import ( |
7 "launchpad.net/juju-core/state" | 7 "launchpad.net/juju-core/state" |
8 "launchpad.net/juju-core/state/api/params" | 8 "launchpad.net/juju-core/state/api/params" |
9 "launchpad.net/juju-core/state/watcher" | 9 "launchpad.net/juju-core/state/watcher" |
10 ) | 10 ) |
11 | 11 |
12 // AgentEntityWatcher implements a common Watch method for use by | 12 // AgentEntityWatcher implements a common Watch method for use by |
13 // various facades. | 13 // various facades. |
14 type AgentEntityWatcher struct { | 14 type AgentEntityWatcher struct { |
15 » st AgentEntityWatcherer | 15 » st state.EntityFinder |
16 resources *Resources | 16 resources *Resources |
17 getCanWatch GetAuthFunc | 17 getCanWatch GetAuthFunc |
18 } | |
19 | |
20 type AgentEntityWatcherer interface { | |
21 // state.State implements AgentEntityWatcher to provide ways for | |
22 // us to call object.Watch (for machines, units, etc). This is | |
23 // used to allow us to test with mocks without having to actually | |
24 // bring up state. | |
25 AgentEntityWatcher(tag string) (state.AgentEntityWatcher, error) | |
26 } | 18 } |
27 | 19 |
28 // NewAgentEntityWatcher returns a new AgentEntityWatcher. The | 20 // NewAgentEntityWatcher returns a new AgentEntityWatcher. The |
29 // GetAuthFunc will be used on each invocation of Watch to determine | 21 // GetAuthFunc will be used on each invocation of Watch to determine |
30 // current permissions. | 22 // current permissions. |
31 func NewAgentEntityWatcher(st AgentEntityWatcherer, resources *Resources, getCan
Watch GetAuthFunc) *AgentEntityWatcher { | 23 func NewAgentEntityWatcher(st state.EntityFinder, resources *Resources, getCanWa
tch GetAuthFunc) *AgentEntityWatcher { |
32 return &AgentEntityWatcher{ | 24 return &AgentEntityWatcher{ |
33 st: st, | 25 st: st, |
34 resources: resources, | 26 resources: resources, |
35 getCanWatch: getCanWatch, | 27 getCanWatch: getCanWatch, |
36 } | 28 } |
37 } | 29 } |
38 | 30 |
39 func (a *AgentEntityWatcher) watchEntity(tag string) (string, error) { | 31 func (a *AgentEntityWatcher) watchEntity(tag string) (string, error) { |
40 » agentEntityWatcher, err := a.st.AgentEntityWatcher(tag) | 32 » entity0, err := a.st.FindEntity(tag) |
41 if err != nil { | 33 if err != nil { |
42 return "", err | 34 return "", err |
43 } | 35 } |
44 » watch := agentEntityWatcher.Watch() | 36 » entity, ok := entity0.(state.NotifyWatcherFactory) |
| 37 » if !ok { |
| 38 » » return "", NotSupportedError(tag, "watching") |
| 39 » } |
| 40 » watch := entity.Watch() |
45 // Consume the initial event. Technically, API | 41 // Consume the initial event. Technically, API |
46 // calls to Watch 'transmit' the initial event | 42 // calls to Watch 'transmit' the initial event |
47 // in the Watch response. But NotifyWatchers | 43 // in the Watch response. But NotifyWatchers |
48 // have no state to transmit. | 44 // have no state to transmit. |
49 if _, ok := <-watch.Changes(); ok { | 45 if _, ok := <-watch.Changes(); ok { |
50 return a.resources.Register(watch), nil | 46 return a.resources.Register(watch), nil |
51 } | 47 } |
52 return "", watcher.MustErr(watch) | 48 return "", watcher.MustErr(watch) |
53 } | 49 } |
54 | 50 |
(...skipping 13 matching lines...) Expand all Loading... |
68 err := ErrPerm | 64 err := ErrPerm |
69 watcherId := "" | 65 watcherId := "" |
70 if canWatch(entity.Tag) { | 66 if canWatch(entity.Tag) { |
71 watcherId, err = a.watchEntity(entity.Tag) | 67 watcherId, err = a.watchEntity(entity.Tag) |
72 } | 68 } |
73 result.Results[i].NotifyWatcherId = watcherId | 69 result.Results[i].NotifyWatcherId = watcherId |
74 result.Results[i].Error = ServerError(err) | 70 result.Results[i].Error = ServerError(err) |
75 } | 71 } |
76 return result, nil | 72 return result, nil |
77 } | 73 } |
LEFT | RIGHT |