-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatcher.go
293 lines (266 loc) · 8.16 KB
/
watcher.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package apiserver
import (
"reflect"
"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/state"
)
func init() {
common.RegisterFacade(
"AllWatcher", 0, newClientAllWatcher,
reflect.TypeOf((*srvClientAllWatcher)(nil)),
)
common.RegisterFacade(
"NotifyWatcher", 0, newNotifyWatcher,
reflect.TypeOf((*srvNotifyWatcher)(nil)),
)
common.RegisterFacade(
"StringsWatcher", 0, newStringsWatcher,
reflect.TypeOf((*srvStringsWatcher)(nil)),
)
common.RegisterFacade(
"RelationUnitsWatcher", 0, newRelationUnitsWatcher,
reflect.TypeOf((*srvRelationUnitsWatcher)(nil)),
)
common.RegisterFacade(
"VolumeAttachmentsWatcher", 1, newVolumeAttachmentsWatcher,
reflect.TypeOf((*srvMachineStorageIdsWatcher)(nil)),
)
common.RegisterFacade(
"FilesystemAttachmentsWatcher", 1, newFilesystemAttachmentsWatcher,
reflect.TypeOf((*srvMachineStorageIdsWatcher)(nil)),
)
}
func newClientAllWatcher(st *state.State, resources *common.Resources, auth common.Authorizer, id string) (interface{}, error) {
if !auth.AuthClient() {
return nil, common.ErrPerm
}
watcher, ok := resources.Get(id).(*state.Multiwatcher)
if !ok {
return nil, common.ErrUnknownWatcher
}
return &srvClientAllWatcher{
watcher: watcher,
id: id,
resources: resources,
}, nil
}
// srvClientAllWatcher defines the API methods on a state.Multiwatcher.
// which watches any changes to the state. Each client has its own current set
// of watchers, stored in resources.
type srvClientAllWatcher struct {
watcher *state.Multiwatcher
id string
resources *common.Resources
}
func (aw *srvClientAllWatcher) Next() (params.AllWatcherNextResults, error) {
deltas, err := aw.watcher.Next()
return params.AllWatcherNextResults{
Deltas: deltas,
}, err
}
func (w *srvClientAllWatcher) Stop() error {
return w.resources.Stop(w.id)
}
// srvNotifyWatcher defines the API access to methods on a state.NotifyWatcher.
// Each client has its own current set of watchers, stored in resources.
type srvNotifyWatcher struct {
watcher state.NotifyWatcher
id string
resources *common.Resources
}
func isAgent(auth common.Authorizer) bool {
return auth.AuthMachineAgent() || auth.AuthUnitAgent()
}
func newNotifyWatcher(st *state.State, resources *common.Resources, auth common.Authorizer, id string) (interface{}, error) {
if !isAgent(auth) {
return nil, common.ErrPerm
}
watcher, ok := resources.Get(id).(state.NotifyWatcher)
if !ok {
return nil, common.ErrUnknownWatcher
}
return &srvNotifyWatcher{
watcher: watcher,
id: id,
resources: resources,
}, nil
}
// Next returns when a change has occurred to the
// entity being watched since the most recent call to Next
// or the Watch call that created the NotifyWatcher.
func (w *srvNotifyWatcher) Next() error {
if _, ok := <-w.watcher.Changes(); ok {
return nil
}
err := w.watcher.Err()
if err == nil {
err = common.ErrStoppedWatcher
}
return err
}
// Stop stops the watcher.
func (w *srvNotifyWatcher) Stop() error {
return w.resources.Stop(w.id)
}
// srvStringsWatcher defines the API for methods on a state.StringsWatcher.
// Each client has its own current set of watchers, stored in resources.
// srvStringsWatcher notifies about changes for all entities of a given kind,
// sending the changes as a list of strings.
type srvStringsWatcher struct {
watcher state.StringsWatcher
id string
resources *common.Resources
}
func newStringsWatcher(st *state.State, resources *common.Resources, auth common.Authorizer, id string) (interface{}, error) {
if !isAgent(auth) {
return nil, common.ErrPerm
}
watcher, ok := resources.Get(id).(state.StringsWatcher)
if !ok {
return nil, common.ErrUnknownWatcher
}
return &srvStringsWatcher{
watcher: watcher,
id: id,
resources: resources,
}, nil
}
// Next returns when a change has occured to an entity of the
// collection being watched since the most recent call to Next
// or the Watch call that created the srvStringsWatcher.
func (w *srvStringsWatcher) Next() (params.StringsWatchResult, error) {
if changes, ok := <-w.watcher.Changes(); ok {
return params.StringsWatchResult{
Changes: changes,
}, nil
}
err := w.watcher.Err()
if err == nil {
err = common.ErrStoppedWatcher
}
return params.StringsWatchResult{}, err
}
// Stop stops the watcher.
func (w *srvStringsWatcher) Stop() error {
return w.resources.Stop(w.id)
}
// srvRelationUnitsWatcher defines the API wrapping a state.RelationUnitsWatcher.
// It notifies about units entering and leaving the scope of a RelationUnit,
// and changes to the settings of those units known to have entered.
type srvRelationUnitsWatcher struct {
watcher state.RelationUnitsWatcher
id string
resources *common.Resources
}
func newRelationUnitsWatcher(st *state.State, resources *common.Resources, auth common.Authorizer, id string) (interface{}, error) {
if !isAgent(auth) {
return nil, common.ErrPerm
}
watcher, ok := resources.Get(id).(state.RelationUnitsWatcher)
if !ok {
return nil, common.ErrUnknownWatcher
}
return &srvRelationUnitsWatcher{
watcher: watcher,
id: id,
resources: resources,
}, nil
}
// Next returns when a change has occured to an entity of the
// collection being watched since the most recent call to Next
// or the Watch call that created the srvRelationUnitsWatcher.
func (w *srvRelationUnitsWatcher) Next() (params.RelationUnitsWatchResult, error) {
if changes, ok := <-w.watcher.Changes(); ok {
return params.RelationUnitsWatchResult{
Changes: changes,
}, nil
}
err := w.watcher.Err()
if err == nil {
err = common.ErrStoppedWatcher
}
return params.RelationUnitsWatchResult{}, err
}
// Stop stops the watcher.
func (w *srvRelationUnitsWatcher) Stop() error {
return w.resources.Stop(w.id)
}
// srvMachineStorageIdsWatcher defines the API wrapping a state.StringsWatcher
// watching machine/storage attachments. This watcher notifies about storage
// entities (volumes/filesystems) being attached to and detached from machines.
//
// TODO(axw) state needs a new watcher, this is a bt of a hack. State watchers
// could do with some deduplication of logic, and I don't want to add to that
// spaghetti right now.
type srvMachineStorageIdsWatcher struct {
watcher state.StringsWatcher
id string
resources *common.Resources
parser func([]string) ([]params.MachineStorageId, error)
}
func newVolumeAttachmentsWatcher(
st *state.State,
resources *common.Resources,
auth common.Authorizer,
id string,
) (interface{}, error) {
return newMachineStorageIdsWatcher(
st, resources, auth, id, common.ParseVolumeAttachmentIds,
)
}
func newFilesystemAttachmentsWatcher(
st *state.State,
resources *common.Resources,
auth common.Authorizer,
id string,
) (interface{}, error) {
return newMachineStorageIdsWatcher(
st, resources, auth, id, common.ParseFilesystemAttachmentIds,
)
}
func newMachineStorageIdsWatcher(
st *state.State,
resources *common.Resources,
auth common.Authorizer,
id string,
parser func([]string) ([]params.MachineStorageId, error),
) (interface{}, error) {
if !isAgent(auth) {
return nil, common.ErrPerm
}
watcher, ok := resources.Get(id).(state.StringsWatcher)
if !ok {
return nil, common.ErrUnknownWatcher
}
return &srvMachineStorageIdsWatcher{
watcher: watcher,
id: id,
resources: resources,
}, nil
}
// Next returns when a change has occured to an entity of the
// collection being watched since the most recent call to Next
// or the Watch call that created the srvMachineStorageIdsWatcher.
func (w *srvMachineStorageIdsWatcher) Next() (params.MachineStorageIdsWatchResult, error) {
if stringChanges, ok := <-w.watcher.Changes(); ok {
changes, err := common.ParseVolumeAttachmentIds(stringChanges)
if err != nil {
return params.MachineStorageIdsWatchResult{}, err
}
return params.MachineStorageIdsWatchResult{
Changes: changes,
}, nil
}
err := w.watcher.Err()
if err == nil {
err = common.ErrStoppedWatcher
}
return params.MachineStorageIdsWatchResult{}, err
}
// Stop stops the watcher.
func (w *srvMachineStorageIdsWatcher) Stop() error {
return w.resources.Stop(w.id)
}