forked from juju/juju
/
relationunit.go
665 lines (610 loc) · 22.7 KB
/
relationunit.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
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"fmt"
"strings"
"time"
"github.com/juju/charm/v9"
"github.com/juju/collections/set"
"github.com/juju/errors"
"github.com/juju/mgo/v2"
"github.com/juju/mgo/v2/bson"
"github.com/juju/mgo/v2/txn"
"github.com/juju/names/v4"
jujutxn "github.com/juju/txn/v2"
stateerrors "github.com/DavinZhang/juju/state/errors"
)
// RelationUnit holds information about a single unit in a relation, and
// allows clients to conveniently access unit-specific functionality.
type RelationUnit struct {
st *State
relation *Relation
unitName string
isPrincipal bool
endpoint Endpoint
scope string
// isLocalUnit is true for relation units representing
// the local side of a cross model relation, or for
// any 2 units in a non cross model relation.
isLocalUnit bool
}
// Relation returns the relation associated with the unit.
func (ru *RelationUnit) Relation() *Relation {
return ru.relation
}
// Endpoint returns the relation endpoint that defines the unit's
// participation in the relation.
func (ru *RelationUnit) Endpoint() Endpoint {
return ru.endpoint
}
// UnitName returns the name of the unit in the relation.
func (ru *RelationUnit) UnitName() string {
return ru.unitName
}
// EnterScope ensures that the unit has entered its scope in the relation.
// When the unit has already entered its relation scope, EnterScope will report
// success but make no changes to state.
//
// Otherwise, assuming both the relation and the unit are alive, it will enter
// scope and create or overwrite the unit's settings in the relation according
// to the supplied map.
//
// If the unit is a principal and the relation has container scope, EnterScope
// will also create the required subordinate unit, if it does not already exist;
// this is because there's no point having a principal in scope if there is no
// corresponding subordinate to join it.
//
// Once a unit has entered a scope, it stays in scope without further
// intervention; the relation will not be able to become Dead until all units
// have departed its scopes.
func (ru *RelationUnit) EnterScope(settings map[string]interface{}) error {
db, closer := ru.st.newDB()
defer closer()
relationScopes, closer := db.GetCollection(relationScopesC)
defer closer()
// Verify that the unit is not already in scope, and abort without error
// if it is.
ruKey := ru.key()
if count, err := relationScopes.FindId(ruKey).Count(); err != nil {
return err
} else if count != 0 {
return nil
}
// Collect the operations necessary to enter scope, as follows:
// * Check unit and relation state, and incref the relation.
// * TODO(fwereade): check unit status == params.StatusActive (this
// breaks a bunch of tests in a boring but noisy-to-fix way, and is
// being saved for a followup).
relationDocID := ru.relation.doc.DocID
var ops []txn.Op
if ru.isLocalUnit {
ops = append(ops, txn.Op{
C: unitsC,
Id: ru.unitName,
Assert: isAliveDoc,
})
}
ops = append(ops, txn.Op{
C: relationsC,
Id: relationDocID,
Assert: isAliveDoc,
Update: bson.D{{"$inc", bson.D{{"unitcount", 1}}}},
})
// * Create the unit settings in this relation, if they do not already
// exist; or completely overwrite them if they do. This must happen
// before we create the scope doc, because the existence of a scope doc
// is considered to be a guarantee of the existence of a settings doc.
settingsChanged := func() (bool, error) { return false, nil }
settingsColl, closer := db.GetCollection(settingsC)
defer closer()
if count, err := settingsColl.FindId(ruKey).Count(); err != nil {
return err
} else if count == 0 {
ops = append(ops, createSettingsOp(settingsC, ruKey, settings))
} else {
var rop txn.Op
rop, settingsChanged, err = replaceSettingsOp(ru.st.db(), settingsC, ruKey, settings)
if err != nil {
return err
}
ops = append(ops, rop)
}
// * Create the scope doc.
ops = append(ops, txn.Op{
C: relationScopesC,
Id: ruKey,
Assert: txn.DocMissing,
Insert: relationScopeDoc{
Key: ruKey,
},
})
// * If the unit should have a subordinate, and does not, create it.
var existingSubName string
if subOps, subName, err := ru.subordinateOps(); err != nil {
return err
} else {
existingSubName = subName
ops = append(ops, subOps...)
}
// Now run the complete transaction, or figure out why we can't.
if err := ru.st.db().RunTransaction(ops); err != txn.ErrAborted {
return err
}
if count, err := relationScopes.FindId(ruKey).Count(); err != nil {
return err
} else if count != 0 {
// The scope document exists, so we're actually already in scope.
return nil
}
// The relation or unit might no longer be Alive. (Note that there is no
// need for additional checks if we're trying to create a subordinate
// unit: this could fail due to the subordinate applications not being Alive,
// but this case will always be caught by the check for the relation's
// life (because a relation cannot be Alive if its applications are not).)
relations, closer := db.GetCollection(relationsC)
defer closer()
if alive, err := isAliveWithSession(relations, relationDocID); err != nil {
return err
} else if !alive {
return stateerrors.ErrCannotEnterScope
}
if ru.isLocalUnit {
units, closer := db.GetCollection(unitsC)
defer closer()
if alive, err := isAliveWithSession(units, ru.unitName); err != nil {
return err
} else if !alive {
return stateerrors.ErrCannotEnterScope
}
// Maybe a subordinate used to exist, but is no longer alive. If that is
// case, we will be unable to enter scope until that unit is gone.
if existingSubName != "" {
if alive, err := isAliveWithSession(units, existingSubName); err != nil {
return err
} else if !alive {
return stateerrors.ErrCannotEnterScopeYet
}
}
}
// It's possible that there was a pre-existing settings doc whose version
// has changed under our feet, preventing us from clearing it properly; if
// that is the case, something is seriously wrong (nobody else should be
// touching that doc under our feet) and we should bail out.
prefix := fmt.Sprintf("cannot enter scope for unit %q in relation %q: ", ru.unitName, ru.relation)
if changed, err := settingsChanged(); err != nil {
return err
} else if changed {
return fmt.Errorf(prefix + "concurrent settings change detected")
}
// Apparently, all our assertions should have passed, but the txn was
// aborted: something is really seriously wrong.
return fmt.Errorf(prefix + "inconsistent state in EnterScope")
}
// CounterpartApplications returns the slice of application names that are the counterpart of this unit.
// (So for Peer relations, app is returned, for a Provider the apps on Requirer side is returned
func (ru *RelationUnit) CounterpartApplications() []string {
counterApps := set.NewStrings()
counterRole := counterpartRole(ru.endpoint.Role)
for _, ep := range ru.relation.Endpoints() {
if ep.Role == counterRole {
counterApps.Add(ep.ApplicationName)
}
}
return counterApps.SortedValues()
}
// counterpartApplicationSettingsKeys is the database keys of related applications.
func (ru *RelationUnit) counterpartApplicationSettingsKeys() []string {
counterpartApps := ru.CounterpartApplications()
out := make([]string, len(counterpartApps))
for i, appName := range counterpartApps {
out[i] = relationApplicationSettingsKey(ru.relation.Id(), appName)
}
return out
}
// subordinateOps returns any txn operations necessary to ensure sane
// subordinate state when entering scope. If a required subordinate unit
// exists and is Alive, its name will be returned as well; if one exists
// but is not Alive, ErrCannotEnterScopeYet is returned.
func (ru *RelationUnit) subordinateOps() ([]txn.Op, string, error) {
units, closer := ru.st.db().GetCollection(unitsC)
defer closer()
if !ru.isPrincipal || ru.endpoint.Scope != charm.ScopeContainer {
return nil, "", nil
}
related, err := ru.relation.RelatedEndpoints(ru.endpoint.ApplicationName)
if err != nil {
return nil, "", err
}
if len(related) != 1 {
return nil, "", errors.Errorf("expected single related endpoint, got %v", related)
}
// Find the machine ID that the principal unit is deployed on, and use
// that for the subordinate. It is worthwhile noting that if the unit is
// in a CAAS model, there are no machines.
principal, err := ru.st.Unit(ru.unitName)
if err != nil {
return nil, "", errors.Annotate(err, "unable to load principal unit")
}
var principalMachineID string
if principal.ShouldBeAssigned() {
// We don't care just now if the machine isn't assigned, as CAAS models
// will return that error. For IAAS models, the machine *should* always
// be assigned before it is able to enter scope.
// We don't check the error here now because it'll cause *huge* test
// fallout as many tests don't follow reality, particularly when
// relations are being tested.
principalMachineID, _ = principal.AssignedMachineId()
}
applicationname, unitName := related[0].ApplicationName, ru.unitName
selSubordinate := bson.D{{"application", applicationname}, {"principal", unitName}}
var lDoc lifeDoc
if err := units.Find(selSubordinate).One(&lDoc); err == mgo.ErrNotFound {
application, err := ru.st.Application(applicationname)
if err != nil {
return nil, "", err
}
_, ops, err := application.addUnitOps(unitName, AddUnitParams{
machineID: principalMachineID,
}, nil)
return ops, "", err
} else if err != nil {
return nil, "", err
} else if lDoc.Life != Alive {
return nil, "", stateerrors.ErrCannotEnterScopeYet
}
return []txn.Op{{
C: unitsC,
Id: lDoc.Id,
Assert: isAliveDoc,
}}, lDoc.Id, nil
}
// PrepareLeaveScope causes the unit to be reported as departed by watchers,
// but does not *actually* leave the scope, to avoid triggering relation
// cleanup.
func (ru *RelationUnit) PrepareLeaveScope() error {
relationScopes, closer := ru.st.db().GetCollection(relationScopesC)
defer closer()
key := ru.key()
if count, err := relationScopes.FindId(key).Count(); err != nil {
return err
} else if count == 0 {
return nil
}
ops := []txn.Op{{
C: relationScopesC,
Id: key,
Update: bson.D{{"$set", bson.D{{"departing", true}}}},
}}
return ru.st.db().RunTransaction(ops)
}
// LeaveScopeOperation returns a model operation that will allow relation to leave scope.
func (ru *RelationUnit) LeaveScopeOperation(force bool) *LeaveScopeOperation {
return &LeaveScopeOperation{
ru: &RelationUnit{
st: ru.st,
relation: ru.relation,
unitName: ru.unitName,
isPrincipal: ru.isPrincipal,
endpoint: ru.endpoint,
scope: ru.scope,
isLocalUnit: ru.isLocalUnit,
},
ForcedOperation: ForcedOperation{Force: force},
}
}
// LeaveScopeOperation is a model operation for relation to leave scope.
type LeaveScopeOperation struct {
// ForcedOperation stores needed information to force this operation.
ForcedOperation
// ru holds the unit relation that wants to leave scope.
ru *RelationUnit
}
// Build is part of the ModelOperation interface.
func (op *LeaveScopeOperation) Build(attempt int) ([]txn.Op, error) {
if attempt > 0 {
if err := op.ru.relation.Refresh(); errors.IsNotFound(err) {
return nil, jujutxn.ErrNoOperations
} else if err != nil {
return nil, err
}
}
// When 'force' is set on the operation, this call will return needed operations
// and accumulate all operational errors encountered in the operation.
// If the 'force' is not set, any error will be fatal and no operations will be returned.
switch ops, err := op.internalLeaveScope(); err {
case errRefresh:
case errAlreadyDying:
return nil, jujutxn.ErrNoOperations
case nil:
return ops, nil
default:
if op.Force {
logger.Warningf("forcing %v to leave scope despite error %v", op.Description(), err)
return ops, nil
}
return nil, err
}
return nil, jujutxn.ErrNoOperations
}
func (op *LeaveScopeOperation) Description() string {
return fmt.Sprintf("unit %q in relation %q", op.ru.unitName, op.ru.relation)
}
// Done is part of the ModelOperation interface.
func (op *LeaveScopeOperation) Done(err error) error {
if err != nil {
if !op.Force {
return errors.Annotatef(err, "%v cannot leave scope", op.Description())
}
op.AddError(errors.Errorf("%v tried to forcefully leave scope but proceeded despite encountering ERROR %v", op.Description(), err))
}
return nil
}
// LeaveScopeWithForce in addition to doing what LeaveScope() does,
// when force is passed in as 'true', forces relation unit to leave scope,
// ignoring errors.
func (ru *RelationUnit) LeaveScopeWithForce(force bool, maxWait time.Duration) ([]error, error) {
op := ru.LeaveScopeOperation(force)
op.MaxWait = maxWait
err := ru.st.ApplyOperation(op)
return op.Errors, err
}
// LeaveScope signals that the unit has left its scope in the relation.
// After the unit has left its relation scope, it is no longer a member
// of the relation; if the relation is dying when its last member unit
// leaves, it is removed immediately. It is not an error to leave a scope
// that the unit is not, or never was, a member of.
func (ru *RelationUnit) LeaveScope() error {
errs, err := ru.LeaveScopeWithForce(false, time.Duration(0))
if len(errs) != 0 {
logger.Warningf("operational errors leaving scope for unit %q in relation %q: %v", ru.unitName, ru.relation, errs)
}
return err
}
// leaveScopeForcedOps is an internal method used by other state objects when they just want
// to get database operations that are involved in leaving scope without
// the actual immediate act of leaving scope.
func (ru *RelationUnit) leaveScopeForcedOps(existingOperation *ForcedOperation) ([]txn.Op, error) {
// It does not matter that we are say false to force here- we'll overwrite the whole ForcedOperation.
leaveScopeOperation := ru.LeaveScopeOperation(false)
leaveScopeOperation.ForcedOperation = *existingOperation
return leaveScopeOperation.internalLeaveScope()
}
// When 'force' is set, this call will return needed operations
// and will accumulate all operational errors encountered in the operation.
// If the 'force' is not set, any error will be fatal and no operations will be applied.
func (op *LeaveScopeOperation) internalLeaveScope() ([]txn.Op, error) {
relationScopes, closer := op.ru.st.db().GetCollection(relationScopesC)
defer closer()
key := op.ru.key()
// The logic below is involved because we remove a dying relation
// with the last unit that leaves a scope in it. It handles three
// possible cases:
//
// 1. Relation is alive: just leave the scope.
//
// 2. Relation is dying, and other units remain: just leave the scope.
//
// 3. Relation is dying, and this is the last unit: leave the scope
// and remove the relation.
//
// In each of those cases, proper assertions are done to guarantee
// that the condition observed is still valid when the transaction is
// applied. If an abort happens, it observes the new condition and
// retries. In theory, a worst case will try at most all of the
// conditions once, because units cannot join a scope once its relation
// is dying.
//
// Keep in mind that in the first iteration of the loop it's possible
// to have a Dying relation with a smaller-than-real unit count, because
// Destroy changes the Life attribute in memory (units could join before
// the database is actually changed).
logger.Debugf("%v leaving scope", op.Description())
count, err := relationScopes.FindId(key).Count()
if op.FatalError(errors.Annotatef(err, "cannot examine scope for %s", op.Description())) {
return nil, err
} else if count == 0 {
return nil, jujutxn.ErrNoOperations
}
ops := []txn.Op{{
C: relationScopesC,
Id: key,
Assert: txn.DocExists,
Remove: true,
}}
if op.ru.relation.doc.Life == Alive {
ops = append(ops, txn.Op{
C: relationsC,
Id: op.ru.relation.doc.DocID,
Assert: bson.D{{"life", Alive}},
Update: bson.D{{"$inc", bson.D{{"unitcount", -1}}}},
})
} else if op.ru.relation.doc.UnitCount > 1 {
ops = append(ops, txn.Op{
C: relationsC,
Id: op.ru.relation.doc.DocID,
Assert: bson.D{{"unitcount", bson.D{{"$gt", 1}}}},
Update: bson.D{{"$inc", bson.D{{"unitcount", -1}}}},
})
} else {
// When 'force' is set, this call will return needed operations
// and accumulate all operational errors encountered in the operation.
// If the 'force' is not set, any error will be fatal and no operations will be returned.
relOps, err := op.ru.relation.removeOps("", op.ru.unitName, &op.ForcedOperation)
if op.FatalError(err) {
return nil, err
}
ops = append(ops, relOps...)
}
return ops, nil
}
// Valid returns whether this RelationUnit is one that can actually
// exist in the relation. For container-scoped relations, RUs can be
// created for subordinate units whose principal unit isn't a member
// of the relation. There are too many places that rely on being able
// to construct a nonsensical RU to query InScope or Joined, so we
// allow them to be constructed but they will always return false for
// Valid.
// TODO(babbageclunk): unpick the reliance on creating invalid RUs.
func (ru *RelationUnit) Valid() (bool, error) {
if ru.endpoint.Scope != charm.ScopeContainer || ru.isPrincipal {
return true, nil
}
// A subordinate container-scoped relation unit is valid if:
// the other end of the relation is also a subordinate charm
// or its principal unit is also a member of the relation.
appName, err := names.UnitApplication(ru.unitName)
if err != nil {
return false, errors.Trace(err)
}
var otherAppName string
for _, ep := range ru.relation.Endpoints() {
if ep.ApplicationName != appName {
otherAppName = ep.ApplicationName
}
}
if otherAppName == "" {
return false, errors.Errorf("couldn't find other endpoint")
}
otherApp, err := ru.st.Application(otherAppName)
if err != nil {
return false, errors.Trace(err)
}
if !otherApp.IsPrincipal() {
return true, nil
}
unit, err := ru.st.Unit(ru.unitName)
if err != nil {
return false, errors.Trace(err)
}
// No need to check the flag here - we know we're subordinate.
pName, _ := unit.PrincipalName()
principalAppName, err := names.UnitApplication(pName)
if err != nil {
return false, errors.Trace(err)
}
// If the other application is a principal, only allow it if it's in the relation.
_, err = ru.relation.Endpoint(principalAppName)
if errors.IsNotFound(err) {
return false, nil
} else if err != nil {
return false, errors.Trace(err)
}
return true, nil
}
// InScope returns whether the relation unit has entered scope and not left it.
func (ru *RelationUnit) InScope() (bool, error) {
return ru.inScope(nil)
}
// Joined returns whether the relation unit has entered scope and neither left
// it nor prepared to leave it.
func (ru *RelationUnit) Joined() (bool, error) {
return ru.inScope(bson.D{{"departing", bson.D{{"$ne", true}}}})
}
// inScope returns whether a scope document exists satisfying the supplied
// selector.
func (ru *RelationUnit) inScope(sel bson.D) (bool, error) {
relationScopes, closer := ru.st.db().GetCollection(relationScopesC)
defer closer()
sel = append(sel, bson.D{{"_id", ru.key()}}...)
count, err := relationScopes.Find(sel).Count()
if err != nil {
return false, err
}
return count > 0, nil
}
// WatchScope returns a watcher which notifies of counterpart units
// entering and leaving the unit's scope.
func (ru *RelationUnit) WatchScope() *RelationScopeWatcher {
role := counterpartRole(ru.endpoint.Role)
return watchRelationScope(ru.st, ru.scope, role, ru.unitName)
}
func watchRelationScope(
st *State, scope string, role charm.RelationRole, ignore string,
) *RelationScopeWatcher {
scope = scope + "#" + string(role)
return newRelationScopeWatcher(st, scope, ignore)
}
// Settings returns a Settings which allows access to the unit's settings
// within the relation.
func (ru *RelationUnit) Settings() (*Settings, error) {
s, err := readSettings(ru.st.db(), settingsC, ru.key())
if err != nil {
return nil, errors.Annotatef(err, "unit %q", ru.unitName)
}
return s, nil
}
// ReadSettings returns a map holding the settings of the unit with the
// supplied name within this relation. An error will be returned if the
// relation no longer exists, or if the unit's application is not part of the
// relation, or the settings are invalid; but mere non-existence of the
// unit is not grounds for an error, because the unit settings are
// guaranteed to persist for the lifetime of the relation, regardless
// of the lifetime of the unit.
func (ru *RelationUnit) ReadSettings(uname string) (m map[string]interface{}, err error) {
defer errors.DeferredAnnotatef(&err, "cannot read settings for unit %q in relation %q", uname, ru.relation)
if !names.IsValidUnit(uname) {
return nil, fmt.Errorf("%q is not a valid unit name", uname)
}
key, err := ru.unitKey(uname)
if err != nil {
return nil, err
}
node, err := readSettings(ru.st.db(), settingsC, key)
if err != nil {
return nil, errors.Annotatef(err, "unit %q", uname)
}
return node.Map(), nil
}
// unitKey returns a string, based on the relation and the supplied unit name,
// which is used as a key for that unit within this relation in the settings,
// presence, and relationScopes collections.
func (ru *RelationUnit) unitKey(uname string) (string, error) {
uparts := strings.Split(uname, "/")
sname := uparts[0]
ep, err := ru.relation.Endpoint(sname)
if err != nil {
return "", err
}
return ru._key(string(ep.Role), uname), nil
}
// key returns a string, based on the relation and the current unit name,
// which is used as a key for that unit within this relation in the settings,
// presence, and relationScopes collections.
func (ru *RelationUnit) key() string {
return ru._key(string(ru.endpoint.Role), ru.unitName)
}
func (ru *RelationUnit) _key(role, unitname string) string {
parts := []string{ru.scope, role, unitname}
return strings.Join(parts, "#")
}
// relationScopeDoc represents a unit which is in a relation scope.
// The relation, container, role, and unit are all encoded in the key.
type relationScopeDoc struct {
DocID string `bson:"_id"`
Key string `bson:"key"`
ModelUUID string `bson:"model-uuid"`
Departing bool `bson:"departing"`
}
func (d *relationScopeDoc) unitName() string {
return unitNameFromScopeKey(d.Key)
}
func unitNameFromScopeKey(key string) string {
parts := strings.Split(key, "#")
return parts[len(parts)-1]
}
// unpackScopeKey returns the scope, role and unitname from the
// relation scope key.
func unpackScopeKey(key string) (string, string, string, error) {
if _, localID, ok := splitDocID(key); ok {
key = localID
}
parts := strings.Split(key, "#")
if len(parts) < 4 {
return "", "", "", errors.Errorf("%q has too few parts to be a relation scope key", key)
}
unitName := parts[len(parts)-1]
role := parts[len(parts)-2]
scope := strings.Join(parts[:len(parts)-2], "#")
return scope, role, unitName, nil
}