~hazmat/pyjuju/rapi-rollup

« back to all changes in this revision

Viewing changes to juju/rapi/context.py

  • Committer: Kapil Thangavelu
  • Date: 2012-12-17 22:14:37 UTC
  • mfrom: (616.1.2 rapi-login)
  • Revision ID: kapil@canonical.com-20121217221437-lhy4eck58wx3sf5n
merge rapi-login

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from StringIO import StringIO
2
 
from twisted.internet.defer import maybeDeferred, succeed
 
2
from twisted.internet.defer import (
 
3
    maybeDeferred, succeed, returnValue, inlineCallbacks)
3
4
 
4
5
from juju.rapi.cmd import add_relation
5
6
from juju.rapi.cmd import add_unit
22
23
 
23
24
from juju.rapi import rest
24
25
 
 
26
from juju.state.security import Principal
 
27
 
25
28
import logging
 
29
import zookeeper
26
30
 
27
31
log = logging.getLogger('juju.rapi.context')
28
32
 
78
82
        d = maybeDeferred(func, *args, **kw)
79
83
        return d.addCallback(
80
84
            lambda r: {'result': r, 'log': self.log.reset()}
81
 
            ).addErrback(self._on_error)
 
85
        ).addErrback(self._on_error)
82
86
 
83
87
    def _on_error(self, failure):
84
88
        io = StringIO()
134
138
        return self._invoke(
135
139
            constraints_get.constraints_get,
136
140
            self,
137
 
            named_entities
138
 
            )
 
141
            named_entities)
139
142
 
140
143
    def get_config(self, service_name):
141
144
        """Get configuration of a service.
163
166
            constraints,
164
167
            config,
165
168
            config_raw,
166
 
            num_units
167
 
            )
 
169
            num_units)
168
170
 
169
171
    def debug_hooks(self, unit_name, hook_names=()):
170
172
        """Enable hook debug on a unit.
206
208
            self,
207
209
            data)
208
210
 
 
211
    def login(self, user, password):
 
212
        return self._invoke(
 
213
            self._login,
 
214
            user,
 
215
            password)
 
216
 
 
217
    @inlineCallbacks
 
218
    def _login(self, user, password):
 
219
        principal = Principal(user, password)
 
220
        yield principal.attach(self.client)
 
221
        try:
 
222
            yield self.client.get("/login")
 
223
        except (zookeeper.NoAuthException, zookeeper.AuthFailedException):
 
224
            self.log.error("Invalid credentials")
 
225
            raise
 
226
        else:
 
227
            self.log.info("Login success")
 
228
        returnValue(True)
 
229
 
209
230
    def remove_relation(self, endpoint_a, endpoint_b):
210
231
        """Remove a relation from a service(s).
211
232
        """