Commit a00de254 authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Seperate error handling from main cli classes

So far tested in astakos only
parent 36526b3c
......@@ -31,14 +31,10 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.command
from traceback import print_stack, print_exc
from kamaki.clients import ClientError
from kamaki.cli.errors import raiseCLIError
from kamaki.cli import command, _debug
from kamaki.cli import command
from kamaki.clients.astakos import AstakosClient
from kamaki.cli.utils import print_dict
from kamaki.cli.commands import _command_init
from kamaki.cli.commands import _command_init, errors
from kamaki.cli.command_tree import CommandTree
astakos_cmds = CommandTree('astakos', 'Astakos API commands')
......@@ -47,29 +43,14 @@ _commands = [astakos_cmds]
class _astakos_init(_command_init):
@errors.astakos.init
def main(self):
token = self.config.get('astakos', 'token')\
or self.config.get('global', 'token')
base_url = self.config.get('astakos', 'url')\
or self.config.get('global', 'url')
if base_url is None:
raiseCLIError(None, 'Missing astakos server URL')
self.client = AstakosClient(base_url=base_url, token=token)
_token_details = [
'See if token is set: /config get token',
'If not, set a token:',
'* (permanent): /config set token <token>',
'* (temporary): re-run with <token> parameter']
def _raise(self, error):
if _debug:
print_stack()
print_exc(error)
if isinstance(error, ClientError) and error.status == 401:
raiseCLIError(error, details=self._token_details)
raiseCLIError(error)
@command(astakos_cmds)
class astakos_authenticate(_astakos_init):
......@@ -81,23 +62,8 @@ class astakos_authenticate(_astakos_init):
Token can also be provided as a parameter
"""
def _raise(self, error, some_token=None):
if isinstance(error, ClientError) and error.status == 401:
some_token = some_token if some_token else self.client.token
if some_token:
raiseCLIError(error,
'Authorization failed for token %s' % some_token,
details=self._token_details)
else:
raiseCLIError(error,
'No token provided',
details=self._token_details)
super(self.__class__, self)._raise(error)
@errors.astakos.authenticate
def main(self, custom_token=None):
super(self.__class__, self).main()
try:
reply = self.client.authenticate(custom_token)
except Exception as e:
self._raise(e)
reply = self.client.authenticate(custom_token)
print_dict(reply)
# Copyright 2011-2012 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.command
from traceback import print_stack, print_exc
import logging
from kamaki.clients import ClientError
from kamaki.cli.errors import CLIError, raiseCLIError
from kamaki.cli import _debug, kloger
sendlog = logging.getLogger('clients.send')
datasendlog = logging.getLogger('data.send')
recvlog = logging.getLogger('clients.recv')
datarecvlog = logging.getLogger('data.recv')
class generic(object):
@classmethod
def all(this, foo):
def _raise(self, *args, **kwargs):
try:
return foo(self, *args, **kwargs)
except Exception as e:
if _debug:
print_stack()
print_exc(e)
raiseCLIError(e)
return _raise
class astakos(object):
_token_details = [
'See if token is set: /config get token',
'If not, set a token:',
'* (permanent): /config set token <token>',
'* (temporary): re-run with <token> parameter']
@classmethod
def init(this, foo):
@generic.all
def _raise(self, *args, **kwargs):
r = foo(self, *args, **kwargs)
try:
client = getattr(self, 'client')
except AttributeError as ae:
raiseCLIError(ae, 'Client setup failure', importance=3)
if not getattr(client, 'token', False):
kloger.warning(
'No permanent token (try: kamaki config set token <tkn>)')
if not getattr(client, 'base_url', False):
raise CLIError('Missing astakos server URL',
importance=3,
details=['Please check if astakos.url is set correctly',
'To get astakos url: /config get astakos.url',
'To set astakos url: /config set astakos.url <URL>'])
return r
return _raise
@classmethod
def authenticate(this, foo):
@generic.all
def _raise(self, *args, **kwargs):
try:
return foo(self, *args, **kwargs)
except ClientError as ce:
if ce.status == 401:
token = kwargs.get('custom_token', 0) or self.client.token
raiseCLIError(ce,
'Authorization failed for token %s' % token if token\
else 'No token provided',
details=this._token_details)
return _raise
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment