forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remoterelations.go
675 lines (630 loc) · 22.6 KB
/
remoterelations.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package remoterelations
import (
"fmt"
"strings"
"github.com/juju/errors"
"github.com/juju/loggo"
"gopkg.in/juju/charm.v6-unstable"
"gopkg.in/juju/names.v2"
"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/core/crossmodel"
"github.com/juju/juju/feature"
"github.com/juju/juju/state"
"github.com/juju/juju/state/watcher"
)
var logger = loggo.GetLogger("juju.apiserver.remoterelations")
func init() {
common.RegisterStandardFacadeForFeature("RemoteRelations", 1, NewStateRemoteRelationsAPI, feature.CrossModelRelations)
}
// RemoteRelationsAPI provides access to the Provisioner API facade.
type RemoteRelationsAPI struct {
st RemoteRelationsState
pool StatePool
resources facade.Resources
authorizer facade.Authorizer
}
// NewRemoteRelationsAPI creates a new server-side RemoteRelationsAPI facade
// backed by global state.
func NewStateRemoteRelationsAPI(ctx facade.Context) (*RemoteRelationsAPI, error) {
return NewRemoteRelationsAPI(stateShim{ctx.State()}, statePoolShim{ctx.StatePool()}, ctx.Resources(), ctx.Auth())
}
// NewRemoteRelationsAPI returns a new server-side RemoteRelationsAPI facade.
func NewRemoteRelationsAPI(
st RemoteRelationsState,
pool StatePool,
resources facade.Resources,
authorizer facade.Authorizer,
) (*RemoteRelationsAPI, error) {
if !authorizer.AuthController() {
return nil, common.ErrPerm
}
return &RemoteRelationsAPI{
st: st,
pool: pool,
resources: resources,
authorizer: authorizer,
}, nil
}
// ImportRemoteEntities adds entities to the remote entities collection with the specified opaque tokens.
func (api *RemoteRelationsAPI) ImportRemoteEntities(args params.RemoteEntityArgs) (params.ErrorResults, error) {
results := params.ErrorResults{
Results: make([]params.ErrorResult, len(args.Args)),
}
for i, arg := range args.Args {
err := api.importRemoteEntity(arg)
results.Results[i].Error = common.ServerError(err)
}
return results, nil
}
func (api *RemoteRelationsAPI) importRemoteEntity(arg params.RemoteEntityArg) error {
entityTag, err := names.ParseTag(arg.Tag)
if err != nil {
return errors.Trace(err)
}
modelTag, err := names.ParseModelTag(arg.ModelTag)
if err != nil {
return errors.Trace(err)
}
return api.st.ImportRemoteEntity(modelTag, entityTag, arg.Token)
}
// ExportEntities allocates unique, remote entity IDs for the given entities in the local model.
func (api *RemoteRelationsAPI) ExportEntities(entities params.Entities) (params.RemoteEntityIdResults, error) {
results := params.RemoteEntityIdResults{
Results: make([]params.RemoteEntityIdResult, len(entities.Entities)),
}
for i, entity := range entities.Entities {
tag, err := names.ParseTag(entity.Tag)
if err != nil {
results.Results[i].Error = common.ServerError(err)
continue
}
token, err := api.st.ExportLocalEntity(tag)
if err != nil {
results.Results[i].Error = common.ServerError(err)
if !errors.IsAlreadyExists(err) {
continue
}
}
results.Results[i].Result = ¶ms.RemoteEntityId{
ModelUUID: api.st.ModelUUID(),
Token: token,
}
}
return results, nil
}
// RemoveRemoteEntities removes the specified entities from the remote entities collection.
func (api *RemoteRelationsAPI) RemoveRemoteEntities(args params.RemoteEntityArgs) (params.ErrorResults, error) {
results := params.ErrorResults{
Results: make([]params.ErrorResult, len(args.Args)),
}
for i, arg := range args.Args {
err := api.removeRemoteEntity(arg)
results.Results[i].Error = common.ServerError(err)
}
return results, nil
}
func (api *RemoteRelationsAPI) removeRemoteEntity(arg params.RemoteEntityArg) error {
entityTag, err := names.ParseTag(arg.Tag)
if err != nil {
return errors.Trace(err)
}
modelTag, err := names.ParseModelTag(arg.ModelTag)
if err != nil {
return errors.Trace(err)
}
return api.st.RemoveRemoteEntity(modelTag, entityTag)
}
// GetToken returns the token associated with the entity with the given tag for the current model.
func (api *RemoteRelationsAPI) GetTokens(args params.GetTokenArgs) (params.StringResults, error) {
results := params.StringResults{
Results: make([]params.StringResult, len(args.Args)),
}
for i, arg := range args.Args {
entityTag, err := names.ParseTag(arg.Tag)
if err != nil {
results.Results[i].Error = common.ServerError(err)
continue
}
modelTag, err := names.ParseModelTag(arg.ModelTag)
if err != nil {
results.Results[i].Error = common.ServerError(err)
continue
}
token, err := api.st.GetToken(modelTag, entityTag)
if err != nil {
results.Results[i].Error = common.ServerError(err)
}
results.Results[i].Result = token
}
return results, nil
}
// RelationUnitSettings returns the relation unit settings for the given relation units in the local model.
func (api *RemoteRelationsAPI) RelationUnitSettings(relationUnits params.RelationUnits) (params.SettingsResults, error) {
results := params.SettingsResults{
Results: make([]params.SettingsResult, len(relationUnits.RelationUnits)),
}
one := func(ru params.RelationUnit) (params.Settings, error) {
relationTag, err := names.ParseRelationTag(ru.Relation)
if err != nil {
return nil, errors.Trace(err)
}
rel, err := api.st.KeyRelation(relationTag.Id())
if err != nil {
return nil, errors.Trace(err)
}
unitTag, err := names.ParseUnitTag(ru.Unit)
if err != nil {
return nil, errors.Trace(err)
}
unit, err := rel.Unit(unitTag.Id())
if err != nil {
return nil, errors.Trace(err)
}
settings, err := unit.Settings()
if err != nil {
return nil, errors.Trace(err)
}
paramsSettings := make(params.Settings)
for k, v := range settings {
vString, ok := v.(string)
if !ok {
return nil, errors.Errorf(
"invalid relation setting %q: expected string, got %T", k, v,
)
}
paramsSettings[k] = vString
}
return paramsSettings, nil
}
for i, ru := range relationUnits.RelationUnits {
settings, err := one(ru)
if err != nil {
results.Results[i].Error = common.ServerError(err)
continue
}
results.Results[i].Settings = settings
}
return results, nil
}
func (api *RemoteRelationsAPI) remoteRelation(entity params.Entity) (*params.RemoteRelation, error) {
tag, err := names.ParseRelationTag(entity.Tag)
if err != nil {
return nil, errors.Trace(err)
}
rel, err := api.st.KeyRelation(tag.Id())
if err != nil {
return nil, errors.Trace(err)
}
result := ¶ms.RemoteRelation{
Id: rel.Id(),
Life: params.Life(rel.Life().String()),
Key: tag.Id(),
}
for _, ep := range rel.Endpoints() {
// Try looking up the info for the remote application.
remoteApp, err := api.st.RemoteApplication(ep.ApplicationName)
if err != nil && !errors.IsNotFound(err) {
return nil, errors.Trace(err)
} else if err == nil {
result.RemoteEndpointName = ep.Name
result.SourceModelUUID = remoteApp.SourceModel().Id()
continue
}
// Try looking up the info for the local application.
_, err = api.st.Application(ep.ApplicationName)
if err != nil && !errors.IsNotFound(err) {
return nil, errors.Trace(err)
} else if err == nil {
result.ApplicationName = ep.ApplicationName
result.Endpoint = params.RemoteEndpoint{
Name: ep.Name,
Interface: ep.Interface,
Role: ep.Role,
Scope: ep.Scope,
Limit: ep.Limit,
}
continue
}
}
return result, nil
}
// Relations returns information about the cross-model relations with the specified keys
// in the local model.
func (api *RemoteRelationsAPI) Relations(entities params.Entities) (params.RemoteRelationResults, error) {
results := params.RemoteRelationResults{
Results: make([]params.RemoteRelationResult, len(entities.Entities)),
}
for i, entity := range entities.Entities {
remoteRelation, err := api.remoteRelation(entity)
if err != nil {
results.Results[i].Error = common.ServerError(err)
continue
}
results.Results[i].Result = remoteRelation
}
return results, nil
}
// RemoteApplications returns the current state of the remote applications with
// the specified names in the local model.
func (api *RemoteRelationsAPI) RemoteApplications(entities params.Entities) (params.RemoteApplicationResults, error) {
results := params.RemoteApplicationResults{
Results: make([]params.RemoteApplicationResult, len(entities.Entities)),
}
one := func(entity params.Entity) (*params.RemoteApplication, error) {
tag, err := names.ParseApplicationTag(entity.Tag)
if err != nil {
return nil, errors.Trace(err)
}
remoteApp, err := api.st.RemoteApplication(tag.Id())
if err != nil {
return nil, errors.Trace(err)
}
status, err := remoteApp.Status()
if err != nil {
return nil, errors.Trace(err)
}
return ¶ms.RemoteApplication{
Name: remoteApp.Name(),
OfferName: remoteApp.OfferName(),
Life: params.Life(remoteApp.Life().String()),
Status: status.Status.String(),
ModelUUID: remoteApp.SourceModel().Id(),
Registered: remoteApp.IsConsumerProxy(),
}, nil
}
for i, entity := range entities.Entities {
remoteApplication, err := one(entity)
if err != nil {
results.Results[i].Error = common.ServerError(err)
continue
}
results.Results[i].Result = remoteApplication
}
return results, nil
}
// PublishLocalRelationChange publishes local relations changes to the
// remote side offering those relations.
func (api *RemoteRelationsAPI) PublishLocalRelationChange(
changes params.RemoteRelationsChanges,
) (params.ErrorResults, error) {
results := params.ErrorResults{
Results: make([]params.ErrorResult, len(changes.Changes)),
}
for i, change := range changes.Changes {
if err := api.publishRelationChange(change); err != nil {
results.Results[i].Error = common.ServerError(err)
continue
}
}
return results, nil
}
func (api *RemoteRelationsAPI) publishRelationChange(change params.RemoteRelationChangeEvent) error {
logger.Debugf("publish into model %v change: %+v", api.st.ModelUUID(), change)
relationTag, err := api.getRemoteEntityTag(change.RelationId)
if err != nil {
if errors.IsNotFound(err) {
logger.Debugf("not found relation tag %+v in model %v, exit early", change.RelationId, api.st.ModelUUID())
return nil
}
return errors.Trace(err)
}
logger.Debugf("relation tag for remote id %+v is %v", change.RelationId, relationTag)
// Ensure the relation exists.
rel, err := api.st.KeyRelation(relationTag.Id())
if errors.IsNotFound(err) {
if change.Life != params.Alive {
return nil
}
}
if err != nil {
return errors.Trace(err)
}
// Look up the application on the remote side of this relation
// ie from the model which published this change.
applicationTag, err := api.getRemoteEntityTag(change.ApplicationId)
if err != nil {
return errors.Trace(err)
}
logger.Debugf("application tag for remote id %+v is %v", change.ApplicationId, applicationTag)
// If the remote model has destroyed the relation, do it here also.
if change.Life != params.Alive {
logger.Debugf("remote side of %v died", relationTag)
if err := rel.Destroy(); err != nil {
return errors.Trace(err)
}
// See if we need to remove the remote application proxy - we do this
// on the offering side as there is 1:1 between proxy and consuming app.
if applicationTag != nil {
remoteApp, err := api.st.RemoteApplication(applicationTag.Id())
if err != nil && !errors.IsNotFound(err) {
return errors.Trace(err)
}
if err == nil && remoteApp.IsConsumerProxy() {
logger.Debugf("destroy consuming app proxy for %v", remoteApp.Name())
if err := remoteApp.Destroy(); err != nil {
return errors.Trace(err)
}
}
}
}
// TODO(wallyworld) - deal with remote application being removed
if applicationTag == nil {
logger.Infof("no remote application found for %v", relationTag.Id())
return nil
}
logger.Debugf("remote applocation for changed relation %v is %v", relationTag.Id(), applicationTag.Id())
for _, id := range change.DepartedUnits {
unitTag := names.NewUnitTag(fmt.Sprintf("%s/%v", applicationTag.Id(), id))
logger.Debugf("unit %v has departed relation %v", unitTag.Id(), relationTag.Id())
ru, err := rel.RemoteUnit(unitTag.Id())
if err != nil {
return errors.Trace(err)
}
logger.Debugf("%s leaving scope", unitTag.Id())
if err := ru.LeaveScope(); err != nil {
return errors.Trace(err)
}
}
for _, change := range change.ChangedUnits {
unitTag := names.NewUnitTag(fmt.Sprintf("%s/%v", applicationTag.Id(), change.UnitId))
logger.Debugf("changed unit tag for remote id %v is %v", change.UnitId, unitTag)
ru, err := rel.RemoteUnit(unitTag.Id())
if err != nil {
return errors.Trace(err)
}
inScope, err := ru.InScope()
if err != nil {
return errors.Trace(err)
}
settings := make(map[string]interface{})
for k, v := range change.Settings {
settings[k] = v
}
if !inScope {
logger.Debugf("%s entering scope (%v)", unitTag.Id(), settings)
err = ru.EnterScope(settings)
} else {
logger.Debugf("%s updated settings (%v)", unitTag.Id(), settings)
err = ru.ReplaceSettings(settings)
}
if err != nil {
return errors.Trace(err)
}
}
return nil
}
func (api *RemoteRelationsAPI) getRemoteEntityTag(id params.RemoteEntityId) (names.Tag, error) {
modelTag := names.NewModelTag(id.ModelUUID)
return api.st.GetRemoteEntity(modelTag, id.Token)
}
// RegisterRemoteRelations sets up the local model to participate
// in the specified relations. This operation is idempotent.
func (api *RemoteRelationsAPI) RegisterRemoteRelations(
relations params.RegisterRemoteRelations,
) (params.RemoteEntityIdResults, error) {
results := params.RemoteEntityIdResults{
Results: make([]params.RemoteEntityIdResult, len(relations.Relations)),
}
for i, relation := range relations.Relations {
if id, err := api.registerRemoteRelation(relation); err != nil {
results.Results[i].Error = common.ServerError(err)
continue
} else {
results.Results[i].Result = id
}
}
return results, nil
}
func (api *RemoteRelationsAPI) registerRemoteRelation(relation params.RegisterRemoteRelation) (*params.RemoteEntityId, error) {
logger.Debugf("register remote relation %+v", relation)
// TODO(wallyworld) - do this as a transaction so the result is atomic
// Perform some initial validation - is the local application alive?
// Look up the offer record so get the local application to which we need to relate.
appOffers, err := api.st.ListOffers(crossmodel.ApplicationOfferFilter{
OfferName: relation.OfferName,
})
if err != nil {
return nil, errors.Trace(err)
}
if len(appOffers) == 0 {
return nil, errors.NotFoundf("application offer %v", relation.OfferName)
}
localApplicationName := appOffers[0].ApplicationName
localApp, err := api.st.Application(localApplicationName)
if err != nil {
return nil, errors.Annotatef(err, "cannot get application for offer %q", relation.OfferName)
}
if localApp.Life() != state.Alive {
// We don't want to leak the application name so just log it.
logger.Warningf("local application for offer %v not found", localApplicationName)
return nil, errors.NotFoundf("local application for offer %v", relation.OfferName)
}
eps, err := localApp.Endpoints()
if err != nil {
return nil, errors.Trace(err)
}
// Does the requested local endpoint exist?
var localEndpoint *state.Endpoint
for _, ep := range eps {
if ep.Name == relation.LocalEndpointName {
localEndpoint = &ep
break
}
}
if localEndpoint == nil {
return nil, errors.NotFoundf("relation endpoint %v", relation.LocalEndpointName)
}
// Add the remote application reference. We construct a unique, opaque application name based on the
// token passed in from the consuming model. This model, which is offering the application being
// related to, does not need to know the name of the consuming application.
uniqueRemoteApplicationName := "remote-" + strings.Replace(relation.ApplicationId.Token, "-", "", -1)
remoteEndpoint := state.Endpoint{
ApplicationName: uniqueRemoteApplicationName,
Relation: charm.Relation{
Name: relation.RemoteEndpoint.Name,
Scope: relation.RemoteEndpoint.Scope,
Interface: relation.RemoteEndpoint.Interface,
Role: relation.RemoteEndpoint.Role,
Limit: relation.RemoteEndpoint.Limit,
},
}
remoteModelTag := names.NewModelTag(relation.ApplicationId.ModelUUID)
_, err = api.st.AddRemoteApplication(state.AddRemoteApplicationParams{
Name: uniqueRemoteApplicationName,
OfferName: relation.OfferName,
SourceModel: names.NewModelTag(relation.ApplicationId.ModelUUID),
Token: relation.ApplicationId.Token,
Endpoints: []charm.Relation{remoteEndpoint.Relation},
IsConsumerProxy: true,
})
// If it already exists, that's fine.
if err != nil && !errors.IsAlreadyExists(err) {
return nil, errors.Annotatef(err, "adding remote application %v", uniqueRemoteApplicationName)
}
logger.Debugf("added remote application %v to local model with token %v", uniqueRemoteApplicationName, relation.ApplicationId.Token)
// Now add the relation if it doesn't already exist.
localRel, err := api.st.EndpointsRelation(*localEndpoint, remoteEndpoint)
if err != nil && !errors.IsNotFound(err) {
return nil, errors.Trace(err)
}
if err != nil {
localRel, err = api.st.AddRelation(*localEndpoint, remoteEndpoint)
// Again, if it already exists, that's fine.
if err != nil && !errors.IsAlreadyExists(err) {
return nil, errors.Annotate(err, "adding remote relation")
}
logger.Debugf("added relation %v to model %v", localRel.Tag().Id(), api.st.ModelUUID())
}
// Ensure we have references recorded.
logger.Debugf("importing remote relation into model %v", api.st.ModelUUID())
logger.Debugf("remote model is %v", remoteModelTag.Id())
err = api.st.ImportRemoteEntity(remoteModelTag, localRel.Tag(), relation.RelationId.Token)
if err != nil && !errors.IsAlreadyExists(err) {
return nil, errors.Annotatef(err, "importing remote relation %v to local model", localRel.Tag().Id())
}
logger.Debugf("relation token %v exported for %v ", relation.RelationId.Token, localRel.Tag().Id())
// Export the local application from this model so we can tell the caller what the remote id is.
// NB we need to export the application last so that everything else is in place when the worker is
// woken up by the watcher.
token, err := api.st.ExportLocalEntity(names.NewApplicationTag(localApp.Name()))
if err != nil && !errors.IsAlreadyExists(err) {
return nil, errors.Annotatef(err, "exporting local application %v", localApp.Name())
}
logger.Debugf("local application %v from model %v exported with token %v ", localApp.Name(), api.st.ModelUUID(), token)
return ¶ms.RemoteEntityId{
ModelUUID: api.st.ModelUUID(),
Token: token,
}, nil
}
// WatchRemoteApplications starts a strings watcher that notifies of the addition,
// removal, and lifecycle changes of remote applications in the model; and
// returns the watcher ID and initial IDs of remote applications, or an error if
// watching failed.
func (api *RemoteRelationsAPI) WatchRemoteApplications() (params.StringsWatchResult, error) {
w := api.st.WatchRemoteApplications()
if changes, ok := <-w.Changes(); ok {
return params.StringsWatchResult{
StringsWatcherId: api.resources.Register(w),
Changes: changes,
}, nil
}
return params.StringsWatchResult{}, watcher.EnsureErr(w)
}
// WatchLocalRelationUnits starts a RelationUnitsWatcher for watching the local
// relation units involved in each specified relation in the local model,
// and returns the watcher IDs and initial values, or an error if the relation
// units could not be watched.
func (api *RemoteRelationsAPI) WatchLocalRelationUnits(args params.Entities) (params.RelationUnitsWatchResults, error) {
results := params.RelationUnitsWatchResults{
make([]params.RelationUnitsWatchResult, len(args.Entities)),
}
for i, arg := range args.Entities {
relationTag, err := names.ParseRelationTag(arg.Tag)
if err != nil {
results.Results[i].Error = common.ServerError(err)
continue
}
w, err := api.watchLocalRelationUnits(relationTag)
if err != nil {
results.Results[i].Error = common.ServerError(err)
continue
}
changes, ok := <-w.Changes()
if !ok {
results.Results[i].Error = common.ServerError(watcher.EnsureErr(w))
continue
}
results.Results[i].RelationUnitsWatcherId = api.resources.Register(w)
results.Results[i].Changes = changes
}
return results, nil
}
func (api *RemoteRelationsAPI) watchLocalRelationUnits(tag names.RelationTag) (state.RelationUnitsWatcher, error) {
relation, err := api.st.KeyRelation(tag.Id())
if err != nil {
return nil, errors.Trace(err)
}
for _, ep := range relation.Endpoints() {
_, err := api.st.Application(ep.ApplicationName)
if errors.IsNotFound(err) {
// Not found, so it's the remote application. Try the next endpoint.
continue
} else if err != nil {
return nil, errors.Trace(err)
}
w, err := relation.WatchUnits(ep.ApplicationName)
if err != nil {
return nil, errors.Trace(err)
}
return w, nil
}
return nil, errors.NotFoundf("local application for %s", names.ReadableString(tag))
}
// WatchRemoteApplicationRelations starts a StringsWatcher for watching the relations of
// each specified application in the local model, and returns the watcher IDs
// and initial values, or an error if the services' relations could not be
// watched.
func (api *RemoteRelationsAPI) WatchRemoteApplicationRelations(args params.Entities) (params.StringsWatchResults, error) {
results := params.StringsWatchResults{
make([]params.StringsWatchResult, len(args.Entities)),
}
for i, arg := range args.Entities {
applicationTag, err := names.ParseApplicationTag(arg.Tag)
if err != nil {
results.Results[i].Error = common.ServerError(err)
continue
}
appName := applicationTag.Id()
w, err := api.st.WatchRemoteApplicationRelations(appName)
if err != nil {
results.Results[i].Error = common.ServerError(err)
continue
}
changes, ok := <-w.Changes()
if !ok {
results.Results[i].Error = common.ServerError(watcher.EnsureErr(w))
continue
}
results.Results[i].StringsWatcherId = api.resources.Register(w)
results.Results[i].Changes = changes
}
return results, nil
}
// WatchRemoteRelations starts a strings watcher that notifies of the addition,
// removal, and lifecycle changes of remote relations in the model; and
// returns the watcher ID and initial IDs of remote relations, or an error if
// watching failed.
func (api *RemoteRelationsAPI) WatchRemoteRelations() (params.StringsWatchResult, error) {
w := api.st.WatchRemoteRelations()
if changes, ok := <-w.Changes(); ok {
return params.StringsWatchResult{
StringsWatcherId: api.resources.Register(w),
Changes: changes,
}, nil
}
return params.StringsWatchResult{}, watcher.EnsureErr(w)
}