forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
relation.go
80 lines (65 loc) · 1.92 KB
/
relation.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
// Copyright 2012-2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package context
import (
"fmt"
"github.com/juju/juju/api/uniter"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/worker/uniter/runner/jujuc"
)
type RelationInfo struct {
RelationUnit *uniter.RelationUnit
MemberNames []string
}
// ContextRelation is the implementation of jujuc.ContextRelation.
type ContextRelation struct {
ru *uniter.RelationUnit
relationId int
endpointName string
// settings allows read and write access to the relation unit settings.
settings *uniter.Settings
// cache holds remote unit membership and settings.
cache *RelationCache
}
// NewContextRelation creates a new context for the given relation unit.
// The unit-name keys of members supplies the initial membership.
func NewContextRelation(ru *uniter.RelationUnit, cache *RelationCache) *ContextRelation {
return &ContextRelation{
ru: ru,
relationId: ru.Relation().Id(),
endpointName: ru.Endpoint().Name,
cache: cache,
}
}
func (ctx *ContextRelation) Id() int {
return ctx.relationId
}
func (ctx *ContextRelation) Name() string {
return ctx.endpointName
}
func (ctx *ContextRelation) FakeId() string {
return fmt.Sprintf("%s:%d", ctx.endpointName, ctx.relationId)
}
func (ctx *ContextRelation) UnitNames() []string {
return ctx.cache.MemberNames()
}
func (ctx *ContextRelation) ReadSettings(unit string) (settings params.Settings, err error) {
return ctx.cache.Settings(unit)
}
func (ctx *ContextRelation) Settings() (jujuc.Settings, error) {
if ctx.settings == nil {
node, err := ctx.ru.Settings()
if err != nil {
return nil, err
}
ctx.settings = node
}
return ctx.settings, nil
}
// WriteSettings persists all changes made to the unit's relation settings.
func (ctx *ContextRelation) WriteSettings() (err error) {
if ctx.settings != nil {
err = ctx.settings.Write()
}
return
}