raiseCLIError(e)
return _raise
+ @classmethod
+ def _connection(this, foo, base_url):
+ def _raise(self, *args, **kwargs):
+ try:
+ foo(self, *args, **kwargs)
+ except ClientError as ce:
+ if ce.status == 401:
+ raiseCLIError(ce, 'Authorization failed', details=[
+ 'Make sure a valid token is provided:',
+ ' to check if token is valid: /astakos authenticate',
+ ' to set token: /config set [.server.]token <token>',
+ ' to get current token: /config get [server.]token'])
+ elif ce.status in range(-12, 200) + [403, 500]:
+ raiseCLIError(ce, importance=3, details=[
+ 'Check if service is up or set to url %s' % base_url,
+ ' to get url: /config get %s' % base_url,
+ ' to set url: /config set %s <URL>' % base_url])
+ raise
+ return _raise
+
class astakos(object):
_token_details = [
- 'See if token is set: /config get token',
- 'If not, set a token:',
+ 'To check default token: /config get token',
+ 'If set/update a token:',
'* (permanent): /config set token <token>',
'* (temporary): re-run with <token> parameter']
@classmethod
- def init(this, foo):
- @generic.all
+ def load(this, foo):
def _raise(self, *args, **kwargs):
r = foo(self, *args, **kwargs)
try:
class history(object):
@classmethod
def init(this, foo):
- @generic.all
def _raise(self, *args, **kwargs):
r = foo(self, *args, **kwargs)
if not hasattr(self, 'history'):
@classmethod
def _get_cmd_ids(this, foo):
- @generic.all
def _raise(self, cmd_ids, *args, **kwargs):
if not cmd_ids:
raise CLISyntaxError('Usage: <id1|id1-id2> [id3|id3-id4] ...',
return foo(self, cmd_ids, *args, **kwargs)
return _raise
+
+class cyclades(object):
+ @classmethod
+ def connection(this, foo):
+ return generic._connection(foo, 'compute.url')
+
+
+class plankton(object):
+
+ about_image_id = ['To see a list of available image ids: /image list']
+
+ @classmethod
+ def connection(this, foo):
+ return generic._connection(foo, 'image.url')
+
+ @classmethod
+ def id(this, foo):
+ def _raise(self, image_id, *args, **kwargs):
+ try:
+ foo(self, image_id, *args, **kwargs)
+ except ClientError as ce:
+ if ce.status == 404 and image_id:
+ raiseCLIError(ce,
+ 'No image with id %s found' % image_id,
+ details=this.about_image_id)
+ raise
+ return _raise
from kamaki.cli.argument import IntArgument
from kamaki.cli.commands.cyclades_cli import _init_cyclades
from kamaki.cli.commands.cyclades_cli import raise_if_connection_error
-from kamaki.cli.commands import _command_init
+from kamaki.cli.commands import _command_init, errors
image_cmds = CommandTree(
class _init_image(_command_init):
+ @errors.generic.all
+ def _run(self):
+ token = self.config.get('image', 'token')\
+ or self.config.get('compute', 'token')\
+ or self.config.get('global', 'token')
+ base_url = self.config.get('image', 'url')\
+ or self.config.get('compute', 'url')\
+ or self.config.get('global', 'url')
+ self.client = ImageClient(base_url=base_url, token=token)
+
def main(self):
- try:
- token = self.config.get('image', 'token')\
- or self.config.get('compute', 'token')\
- or self.config.get('global', 'token')
- base_url = self.config.get('image', 'url')\
- or self.config.get('compute', 'url')\
- or self.config.get('global', 'url')
- self.client = ImageClient(base_url=base_url, token=token)
- except Exception as err:
- raiseCLIError(err)
+ self._run()
@command(image_cmds)
'--more')
)
- def main(self):
- super(self.__class__, self).main()
+ @errors.generic.all
+ @errors.cyclades.connection
+ def _run(self):
+ super(self.__class__, self)._run()
filters = {}
for arg in set([
'container_format',
order = self['order']
detail = self['detail']
- try:
- images = self.client.list_public(detail, filters, order)
- except ClientError as ce:
- raise_if_connection_error(ce, base_url='image.url')
- raiseCLIError(ce)
- except Exception as err:
- raiseCLIError(err)
+ images = self.client.list_public(detail, filters, order)
if self['more']:
print_items(
images,
else:
print_items(images, title=('name',), with_enumeration=True)
+ def main(self):
+ super(self.__class__, self)._run()
+ self._run()
+
@command(image_cmds)
class image_meta(_init_image):
- image os properties (os, fs, etc.)
"""
- def main(self, image_id):
- super(self.__class__, self).main()
- try:
- image = self.client.get_meta(image_id)
- except ClientError as ce:
- if ce.status == 404:
- raiseCLIError(ce,
- 'No image with id %s found' % image_id,
- details=about_image_id)
- raise_if_connection_error(ce, base_url='image.url')
- raiseCLIError(ce)
- except Exception as err:
- raiseCLIError(err)
+ @errors.generic.all
+ @errors.plankton.connection
+ @errors.plankton.id
+ def _run(self, image_id):
+ image = self.client.get_meta(image_id)
print_dict(image)
+ def main(self, image_id):
+ super(self.__class__, self)._run()
+ self._run(image_id)
+
@command(image_cmds)
class image_register(_init_image):
update=FlagArgument('update existing image properties', '--update')
)
- def main(self, name, location):
- super(self.__class__, self).main()
+ @errors.generic.all
+ @errors.plankton.connection
+ def _run(self, name, location):
if not location.startswith('pithos://'):
account = self.config.get('store', 'account') \
or self.config.get('global', 'account')
]).intersection(self.arguments):
params[key] = self[key]
- try:
properties = self['properties']
- if self['update']:
- self.client.reregister(location, name, params, properties)
- else:
- self.client.register(name, location, params, properties)
- except ClientError as ce:
- raise_if_connection_error(ce, base_url='image.url')
- raiseCLIError(ce)
- except Exception as err:
- raiseCLIError(err)
+ if self['update']:
+ self.client.reregister(location, name, params, properties)
+ else:
+ self.client.register(name, location, params, properties)
+
+ def main(self, name, location):
+ super(self.__class__, self)._run()
+ self._run(name, location)
@command(image_cmds)