diff --git a/branches/ucs-3.2/ucs-3.2-1/base/univention-lib/python/umc_connection.py b/branches/ucs-3.2/ucs-3.2-1/base/univention-lib/python/umc_connection.py index f22c093..0b38906 100644 --- a/branches/ucs-3.2/ucs-3.2-1/base/univention-lib/python/umc_connection.py +++ b/branches/ucs-3.2/ucs-3.2-1/base/univention-lib/python/umc_connection.py @@ -32,22 +32,37 @@ # . # stdlib -from httplib import HTTPSConnection, HTTPException +from httplib import HTTPSConnection, HTTPException, OK as HTTP_OK, FORBIDDEN from json import loads, dumps from socket import error as SocketError # univention from univention.config_registry import ConfigRegistry -ucr = ConfigRegistry() -ucr.load() + + +class UMCConnectionError(HTTPException): + pass + + +class UMCConnectionUnknownCommand(UMCConnectionError): + pass + + +class UMCConnectionRequestError(UMCConnectionError): + pass + + +class UMCConnectionAuthenticationError(UMCConnectionRequestError): + pass + class UMCConnection(object): def __init__(self, host, username=None, password=None, error_handler=None): self._host = host self._headers = { - 'Content-Type' : 'application/json; charset=UTF-8' + 'Content-Type': 'application/json; charset=UTF-8' } - self._error_handler=error_handler + self._error_handler = error_handler if username is not None: self.auth(username, password) @@ -61,6 +76,9 @@ class UMCConnection(object): def get_machine_connection(cls, error_handler=None): '''Creates a connection with the credentials of the local host to the DC Master''' + ucr = ConfigRegistry() + ucr.load() + username = '%s$' % ucr.get('hostname') password = '' try: @@ -82,7 +100,7 @@ class UMCConnection(object): '''Tries to authenticate against the host and preserves the cookie. Has to be done only once (but keep in mind that the session probably expires after 10 minutes of inactivity)''' - data = self.build_data({'username' : username, 'password' : password}) + data = self.build_data({'username': username, 'password': password}) con = self.get_connection() try: con.request('POST', '/umcp/auth', data) @@ -91,23 +109,23 @@ class UMCConnection(object): if self._error_handler: self._error_handler(str(e)) error_message = '%s: Authentication failed while contacting: %s' % (self._host, e) - raise HTTPException(error_message) + raise UMCConnectionAuthenticationError(error_message) else: try: response = con.getresponse() cookie = response.getheader('set-cookie') if cookie is None: - raise ValueError('No cookie') + raise UMCConnectionAuthenticationError('No cookie') self._headers['Cookie'] = cookie except Exception as e: if self._error_handler: self._error_handler(str(e)) error_message = '%s: Authentication failed: %s' % (self._host, response.read()) - raise HTTPException(error_message) + raise UMCConnectionAuthenticationError(error_message) def build_data(self, data, flavor=None): '''Returns a dictionary as expected by the UMC Server''' - data = {'options' : data} + data = {'options': data} if flavor: data['flavor'] = flavor return dumps(data) @@ -130,15 +148,14 @@ class UMCConnection(object): umcp_command = '%s/%s' % (umcp_command, url) con.request('POST', umcp_command, data, headers=self._headers) response = con.getresponse() - if response.status != 200: + if response.status != HTTP_OK: # 200 error_message = '%s on %s (%s): %s' % (response.status, self._host, url, response.read()) - if response.status == 403: + if response.status == FORBIDDEN: # 403 # 403 is either command is unknown # or command is known but forbidden if self._error_handler: self._error_handler(error_message) - raise NotImplementedError('command forbidden: %s' % url) - raise HTTPException(error_message) + raise UMCConnectionUnknownCommand('command forbidden: %s' % url) + raise UMCConnectionRequestError(error_message) content = response.read() return loads(content)['result'] -