Commit f4c2917e authored by Stavros Sachtouris's avatar Stavros Sachtouris

Manage project_id errors in all API commands

Fixes #21
parent 8f8051f4
......@@ -40,17 +40,18 @@ from kamaki.cli.utils import (
filter_dicts_by_dict)
from kamaki.cli.argument import ValueArgument, ProgressBarArgument
from kamaki.cli.errors import CLIInvalidArgument, CLIBaseUrlError
from kamaki.cli.cmds import errors
log = get_logger(__name__)
def dont_raise(*errors):
def dont_raise(*errs):
def decorator(func):
def wrap(*args, **kwargs):
try:
return func(*args, **kwargs)
except errors as e:
except errs as e:
log.debug('Suppressed error %s while calling %s(%s)' % (
e, func.__name__, ','.join(['%s' % i for i in args] + [
('%s=%s' % items) for items in kwargs.items()])))
......@@ -130,6 +131,10 @@ class CommandInit(object):
raise CLIBaseUrlError(service=service)
return cls(URL, TOKEN)
@errors.Astakos.project_id
def _project_id_exists(self, project_id):
self.astakos.get_client().get_project(project_id)
@dont_raise(UnicodeError)
def write(self, s):
self._out.write(s.encode(pref_enc, errors='replace'))
......
......@@ -523,6 +523,8 @@ class server_create(_CycladesInit, OptionalOutput, _ServerWait):
self._network_exists(network_id=net['uuid'])
if 'fixed_ip' in net:
self._ip_ready(net['fixed_ip'], net['uuid'], ce)
if self['project_id'] and ce.status in (400, 403, 404):
self._project_id_exists(project=self['project_id'])
raise
def main(self):
......@@ -678,7 +680,12 @@ class server_reassign(_CycladesInit, OptionalOutput):
@errors.Cyclades.connection
@errors.Cyclades.server_id
def _run(self, server_id):
self.client.reassign_server(server_id, self['project_id'])
try:
self.client.reassign_server(server_id, self['project_id'])
except ClientError as ce:
if ce.status in (400, 403, 404):
self._project_id_exists(project=self['project_id'])
raise
def main(self, server_id):
super(self.__class__, self)._run()
......
......@@ -179,7 +179,7 @@ class Astakos(object):
' kamaki membership list',
'%s %s' % (getattr(ce, 'status', ''), ce)])
_raise.__name__ = func.__name__
return _rause
return _raise
class History(object):
......
......@@ -201,11 +201,15 @@ class network_create(_NetworkInit, OptionalOutput):
@errors.Generic.all
@errors.Cyclades.connection
def _run(self):
net = self.client.create_network(
self['network_type'],
name=self['name'],
shared=self['shared'],
project_id=self['project_id'])
try:
net = self.client.create_network(
self['network_type'],
name=self['name'],
shared=self['shared'],
project_id=self['project_id'])
except ClientError as ce:
if self['project_id'] and ce.status in (400, 403, 404):
self._project_id_exists(project_id=self['project_id'])
self.print_(net, self.print_dict)
def main(self):
......@@ -227,7 +231,12 @@ class network_reassign(_NetworkInit, OptionalOutput):
@errors.Cyclades.network_permissions
@errors.Cyclades.network_id
def _run(self, network_id):
self.client.reassign_network(network_id, self['project_id'])
try:
self.client.reassign_network(network_id, self['project_id'])
except ClientError as ce:
if ce.status in (400, 403, 404):
self._project_id_exists(project_id=self['project_id'])
raise
def main(self, network_id):
super(self.__class__, self)._run()
......@@ -655,6 +664,8 @@ class ip_create(_NetworkInit, OptionalOutput):
self._network_exists(network_id=network_id)
if ip:
self._ip_exists(ip, network_id, ce)
if self['project_id'] and ce.status in (400, 403, 404):
self._project_id_exists(project_id=self['project_id'])
raise
def main(self):
......
......@@ -1633,10 +1633,13 @@ class container_create(_PithosAccount):
metadata=self['meta'],
success=(201, ))
except ClientError as ce:
print 'WHAAAA?'
if ce.status in (202, ):
raise CLIError(
'Container %s alread exists' % self.container, details=[
'Delete %s or choose another name' % self.container])
elif self['project_id'] and ce.status in (400, 403, 404):
self._project_id_exists(project_id=self['project_id'])
raise
def main(self, new_container):
......@@ -1713,7 +1716,12 @@ class container_reassign(_PithosAccount):
@errors.Pithos.connection
@errors.Pithos.container
def _run(self):
self.client.reassign_container(self['project_id'])
try:
self.client.reassign_container(self['project_id'])
except ClientError as ce:
if ce.status in (400, 403, 404):
self._project_id_exists(project_id=self['project_id'])
raise
def main(self, container):
super(self.__class__, self)._run()
......
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