lp835216 client lib was not passing in kwargs when creating exceptions

This commit is contained in:
Dan Wendlandt 2011-08-29 18:59:14 -07:00
parent fa4e2c4c7d
commit 8e86ccf37d
2 changed files with 43 additions and 14 deletions

View File

@ -32,8 +32,8 @@ EXCEPTIONS = {
421: exceptions.NetworkInUse, 421: exceptions.NetworkInUse,
430: exceptions.PortNotFound, 430: exceptions.PortNotFound,
431: exceptions.StateInvalid, 431: exceptions.StateInvalid,
432: exceptions.PortInUse, 432: exceptions.PortInUseClient,
440: exceptions.AlreadyAttached} 440: exceptions.AlreadyAttachedClient}
class ApiCall(object): class ApiCall(object):
@ -131,7 +131,7 @@ class Client(object):
return conn.getresponse() return conn.getresponse()
def do_request(self, method, action, body=None, def do_request(self, method, action, body=None,
headers=None, params=None): headers=None, params=None, exception_args={}):
""" """
Connects to the server and issues a request. Connects to the server and issues a request.
Returns the result data, or raises an appropriate exception if Returns the result data, or raises an appropriate exception if
@ -190,7 +190,7 @@ class Client(object):
LOG.debug("Error message: %s", error_message) LOG.debug("Error message: %s", error_message)
# Create exception with HTTP status code and message # Create exception with HTTP status code and message
if res.status in EXCEPTIONS: if res.status in EXCEPTIONS:
raise EXCEPTIONS[res.status]() raise EXCEPTIONS[res.status](**exception_args)
# Add error code and message to exception arguments # Add error code and message to exception arguments
ex = Exception("Server returned error: %s" % status_code) ex = Exception("Server returned error: %s" % status_code)
ex.args = ([dict(status_code=status_code, ex.args = ([dict(status_code=status_code,
@ -254,7 +254,8 @@ class Client(object):
""" """
Fetches the details of a certain network Fetches the details of a certain network
""" """
return self.do_request("GET", self.network_path % (network)) return self.do_request("GET", self.network_path % (network),
exception_args={"net_id": network})
@ApiCall @ApiCall
def create_network(self, body=None): def create_network(self, body=None):
@ -268,14 +269,16 @@ class Client(object):
""" """
Updates a network Updates a network
""" """
return self.do_request("PUT", self.network_path % (network), body=body) return self.do_request("PUT", self.network_path % (network), body=body,
exception_args={"net_id": network})
@ApiCall @ApiCall
def delete_network(self, network): def delete_network(self, network):
""" """
Deletes the specified network Deletes the specified network
""" """
return self.do_request("DELETE", self.network_path % (network)) return self.do_request("DELETE", self.network_path % (network),
exception_args={"net_id": network})
@ApiCall @ApiCall
def list_ports(self, network): def list_ports(self, network):
@ -289,7 +292,8 @@ class Client(object):
""" """
Fetches the details of a certain port Fetches the details of a certain port
""" """
return self.do_request("GET", self.port_path % (network, port)) return self.do_request("GET", self.port_path % (network, port),
exception_args={"net_id": network, "port_id": port})
@ApiCall @ApiCall
def create_port(self, network, body=None): def create_port(self, network, body=None):
@ -297,14 +301,16 @@ class Client(object):
Creates a new port on a given network Creates a new port on a given network
""" """
body = self.serialize(body) body = self.serialize(body)
return self.do_request("POST", self.ports_path % (network), body=body) return self.do_request("POST", self.ports_path % (network), body=body,
exception_args={"net_id": network})
@ApiCall @ApiCall
def delete_port(self, network, port): def delete_port(self, network, port):
""" """
Deletes the specified port from a network Deletes the specified port from a network
""" """
return self.do_request("DELETE", self.port_path % (network, port)) return self.do_request("DELETE", self.port_path % (network, port),
exception_args={"net_id": network, "port_id": port})
@ApiCall @ApiCall
def set_port_state(self, network, port, body=None): def set_port_state(self, network, port, body=None):
@ -312,14 +318,18 @@ class Client(object):
Sets the state of the specified port Sets the state of the specified port
""" """
return self.do_request("PUT", return self.do_request("PUT",
self.port_path % (network, port), body=body) self.port_path % (network, port), body=body,
exception_args={"net_id": network,
"port_id": port,
"port_state": str(body)})
@ApiCall @ApiCall
def show_port_attachment(self, network, port): def show_port_attachment(self, network, port):
""" """
Fetches the attachment-id associated with the specified port Fetches the attachment-id associated with the specified port
""" """
return self.do_request("GET", self.attachment_path % (network, port)) return self.do_request("GET", self.attachment_path % (network, port),
exception_args={"net_id": network, "port_id": port})
@ApiCall @ApiCall
def attach_resource(self, network, port, body=None): def attach_resource(self, network, port, body=None):
@ -327,7 +337,10 @@ class Client(object):
Sets the attachment-id of the specified port Sets the attachment-id of the specified port
""" """
return self.do_request("PUT", return self.do_request("PUT",
self.attachment_path % (network, port), body=body) self.attachment_path % (network, port), body=body,
exception_args={"net_id": network,
"port_id": port,
"attach_id": str(body)})
@ApiCall @ApiCall
def detach_resource(self, network, port): def detach_resource(self, network, port):
@ -335,4 +348,5 @@ class Client(object):
Removes the attachment-id of the specified port Removes the attachment-id of the specified port
""" """
return self.do_request("DELETE", return self.do_request("DELETE",
self.attachment_path % (network, port)) self.attachment_path % (network, port),
exception_args={"net_id": network, "port_id": port})

View File

@ -111,6 +111,21 @@ class AlreadyAttached(QuantumException):
"already plugged into port %(att_port_id)s") "already plugged into port %(att_port_id)s")
# NOTE: on the client side, we often do not know all of the information
# that is known on the server, thus, we create separate exception for
# those scenarios
class PortInUseClient(QuantumException):
message = _("Unable to complete operation on port %(port_id)s " \
"for network %(net_id)s. An attachment " \
"is plugged into the logical port.")
class AlreadyAttachedClient(QuantumException):
message = _("Unable to plug the attachment %(att_id)s into port " \
"%(port_id)s for network %(net_id)s. The attachment is " \
"already plugged into another port.")
class Duplicate(Error): class Duplicate(Error):
pass pass