/
relation.go
185 lines (147 loc) · 5.05 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
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
// Copyright 2012-2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package context
import (
"fmt"
"github.com/juju/errors"
"github.com/juju/names/v5"
"github.com/juju/juju/api/agent/uniter"
"github.com/juju/juju/core/life"
"github.com/juju/juju/core/relation"
"github.com/juju/juju/rpc/params"
"github.com/juju/juju/worker/uniter/runner/jujuc"
)
type Relation interface {
// Id returns the integer internal relation key.
Id() int
// Refresh refreshes the contents of the relation from the underlying
// state.
Refresh() error
// Suspended returns the relation's current suspended status.
Suspended() bool
// SetStatus updates the status of the relation.
SetStatus(status relation.Status) error
// Tag returns the relation tag.
Tag() names.RelationTag
// OtherApplication returns the name of the application on the other
// end of the relation (from this unit's perspective).
OtherApplication() string
// Life returns the relation's current life state.
Life() life.Value
}
type RelationUnit interface {
// ApplicationSettings returns a Settings which allows access to this unit's
// application settings within the relation.
ApplicationSettings() (*uniter.Settings, error)
// Endpoint returns the relation endpoint that defines the unit's
// participation in the relation.
Endpoint() uniter.Endpoint
// ReadSettings returns a map holding the settings of the unit with the
// supplied name within this relation.
ReadSettings(name string) (params.Settings, error)
// Relation returns the relation associated with the unit.
Relation() Relation
// Settings returns a Settings which allows access to the unit's settings
// within the relation.
Settings() (*uniter.Settings, error)
}
type RelationUnitShim struct {
*uniter.RelationUnit
}
func (r *RelationUnitShim) Relation() Relation {
return r.RelationUnit.Relation()
}
type RelationInfo struct {
RelationUnit RelationUnit
MemberNames []string
}
// ContextRelation is the implementation of hooks.ContextRelation.
type ContextRelation struct {
ru RelationUnit
relationId int
endpointName string
// settings allows read and write access to the relation unit settings.
settings *uniter.Settings
// applicationSettings allows read and write access to the relation application settings.
applicationSettings *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 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) RelationTag() names.RelationTag {
return ctx.ru.Relation().Tag()
}
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) ReadApplicationSettings(app string) (settings params.Settings, err error) {
return ctx.cache.ApplicationSettings(app)
}
func (ctx *ContextRelation) Settings() (jujuc.Settings, error) {
if ctx.settings == nil {
node, err := ctx.ru.Settings()
if err != nil {
return nil, errors.Trace(err)
}
ctx.settings = node
}
return ctx.settings, nil
}
func (ctx *ContextRelation) ApplicationSettings() (jujuc.Settings, error) {
if ctx.applicationSettings == nil {
settings, err := ctx.ru.ApplicationSettings()
if err != nil {
return nil, errors.Trace(err)
}
ctx.applicationSettings = settings
}
return ctx.applicationSettings, nil
}
// FinalSettings returns the changes made to the relation settings (unit and application)
func (ctx *ContextRelation) FinalSettings() (unitSettings, appSettings params.Settings) {
if ctx.applicationSettings != nil && ctx.applicationSettings.IsDirty() {
appSettings = ctx.applicationSettings.FinalResult()
}
if ctx.settings != nil {
unitSettings = ctx.settings.FinalResult()
}
return unitSettings, appSettings
}
// Suspended returns true if the relation is suspended.
func (ctx *ContextRelation) Suspended() bool {
return ctx.ru.Relation().Suspended()
}
// SetStatus sets the relation's status.
func (ctx *ContextRelation) SetStatus(status relation.Status) error {
return errors.Trace(ctx.ru.Relation().SetStatus(status))
}
// RemoteApplicationName returns the application on the other end of this
// relation from the perspective of this unit.
func (ctx *ContextRelation) RemoteApplicationName() string {
return ctx.ru.Relation().OtherApplication()
}
// Life returns the relation's current life state.
func (ctx *ContextRelation) Life() life.Value {
return ctx.ru.Relation().Life()
}