FORMAT = "json"
CLI_TEMPLATE = "../quantum/cli_output.template"
-
-### -- Core CLI functions
-
+#TODO(salvatore-orlando): do proper logging!
def prepare_output(cmd, tenant_id, response):
+ """ Fills a cheetah template with the response """
#add command and tenant to response for output generation
response['cmd'] = cmd
response['tenant_id'] = tenant_id
def create_net(manager, *args):
- tid, name = args
- new_net_id = manager.create_network(tid, name)
- print "Created a new Virtual Network with ID:%s\n" % new_net_id
+ tenant_id, name = args
+ new_net_id = manager.create_network(tenant_id, name)['net-id']
+ output = prepare_output("create_net", tenant_id,
+ dict(network_id=new_net_id))
+ print output
def api_create_net(client, *args):
- tid, name = args
- data = {'network': {'net-name': '%s' % name}}
- res = client.create_network(data)
- LOG.debug(res)
- nid = None
+ tenant_id, name = args
+ data = {'network': {'net-name': name}}
+ new_net_id = None
try:
- nid = res["networks"]["network"]["id"]
- except Exception, e:
- print "Failed to create network"
- # TODO(bgh): grab error details from ws request result
+ res = client.create_network(data)
+ new_net_id = res["networks"]["network"]["id"]
+ except Exception as ex:
+ status_code = None
+ message = None
+ #Retrieve dict at 1st element of tuple at last argument
+ if ex.args and isinstance(ex.args[-1][0],dict):
+ status_code = ex.args[-1][0].get('status_code', None)
+ message = ex.args[-1][0].get('message', None)
+ print "Failed to create network: %s" % status_code or '<missing>'
+ print "Error message:%s" % message or '<missing>'
return
- print "Created a new Virtual Network with ID:%s\n" % nid
+ output = prepare_output("create_net", tenant_id,
+ dict(network_id=new_net_id))
+ print output
def delete_net(manager, *args):
- tid, nid = args
- manager.delete_network(tid, nid)
- print "Deleted Virtual Network with ID:%s" % nid
+ tenant_id, network_id = args
+ manager.delete_network(tenant_id, network_id)
+ output = prepare_output("delete_net", tenant_id,
+ dict(network_id=network_id))
+ print output
def api_delete_net(client, *args):
- tid, nid = args
+ tenant_id, network_id = args
try:
- res = client.delete_network(nid)
- print "Deleted Virtual Network with ID:%s" % nid
+ client.delete_network(network_id)
+ output = prepare_output("delete_net", tenant_id,
+ dict(network_id=network_id))
+ print output
except Exception, e:
print "Failed to delete network"
LOG.error("Failed to delete network: %s" % e)
#for $network in $networks
\tNetwork ID: $network.id
#end for
+#elif $cmd == 'create_net'
+Created a new Virtual Network with ID: $network_id for Tenant $tenant_id
+#elif $cmd == 'delete_net'
+Deleted Virtual Network with ID: $network_id for Tenant $tenant_id
#end if
httplib.NO_CONTENT):
return self.deserialize(res)
else:
- raise Exception("Server returned error: %s" % res.read())
+ ex = Exception("Server returned error: %s" % status_code)
+ ex.args = ([dict(status_code=status_code, message=res.read())],)
+ raise ex
except (socket.error, IOError), e:
raise Exception("Unable to connect to "
# Redirect stdout
self.fake_stdout = client_stubs.FakeStdout()
sys.stdout = self.fake_stdout
- # Pre-populate data for testing using db api
- db.network_create(self.tenant_id, self.network_name_1)
- db.network_create(self.tenant_id, self.network_name_2)
def tearDown(self):
"""Clear the test environment"""
# Verify!
# Must add newline at the end to match effect of print call
self.assertEquals(self.fake_stdout.make_string(), output + '\n')
+
+ def _verify_create_network(self):
+ # Verification - get raw result from db
+ nw_list = db.network_list(self.tenant_id)
+ if len(nw_list) != 1:
+ self.fail("No network created")
+ network_id = nw_list[0].uuid
+ # Fill CLI template
+ output = cli.prepare_output('create_net', self.tenant_id,
+ dict(network_id=network_id))
+ # Verify!
+ # Must add newline at the end to match effect of print call
+ self.assertEquals(self.fake_stdout.make_string(), output + '\n')
def test_list_networks(self):
try:
+ # Pre-populate data for testing using db api
+ db.network_create(self.tenant_id, self.network_name_1)
+ db.network_create(self.tenant_id, self.network_name_2)
+
cli.list_nets(self.manager, self.tenant_id)
LOG.debug("Operation completed. Verifying result")
LOG.debug(self.fake_stdout.content)
self._verify_list_networks()
except:
LOG.exception("Exception caught: %s", sys.exc_info())
- self.fail("test_list_network_api failed due to an exception")
+ self.fail("test_list_network failed due to an exception")
def test_list_networks_api(self):
try:
+ # Pre-populate data for testing using db api
+ db.network_create(self.tenant_id, self.network_name_1)
+ db.network_create(self.tenant_id, self.network_name_2)
+
cli.api_list_nets(self.client, self.tenant_id)
LOG.debug("Operation completed. Verifying result")
LOG.debug(self.fake_stdout.content)
LOG.exception("Exception caught: %s", sys.exc_info())
self.fail("test_list_network_api failed due to an exception")
+ def test_create_network(self):
+ try:
+ cli.create_net(self.manager, self.tenant_id, "test")
+ LOG.debug("Operation completed. Verifying result")
+ LOG.debug(self.fake_stdout.content)
+ self._verify_create_network()
+ except:
+ LOG.exception("Exception caught: %s", sys.exc_info())
+ self.fail("test_create_network failed due to an exception")
+
+
+ def test_create_network_api(self):
+ try:
+ cli.api_create_net(self.client, self.tenant_id, "test")
+ LOG.debug("Operation completed. Verifying result")
+ LOG.debug(self.fake_stdout.content)
+ self._verify_create_network()
+ except:
+ LOG.exception("Exception caught: %s", sys.exc_info())
+ self.fail("test_create_network_api failed due to an exception")