~ubuntu-branches/ubuntu/precise/juju/precise

« back to all changes in this revision

Viewing changes to juju/state/hook.py

  • Committer: Package Import Robot
  • Author(s): Clint Byrum
  • Date: 2012-02-16 03:44:02 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20120216034402-zi79nfc84vb0bj88
Tags: 0.5+bzr457-0ubuntu1
New upstream snapshot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from twisted.internet.defer import inlineCallbacks, returnValue
 
1
from twisted.internet.defer import inlineCallbacks, returnValue, succeed, fail
 
2
 
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
6
8
 
71
73
    def get_local_unit_state(self):
72
74
        """Return ServiceUnitState for the local service unit."""
73
75
        service_state_manager = ServiceStateManager(self._client)
74
 
        unit_state = yield service_state_manager.get_unit_state(self._unit_name)
 
76
        unit_state = yield service_state_manager.get_unit_state(
 
77
            self._unit_name)
75
78
        returnValue(unit_state)
76
79
 
77
80
    @inlineCallbacks
84
87
                    parse_service_name(self._unit_name)))
85
88
        returnValue(self._service)
86
89
 
87
 
    def _settings_path(self, unit_id):
88
 
        return "/relations/%s/settings/%s" % (
89
 
            self._unit_relation.internal_relation_id, unit_id)
90
 
 
91
90
    @inlineCallbacks
92
91
    def get_config(self):
93
92
        """Gather the configuration options.
131
130
        # A cache of related units in the relation.
132
131
        self._members = members
133
132
 
 
133
    def _settings_path(self, unit_id):
 
134
        return "/relations/%s/settings/%s" % (
 
135
            self._unit_relation.internal_relation_id, unit_id)
 
136
 
134
137
    @inlineCallbacks
135
138
    def get_members(self):
136
139
        """Gets the related unit members of the relation with caching."""
248
251
        yield super(RelationHookContext, self).flush()
249
252
        returnValue(relation_setting_changes)
250
253
 
 
254
 
 
255
class DepartedRelationHookContext(HookContext):
 
256
    """A hook execution context suitable for running a relation-broken hook.
 
257
 
 
258
    This context exposes the same interface as RelationHookContext, but:
 
259
 
 
260
    * relation settings cannot be changed
 
261
    * no remote units are reported to exist
 
262
    * remote unit settings are not accessible
 
263
    """
 
264
 
 
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)
 
271
 
 
272
        # Cache of relation settings for the local unit
 
273
        self._relation_cache = None
 
274
 
 
275
    def get_members(self):
 
276
        return succeed([])
 
277
 
 
278
    @inlineCallbacks
 
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")
 
284
 
 
285
        if self._relation_cache is None:
 
286
            relation_data = YAMLState(self._client, self._settings_path)
 
287
            try:
 
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)
 
293
 
 
294
    @inlineCallbacks
 
295
    def get_value(self, unit_name, key):
 
296
        settings = yield self.get(unit_name)
 
297
        returnValue(settings.get(key, ""))
 
298
 
 
299
    def set(self, data):
 
300
        return fail(RelationBrokenContextError(
 
301
            "Cannot change settings in broken relation"))
 
302
 
 
303
    def set_value(self, key, value):
 
304
        return fail(RelationBrokenContextError(
 
305
            "Cannot change settings in broken relation"))
 
306
 
 
307
    def delete_value(self, key):
 
308
        return fail(RelationBrokenContextError(
 
309
            "Cannot change settings in broken relation"))
 
310
 
 
311
    def has_read(self, unit_name):
 
312
        """Has the context been used to access the settings of the unit.
 
313
        """
 
314
        if unit_name in (None, self._unit_name):
 
315
            return self._relation_cache is not None
 
316
        return False