/
interface.go
295 lines (227 loc) · 9.78 KB
/
interface.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
// Copyright 2020 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package relation
import (
"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/core/watcher"
"github.com/juju/juju/rpc/params"
"github.com/juju/juju/worker/uniter/hook"
"github.com/juju/juju/worker/uniter/remotestate"
"github.com/juju/juju/worker/uniter/runner/context"
)
type RelationStateTracker interface {
// PrepareHook returns the name of the supplied relation hook, or an error
// if the hook is unknown or invalid given current state.
PrepareHook(hook.Info) (string, error)
// CommitHook persists the state change encoded in the supplied relation
// hook, or returns an error if the hook is unknown or invalid given
// current relation state.
CommitHook(hook.Info) error
// SynchronizeScopes ensures that the locally tracked relation scopes
// reflect the contents of the remote state snapshot by entering or
// exiting scopes as required.
SynchronizeScopes(remotestate.Snapshot) error
// IsKnown returns true if the relation ID is known by the tracker.
IsKnown(int) bool
// IsImplicit returns true if the endpoint for a relation ID is implicit.
IsImplicit(int) (bool, error)
// IsPeerRelation returns true if the endpoint for a relation ID has a
// Peer role.
IsPeerRelation(int) (bool, error)
// HasContainerScope returns true if the specified relation ID has a
// container scope.
HasContainerScope(int) (bool, error)
// RelationCreated returns true if a relation created hook has been
// fired for the specified relation ID.
RelationCreated(int) bool
// RemoteApplication returns the remote application name associated
// with the specified relation ID.
RemoteApplication(int) string
// State returns a State instance for accessing the local state
// for a relation ID.
State(int) (*State, error)
// StateFound returns a true if there is a state for the given in
// in the state manager.
StateFound(int) bool
// GetInfo returns information about current relation state.
GetInfo() map[int]*context.RelationInfo
// Name returns the name of the relation with the supplied id, or an error
// if the relation is unknown.
Name(id int) (string, error)
// LocalUnitName returns the name for the local unit.
LocalUnitName() string
// LocalUnitAndApplicationLife returns the life values for the local
// unit and application.
LocalUnitAndApplicationLife() (life.Value, life.Value, error)
// Report provides information for the engine report.
Report() map[string]interface{}
}
// SubordinateDestroyer destroys all subordinates of a unit.
type SubordinateDestroyer interface {
DestroyAllSubordinates() error
}
// StateManager encapsulates methods required to handle relation
// state.
type StateManager interface {
// KnownIDs returns a slice of relation ids, known to the
// state manager.
KnownIDs() []int
// Relation returns a copy of the relation state for the given id.
Relation(int) (*State, error)
// SetRelation persists the given state, overwriting the previous
// state for a given id or creating state at a new id.
SetRelation(*State) error
// RelationFound returns true if the state manager has a
// state for the given id.
RelationFound(id int) bool
// RemoveRelation removes the state for the given id from the
// manager.
RemoveRelation(id int, unitGetter UnitGetter, knownUnits map[string]bool) error
}
// UnitGetter encapsulates methods to get unit info.
type UnitGetter interface {
// Unit returns the existing unit with the given tag.
Unit(tag names.UnitTag) (Unit, error)
}
// UnitStateReadWriter encapsulates the methods from a state.Unit
// required to set and get unit state.
type UnitStateReadWriter interface {
// SetState sets the state persisted by the charm running in this unit
// and the state internal to the uniter for this unit.
SetState(unitState params.SetUnitStateArg) error
// State returns the state persisted by the charm running in this unit
// and the state internal to the uniter for this unit.
State() (params.UnitStateResult, error)
}
// StateTrackerState encapsulates the methods from state
// required by a relationStateTracker.
type StateTrackerState interface {
// Unit returns the existing unit with the given tag.
Unit(tag names.UnitTag) (Unit, error)
// Relation returns the existing relation with the given tag.
Relation(tag names.RelationTag) (Relation, error)
// RelationById returns the existing relation with the given id.
RelationById(int) (Relation, error)
}
// Unit encapsulates the methods from state.Unit
// required by a relationStateTracker.
type Unit interface {
UnitStateReadWriter
// Tag returns the tag for this unit.
Tag() names.UnitTag
// ApplicationTag returns the tag for this unit's application.
ApplicationTag() names.ApplicationTag
// RelationsStatus returns the tags of the relations the unit has joined
// and entered scope, or the relation is suspended.
RelationsStatus() ([]uniter.RelationStatus, error)
// Watch returns a watcher for observing changes to the unit.
Watch() (watcher.NotifyWatcher, error)
// Destroy when called on a Alive unit, advances its lifecycle as far as
// possible; it otherwise has no effect. In most situations, the unit's
// life is just set to Dying; but if a principal unit that is not assigned
// to a provisioned machine is Destroyed, it will be removed from state
// directly.
Destroy() error
// Name returns the name of the unit.
Name() string
// Refresh updates the cached local copy of the unit's data.
Refresh() error
// Application returns the unit's application.
Application() (Application, error)
// Life returns the unit's lifecycle value.
Life() life.Value
}
// Application encapsulates the methods from
// state.Application required by a relationStateTracker.
type Application interface {
Life() life.Value
}
// Relation encapsulates the methods from
// state.Relation required by a relationStateTracker.
type Relation interface {
// Endpoint returns the endpoint of the relation for the application the
// uniter's managed unit belongs to.
Endpoint() (*uniter.Endpoint, error)
// Id returns the integer internal relation key. This is exposed
// because the unit agent needs to expose a value derived from this
// (as JUJU_RELATION_ID) to allow relation hooks to differentiate
// between relations with different applications.
Id() int
// Life returns the relation's current life state.
Life() life.Value
// OtherApplication returns the name of the application on the other
// end of the relation (from this unit's perspective).
OtherApplication() string
// Refresh refreshes the contents of the relation from the underlying
// state. It returns an error that satisfies errors.IsNotFound if the
// relation has been removed.
Refresh() error
// SetStatus updates the status of the relation.
SetStatus(relation.Status) error
// String returns the relation as a string.
String() string
// Suspended returns the relation's current suspended status.
Suspended() bool
// Tag returns the relation tag.
Tag() names.RelationTag
// Unit returns a uniter.RelationUnit for the supplied unit.
Unit(names.UnitTag) (RelationUnit, error)
// UpdateSuspended updates the in memory value of the
// relation's suspended attribute.
UpdateSuspended(bool)
}
// RelationUnit encapsulates the methods from
// state.RelationUnit required by a relationer.
type RelationUnit interface {
// ApplicationSettings returns a Settings which allows access to this
// unit's application settings within the relation. This can only be
// used from the leader unit.
ApplicationSettings() (*uniter.Settings, error)
// Endpoint returns the endpoint of the relation for the application the
// uniter's managed unit belongs to.
Endpoint() uniter.Endpoint
// 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.
EnterScope() error
// 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.
LeaveScope() error
// Relation returns the relation associated with the unit.
Relation() Relation
// ReadSettings returns a map holding the settings of the unit with the
// supplied name within this relation.
ReadSettings(name string) (params.Settings, error)
// Settings returns a Settings which allows access to the unit's settings
// within the relation.
Settings() (*uniter.Settings, error)
}
// Relationer encapsulates the methods from relationer required by a stateTracker.
type Relationer interface {
// CommitHook persists the fact of the supplied hook's completion.
CommitHook(hi hook.Info) error
// ContextInfo returns a representation of the relationer's current state.
ContextInfo() *context.RelationInfo
// IsDying returns whether the relation is dying.
IsDying() bool
// IsImplicit returns whether the local relation endpoint is implicit.
IsImplicit() bool
// Join initializes local state and causes the unit to enter its relation
// scope, allowing its counterpart units to detect its presence and settings
// changes.
Join() error
// PrepareHook checks that the relation is in a state such that it makes
// sense to execute the supplied hook, and ensures that the relation context
// contains the latest relation state as communicated in the hook.Info.
PrepareHook(hi hook.Info) (string, error)
// RelationUnit returns the relation unit associated with this relationer instance.
RelationUnit() RelationUnit
// SetDying informs the relationer that the unit is departing the relation,
// and that the only hooks it should send henceforth are -departed hooks,
// until the relation is empty, followed by a -broken hook.
SetDying() error
}