Merging: lp:~danwent/quantum/client-lib

Some minor tweaks to the Quantum Client Library, based on my experience using the library.
This commit is contained in:
Salvatore Orlando 2011-08-12 12:17:30 +01:00
commit b9029ee823
3 changed files with 99 additions and 79 deletions

View File

@ -104,7 +104,7 @@ def detail_net(manager, *args):
def api_detail_net(client, *args):
tid, nid = args
try:
res = client.list_network_details(nid)["networks"]["network"]
res = client.show_network_details(nid)["networks"]["network"]
except Exception, e:
LOG.error("Failed to get network details: %s" % e)
return
@ -119,7 +119,7 @@ def api_detail_net(client, *args):
print "Remote Interfaces on Virtual Network:%s\n" % nid
for port in ports["ports"]:
pid = port["id"]
res = client.list_port_attachments(nid, pid)
res = client.show_port_attachment(nid, pid)
LOG.debug(res)
remote_iface = res["attachment"]
print "\tRemote interface:%s" % remote_iface
@ -214,7 +214,7 @@ def detail_port(manager, *args):
def api_detail_port(client, *args):
tid, nid, pid = args
try:
port = client.list_port_details(nid, pid)["ports"]["port"]
port = client.show_port_details(nid, pid)["ports"]["port"]
except Exception, e:
LOG.error("Failed to get port details: %s" % e)
return

View File

@ -57,7 +57,8 @@ class Client(object):
attachment_path = "/networks/%s/ports/%s/attachment"
def __init__(self, host="127.0.0.1", port=9696, use_ssl=False, tenant=None,
format="xml", testingStub=None, key_file=None, cert_file=None):
format="xml", testingStub=None, key_file=None, cert_file=None,
logger=None):
"""
Creates a new client to some service.
@ -79,6 +80,7 @@ class Client(object):
self.testingStub = testingStub
self.key_file = key_file
self.cert_file = cert_file
self.logger = logger
def get_connection_type(self):
"""
@ -135,14 +137,26 @@ class Client(object):
else:
c = connection_type(self.host, self.port)
if self.logger:
self.logger.debug("Quantum Client Request:\n" \
+ method + " " + action + "\n")
if body:
self.logger.debug(body)
c.request(method, action, body, headers)
res = c.getresponse()
status_code = self.get_status_code(res)
data = res.read()
if self.logger:
self.logger.debug("Quantum Client Reply (code = %s) :\n %s" \
% (str(status_code), data))
if status_code in (httplib.OK,
httplib.CREATED,
httplib.ACCEPTED,
httplib.NO_CONTENT):
return self.deserialize(res)
return self.deserialize(data, status_code)
else:
raise Exception("Server returned error: %s" % res.read())
@ -161,13 +175,18 @@ class Client(object):
return response.status
def serialize(self, data):
if type(data) is dict:
if data is None:
return None
elif type(data) is dict:
return Serializer().serialize(data, self.content_type())
else:
raise Exception("unable to deserialize object of type = '%s'" \
% type(data))
def deserialize(self, data):
if self.get_status_code(data) == 202:
return data.read()
return Serializer().deserialize(data.read(), self.content_type())
def deserialize(self, data, status_code):
if status_code == 202:
return data
return Serializer().deserialize(data, self.content_type())
def content_type(self, format=None):
if not format:
@ -177,85 +196,86 @@ class Client(object):
@api_call
def list_networks(self):
"""
Queries the server for a list of networks
Fetches a list of all networks for a tenant
"""
return self.do_request("GET", self.networks_path)
@api_call
def list_network_details(self, network):
def show_network_details(self, network):
"""
Queries the server for the details of a certain network
Fetches the details of a certain network
"""
return self.do_request("GET", self.network_path % (network))
@api_call
def create_network(self, body=None):
"""
Creates a new network on the server
Creates a new network
"""
return self.do_request("POST", self.networks_path, body=body)
@api_call
def update_network(self, network, body=None):
"""
Updates a network on the server
Updates a network
"""
return self.do_request("PUT", self.network_path % (network), body=body)
@api_call
def delete_network(self, network):
"""
Deletes a network on the server
Deletes the specified network
"""
return self.do_request("DELETE", self.network_path % (network))
@api_call
def list_ports(self, network):
"""
Queries the server for a list of ports on a given network
Fetches a list of ports on a given network
"""
return self.do_request("GET", self.ports_path % (network))
@api_call
def list_port_details(self, network, port):
def show_port_details(self, network, port):
"""
Queries the server for a list of ports on a given network
Fetches the details of a certain port
"""
return self.do_request("GET", self.port_path % (network, port))
@api_call
def create_port(self, network):
def create_port(self, network, body=None):
"""
Creates a new port on a network on the server
Creates a new port on a given network
"""
return self.do_request("POST", self.ports_path % (network))
body = self.serialize(body)
return self.do_request("POST", self.ports_path % (network), body=body)
@api_call
def delete_port(self, network, port):
"""
Deletes a port from a network on the server
Deletes the specified port from a network
"""
return self.do_request("DELETE", self.port_path % (network, port))
@api_call
def set_port_state(self, network, port, body=None):
"""
Sets the state of a port on the server
Sets the state of the specified port
"""
return self.do_request("PUT",
self.port_path % (network, port), body=body)
@api_call
def list_port_attachments(self, network, port):
def show_port_attachment(self, network, port):
"""
Deletes a port from a network on the server
Fetches the attachment-id associated with the specified port
"""
return self.do_request("GET", self.attachment_path % (network, port))
@api_call
def attach_resource(self, network, port, body=None):
"""
Deletes a port from a network on the server
Sets the attachment-id of the specified port
"""
return self.do_request("PUT",
self.attachment_path % (network, port), body=body)
@ -263,7 +283,7 @@ class Client(object):
@api_call
def detach_resource(self, network, port):
"""
Deletes a port from a network on the server
Removes the attachment-id of the specified port
"""
return self.do_request("DELETE",
self.attachment_path % (network, port))

