Commit 6253554d authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

cyclades: Add 'addresses' to servers API response

parent b2e91b8c
......@@ -124,6 +124,17 @@ def nic_to_dict(nic):
return d
def nics_to_addresses(nics):
addresses = {}
for nic in nics:
net_nics = []
net_nics.append({"version": 4, "addr": nic.ipv4})
if nic.ipv6:
net_nics.append({"version": 6, "addr": nic.ipv6})
addresses[nic.network.id] = net_nics
return addresses
def vm_to_dict(vm, detail=False):
d = dict(id=vm.id, name=vm.name)
d['links'] = util.vm_to_links(vm.id)
......@@ -148,6 +159,7 @@ def vm_to_dict(vm, detail=False):
vm_nics = vm.nics.filter(state="ACTIVE").order_by("index")
attachments = map(nic_to_dict, vm_nics)
d['attachments'] = attachments
d['addresses'] = nics_to_addresses(vm_nics)
# include the latest vm diagnostic, if set
diagnostic = vm.get_last_diagnostic()
......@@ -299,7 +311,7 @@ def create_server(request):
@transaction.commit_manually
def do_create_server(userid, name, password, flavor, image, metadata={},
personality=[], network=None, backend=None):
personality=[], network=None, backend=None):
if backend is None:
# Allocate backend to host the server. Commit after allocation to
# release the locks hold by the backend allocator.
......@@ -544,12 +556,13 @@ def list_addresses(request, server_id):
log.debug('list_addresses %s', server_id)
vm = util.get_vm(server_id, request.user_uniq)
addresses = [nic_to_dict(nic) for nic in vm.nics.all()]
attachments = [nic_to_dict(nic) for nic in vm.nics.all()]
addresses = nics_to_addresses(vm.nics.all())
if request.serialization == 'xml':
data = render_to_string('list_addresses.xml', {'addresses': addresses})
else:
data = json.dumps({'addresses': addresses})
data = json.dumps({'addresses': addresses, 'attachments': attachments})
return HttpResponse(data, status=200)
......@@ -567,13 +580,13 @@ def list_addresses_by_network(request, server_id, network_id):
log.debug('list_addresses_by_network %s %s', server_id, network_id)
machine = util.get_vm(server_id, request.user_uniq)
network = util.get_network(network_id, request.user_uniq)
nic = util.get_nic(machine, network)
address = nic_to_dict(nic)
nics = machine.nics.filter(network=network).all()
addresses = nics_to_addresses(nics)
if request.serialization == 'xml':
data = render_to_string('address.xml', {'address': address})
data = render_to_string('address.xml', {'addresses': addresses})
else:
data = json.dumps({'network': address})
data = json.dumps({'network': addresses})
return HttpResponse(data, status=200)
......
......@@ -118,7 +118,8 @@ class ServerAPITest(ComputeAPITest):
db_vm = self.vm2
user = self.vm2.userid
net = mfactory.NetworkFactory()
nic = mfactory.NetworkInterfaceFactory(machine=self.vm2, network=net)
nic = mfactory.NetworkInterfaceFactory(machine=self.vm2, network=net,
ipv6="::babe")
db_vm_meta = mfactory.VirtualMachineMetadataFactory(vm=db_vm)
......@@ -138,6 +139,10 @@ class ServerAPITest(ComputeAPITest):
self.assertEqual(api_nic['ipv4'], nic.ipv4)
self.assertEqual(api_nic['ipv6'], nic.ipv6)
self.assertEqual(api_nic['id'], 'nic-%s-%s' % (db_vm.id, nic.index))
api_address = server["addresses"]
self.assertEqual(api_address[str(net.id)],
[{"version": 4, "addr": nic.ipv4},
{"version": 6, "addr": nic.ipv6}])
metadata = server['metadata']
self.assertEqual(len(metadata), 1)
......
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