github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/worker/uniter/relation/interface.go (about) 1 // Copyright 2020 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package relation 5 6 import ( 7 "github.com/juju/names/v5" 8 9 "github.com/juju/juju/api/agent/uniter" 10 "github.com/juju/juju/core/life" 11 "github.com/juju/juju/core/relation" 12 "github.com/juju/juju/core/watcher" 13 "github.com/juju/juju/rpc/params" 14 "github.com/juju/juju/worker/uniter/hook" 15 "github.com/juju/juju/worker/uniter/remotestate" 16 "github.com/juju/juju/worker/uniter/runner/context" 17 ) 18 19 type RelationStateTracker interface { 20 // PrepareHook returns the name of the supplied relation hook, or an error 21 // if the hook is unknown or invalid given current state. 22 PrepareHook(hook.Info) (string, error) 23 24 // CommitHook persists the state change encoded in the supplied relation 25 // hook, or returns an error if the hook is unknown or invalid given 26 // current relation state. 27 CommitHook(hook.Info) error 28 29 // SynchronizeScopes ensures that the locally tracked relation scopes 30 // reflect the contents of the remote state snapshot by entering or 31 // exiting scopes as required. 32 SynchronizeScopes(remotestate.Snapshot) error 33 34 // IsKnown returns true if the relation ID is known by the tracker. 35 IsKnown(int) bool 36 37 // IsImplicit returns true if the endpoint for a relation ID is implicit. 38 IsImplicit(int) (bool, error) 39 40 // IsPeerRelation returns true if the endpoint for a relation ID has a 41 // Peer role. 42 IsPeerRelation(int) (bool, error) 43 44 // HasContainerScope returns true if the specified relation ID has a 45 // container scope. 46 HasContainerScope(int) (bool, error) 47 48 // RelationCreated returns true if a relation created hook has been 49 // fired for the specified relation ID. 50 RelationCreated(int) bool 51 52 // RemoteApplication returns the remote application name associated 53 // with the specified relation ID. 54 RemoteApplication(int) string 55 56 // State returns a State instance for accessing the local state 57 // for a relation ID. 58 State(int) (*State, error) 59 60 // StateFound returns a true if there is a state for the given in 61 // in the state manager. 62 StateFound(int) bool 63 64 // GetInfo returns information about current relation state. 65 GetInfo() map[int]*context.RelationInfo 66 67 // Name returns the name of the relation with the supplied id, or an error 68 // if the relation is unknown. 69 Name(id int) (string, error) 70 71 // LocalUnitName returns the name for the local unit. 72 LocalUnitName() string 73 74 // LocalUnitAndApplicationLife returns the life values for the local 75 // unit and application. 76 LocalUnitAndApplicationLife() (life.Value, life.Value, error) 77 78 // Report provides information for the engine report. 79 Report() map[string]interface{} 80 } 81 82 // SubordinateDestroyer destroys all subordinates of a unit. 83 type SubordinateDestroyer interface { 84 DestroyAllSubordinates() error 85 } 86 87 // StateManager encapsulates methods required to handle relation 88 // state. 89 type StateManager interface { 90 // KnownIDs returns a slice of relation ids, known to the 91 // state manager. 92 KnownIDs() []int 93 94 // Relation returns a copy of the relation state for the given id. 95 Relation(int) (*State, error) 96 97 // SetRelation persists the given state, overwriting the previous 98 // state for a given id or creating state at a new id. 99 SetRelation(*State) error 100 101 // RelationFound returns true if the state manager has a 102 // state for the given id. 103 RelationFound(id int) bool 104 105 // RemoveRelation removes the state for the given id from the 106 // manager. 107 RemoveRelation(id int, unitGetter UnitGetter, knownUnits map[string]bool) error 108 } 109 110 // UnitGetter encapsulates methods to get unit info. 111 type UnitGetter interface { 112 // Unit returns the existing unit with the given tag. 113 Unit(tag names.UnitTag) (Unit, error) 114 } 115 116 // UnitStateReadWriter encapsulates the methods from a state.Unit 117 // required to set and get unit state. 118 type UnitStateReadWriter interface { 119 // SetState sets the state persisted by the charm running in this unit 120 // and the state internal to the uniter for this unit. 121 SetState(unitState params.SetUnitStateArg) error 122 123 // State returns the state persisted by the charm running in this unit 124 // and the state internal to the uniter for this unit. 125 State() (params.UnitStateResult, error) 126 } 127 128 // StateTrackerState encapsulates the methods from state 129 // required by a relationStateTracker. 130 type StateTrackerState interface { 131 // Unit returns the existing unit with the given tag. 132 Unit(tag names.UnitTag) (Unit, error) 133 134 // Relation returns the existing relation with the given tag. 135 Relation(tag names.RelationTag) (Relation, error) 136 137 // RelationById returns the existing relation with the given id. 138 RelationById(int) (Relation, error) 139 } 140 141 // Unit encapsulates the methods from state.Unit 142 // required by a relationStateTracker. 143 type Unit interface { 144 UnitStateReadWriter 145 146 // Tag returns the tag for this unit. 147 Tag() names.UnitTag 148 149 // ApplicationTag returns the tag for this unit's application. 150 ApplicationTag() names.ApplicationTag 151 152 // RelationsStatus returns the tags of the relations the unit has joined 153 // and entered scope, or the relation is suspended. 154 RelationsStatus() ([]uniter.RelationStatus, error) 155 156 // Watch returns a watcher for observing changes to the unit. 157 Watch() (watcher.NotifyWatcher, error) 158 159 // Destroy when called on a Alive unit, advances its lifecycle as far as 160 // possible; it otherwise has no effect. In most situations, the unit's 161 // life is just set to Dying; but if a principal unit that is not assigned 162 // to a provisioned machine is Destroyed, it will be removed from state 163 // directly. 164 Destroy() error 165 166 // Name returns the name of the unit. 167 Name() string 168 169 // Refresh updates the cached local copy of the unit's data. 170 Refresh() error 171 172 // Application returns the unit's application. 173 Application() (Application, error) 174 175 // Life returns the unit's lifecycle value. 176 Life() life.Value 177 } 178 179 // Application encapsulates the methods from 180 // state.Application required by a relationStateTracker. 181 type Application interface { 182 Life() life.Value 183 } 184 185 // Relation encapsulates the methods from 186 // state.Relation required by a relationStateTracker. 187 type Relation interface { 188 // Endpoint returns the endpoint of the relation for the application the 189 // uniter's managed unit belongs to. 190 Endpoint() (*uniter.Endpoint, error) 191 192 // Id returns the integer internal relation key. This is exposed 193 // because the unit agent needs to expose a value derived from this 194 // (as JUJU_RELATION_ID) to allow relation hooks to differentiate 195 // between relations with different applications. 196 Id() int 197 198 // Life returns the relation's current life state. 199 Life() life.Value 200 201 // OtherApplication returns the name of the application on the other 202 // end of the relation (from this unit's perspective). 203 OtherApplication() string 204 205 // Refresh refreshes the contents of the relation from the underlying 206 // state. It returns an error that satisfies errors.IsNotFound if the 207 // relation has been removed. 208 Refresh() error 209 210 // SetStatus updates the status of the relation. 211 SetStatus(relation.Status) error 212 213 // String returns the relation as a string. 214 String() string 215 216 // Suspended returns the relation's current suspended status. 217 Suspended() bool 218 219 // Tag returns the relation tag. 220 Tag() names.RelationTag 221 222 // Unit returns a uniter.RelationUnit for the supplied unit. 223 Unit(names.UnitTag) (RelationUnit, error) 224 225 // UpdateSuspended updates the in memory value of the 226 // relation's suspended attribute. 227 UpdateSuspended(bool) 228 } 229 230 // RelationUnit encapsulates the methods from 231 // state.RelationUnit required by a relationer. 232 type RelationUnit interface { 233 // ApplicationSettings returns a Settings which allows access to this 234 // unit's application settings within the relation. This can only be 235 // used from the leader unit. 236 ApplicationSettings() (*uniter.Settings, error) 237 238 // Endpoint returns the endpoint of the relation for the application the 239 // uniter's managed unit belongs to. 240 Endpoint() uniter.Endpoint 241 242 // EnterScope ensures that the unit has entered its scope in the relation. 243 // When the unit has already entered its relation scope, EnterScope will 244 // report success but make no changes to state. 245 EnterScope() error 246 247 // LeaveScope signals that the unit has left its scope in the relation. 248 // After the unit has left its relation scope, it is no longer a member 249 // of the relation. 250 LeaveScope() error 251 252 // Relation returns the relation associated with the unit. 253 Relation() Relation 254 255 // ReadSettings returns a map holding the settings of the unit with the 256 // supplied name within this relation. 257 ReadSettings(name string) (params.Settings, error) 258 259 // Settings returns a Settings which allows access to the unit's settings 260 // within the relation. 261 Settings() (*uniter.Settings, error) 262 } 263 264 // Relationer encapsulates the methods from relationer required by a stateTracker. 265 type Relationer interface { 266 // CommitHook persists the fact of the supplied hook's completion. 267 CommitHook(hi hook.Info) error 268 269 // ContextInfo returns a representation of the relationer's current state. 270 ContextInfo() *context.RelationInfo 271 272 // IsDying returns whether the relation is dying. 273 IsDying() bool 274 275 // IsImplicit returns whether the local relation endpoint is implicit. 276 IsImplicit() bool 277 278 // Join initializes local state and causes the unit to enter its relation 279 // scope, allowing its counterpart units to detect its presence and settings 280 // changes. 281 Join() error 282 283 // PrepareHook checks that the relation is in a state such that it makes 284 // sense to execute the supplied hook, and ensures that the relation context 285 // contains the latest relation state as communicated in the hook.Info. 286 PrepareHook(hi hook.Info) (string, error) 287 288 // RelationUnit returns the relation unit associated with this relationer instance. 289 RelationUnit() RelationUnit 290 291 // SetDying informs the relationer that the unit is departing the relation, 292 // and that the only hooks it should send henceforth are -departed hooks, 293 // until the relation is empty, followed by a -broken hook. 294 SetDying() error 295 }