View File

@ -131,19 +131,19 @@ class APITest(unittest.TestCase):
LOG.debug("_test_list_networks - tenant:%s "\
"- format:%s - END", format, tenant)
def _test_list_network_details(self,
def _test_show_network_details(self,
tenant=TENANT_1, format='json', status=200):
LOG.debug("_test_list_network_details - tenant:%s "\
LOG.debug("_test_show_network_details - tenant:%s "\
"- format:%s - START", format, tenant)
self._assert_sanity(self.client.list_network_details,
self._assert_sanity(self.client.show_network_details,
status,
"GET",
"networks/001",
data=["001"],
params={'tenant': tenant, 'format': format})
LOG.debug("_test_list_network_details - tenant:%s "\
LOG.debug("_test_show_network_details - tenant:%s "\
"- format:%s - END", format, tenant)
def _test_create_network(self, tenant=TENANT_1, format='json', status=200):
@ -203,19 +203,19 @@ class APITest(unittest.TestCase):
LOG.debug("_test_list_ports - tenant:%s "\
"- format:%s - END", format, tenant)
def _test_list_port_details(self,
def _test_show_port_details(self,
tenant=TENANT_1, format='json', status=200):
LOG.debug("_test_list_port_details - tenant:%s "\
LOG.debug("_test_show_port_details - tenant:%s "\
"- format:%s - START", format, tenant)
self._assert_sanity(self.client.list_port_details,
self._assert_sanity(self.client.show_port_details,
status,
"GET",
"networks/001/ports/001",
data=["001", "001"],
params={'tenant': tenant, 'format': format})
LOG.debug("_test_list_port_details - tenant:%s "\
LOG.debug("_test_show_port_details - tenant:%s "\
"- format:%s - END", format, tenant)
def _test_create_port(self, tenant=TENANT_1, format='json', status=200):
@ -261,19 +261,19 @@ class APITest(unittest.TestCase):
LOG.debug("_test_set_port_state - tenant:%s "\
"- format:%s - END", format, tenant)
def _test_list_port_attachments(self,
def _test_show_port_attachment(self,
tenant=TENANT_1, format='json', status=200):
LOG.debug("_test_list_port_attachments - tenant:%s "\
LOG.debug("_test_show_port_attachment - tenant:%s "\
"- format:%s - START", format, tenant)
self._assert_sanity(self.client.list_port_attachments,
self._assert_sanity(self.client.show_port_attachment,
status,
"GET",
"networks/001/ports/001/attachment",
data=["001", "001"],
params={'tenant': tenant, 'format': format})
LOG.debug("_test_list_port_attachments - tenant:%s "\
LOG.debug("_test_show_port_attachment - tenant:%s "\
"- format:%s - END", format, tenant)
def _test_attach_resource(self, tenant=TENANT_1,
@ -345,23 +345,23 @@ class APITest(unittest.TestCase):
def test_list_networks_error_401(self):
self._test_list_networks(status=401)
def test_list_network_details_json(self):
self._test_list_network_details(format='json')
def test_show_network_details_json(self):
self._test_show_network_details(format='json')
def test_list_network_details_xml(self):
self._test_list_network_details(format='xml')
def test_show_network_details_xml(self):
self._test_show_network_details(format='xml')
def test_list_network_details_alt_tenant(self):
self._test_list_network_details(tenant=TENANT_2)
def test_show_network_details_alt_tenant(self):
self._test_show_network_details(tenant=TENANT_2)
def test_list_network_details_error_470(self):
self._test_list_network_details(status=470)
def test_show_network_details_error_470(self):
self._test_show_network_details(status=470)
def test_list_network_details_error_401(self):
self._test_list_network_details(status=401)
def test_show_network_details_error_401(self):
self._test_show_network_details(status=401)
def test_list_network_details_error_420(self):
self._test_list_network_details(status=420)
def test_show_network_details_error_420(self):
self._test_show_network_details(status=420)
def test_create_network_json(self):
self._test_create_network(format='json')
@ -447,26 +447,26 @@ class APITest(unittest.TestCase):
def test_list_ports_error_420(self):
self._test_list_ports(status=420)
def test_list_port_details_json(self):
def test_show_port_details_json(self):
self._test_list_ports(format='json')
def test_list_port_details_xml(self):
def test_show_port_details_xml(self):
self._test_list_ports(format='xml')
def test_list_port_details_alt_tenant(self):
def test_show_port_details_alt_tenant(self):
self._test_list_ports(tenant=TENANT_2)
def test_list_port_details_error_470(self):
self._test_list_port_details(status=470)
def test_show_port_details_error_470(self):
self._test_show_port_details(status=470)
def test_list_port_details_error_401(self):
self._test_list_ports(status=401)
def test_show_port_details_error_401(self):
self._test_show_port_details(status=401)
def test_list_port_details_error_420(self):
self._test_list_ports(status=420)
def test_show_port_details_error_420(self):
self._test_show_port_details(status=420)
def test_list_port_details_error_430(self):
self._test_list_ports(status=430)
def test_show_port_details_error_430(self):
self._test_show_port_details(status=430)
def test_create_port_json(self):
self._test_create_port(format='json')
@ -546,29 +546,29 @@ class APITest(unittest.TestCase):
def test_set_port_state_error_431(self):
self._test_set_port_state(status=431)
def test_list_port_attachments_json(self):
self._test_list_port_attachments(format='json')
def test_show_port_attachment_json(self):
self._test_show_port_attachment(format='json')
def test_list_port_attachments_xml(self):
self._test_list_port_attachments(format='xml')
def test_show_port_attachment_xml(self):
self._test_show_port_attachment(format='xml')
def test_list_port_attachments_alt_tenant(self):
self._test_list_port_attachments(tenant=TENANT_2)
def test_show_port_attachment_alt_tenant(self):
self._test_show_port_attachment(tenant=TENANT_2)
def test_list_port_attachments_error_470(self):
self._test_list_port_attachments(status=470)
def test_show_port_attachment_error_470(self):
self._test_show_port_attachment(status=470)
def test_list_port_attachments_error_401(self):
self._test_list_port_attachments(status=401)
def test_show_port_attachment_error_401(self):
self._test_show_port_attachment(status=401)
def test_list_port_attachments_error_400(self):
self._test_list_port_attachments(status=400)
def test_show_port_attachment_error_400(self):
self._test_show_port_attachment(status=400)
def test_list_port_attachments_error_420(self):
self._test_list_port_attachments(status=420)
def test_show_port_attachment_error_420(self):
self._test_show_port_attachment(status=420)
def test_list_port_attachments_error_430(self):
self._test_list_port_attachments(status=430)
def test_show_port_attachment_error_430(self):
self._test_show_port_attachment(status=430)
def test_attach_resource_json(self):
self._test_attach_resource(format='json')