1
from twisted.internet.defer import inlineCallbacks, returnValue
1
from twisted.internet.defer import inlineCallbacks, returnValue, succeed, fail
2
3
from juju.state.base import StateBase
3
from juju.state.errors import (UnitRelationStateNotFound, StateNotFound)
4
from juju.state.errors import (
5
UnitRelationStateNotFound, StateNotFound, RelationBrokenContextError)
4
6
from juju.state.service import ServiceStateManager, parse_service_name
5
7
from juju.state.utils import YAMLState
84
87
parse_service_name(self._unit_name)))
85
88
returnValue(self._service)
87
def _settings_path(self, unit_id):
88
return "/relations/%s/settings/%s" % (
89
self._unit_relation.internal_relation_id, unit_id)
92
91
def get_config(self):
93
92
"""Gather the configuration options.
248
251
yield super(RelationHookContext, self).flush()
249
252
returnValue(relation_setting_changes)
255
class DepartedRelationHookContext(HookContext):
256
"""A hook execution context suitable for running a relation-broken hook.
258
This context exposes the same interface as RelationHookContext, but:
260
* relation settings cannot be changed
261
* no remote units are reported to exist
262
* remote unit settings are not accessible
265
def __init__(self, client, unit_name, unit_id, relation_name, relation_id):
266
super(DepartedRelationHookContext, self).__init__(client, unit_name)
267
self._relation_name = relation_name
268
self._relation_id = relation_id
269
self._settings_path = "/relations/%s/settings/%s" % (
270
relation_id, unit_id)
272
# Cache of relation settings for the local unit
273
self._relation_cache = None
275
def get_members(self):
279
def get(self, unit_name):
280
# Only this unit's settings should be accessible.
281
if unit_name not in (None, self._unit_name):
282
raise RelationBrokenContextError(
283
"Cannot access other units in broken relation")
285
if self._relation_cache is None:
286
relation_data = YAMLState(self._client, self._settings_path)
288
yield relation_data.read(required=True)
289
self._relation_cache = dict(relation_data)
290
except StateNotFound:
291
self._relation_cache = {}
292
returnValue(self._relation_cache)
295
def get_value(self, unit_name, key):
296
settings = yield self.get(unit_name)
297
returnValue(settings.get(key, ""))
300
return fail(RelationBrokenContextError(
301
"Cannot change settings in broken relation"))
303
def set_value(self, key, value):
304
return fail(RelationBrokenContextError(
305
"Cannot change settings in broken relation"))
307
def delete_value(self, key):
308
return fail(RelationBrokenContextError(
309
"Cannot change settings in broken relation"))
311
def has_read(self, unit_name):
312
"""Has the context been used to access the settings of the unit.
314
if unit_name in (None, self._unit_name):
315
return self._relation_cache is not None