Commit 0e604e91 authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

cyclades: Change format of 'attachment' attribute

Change the format of the 'attachment' attribute. Until now, each
attachment had an identifier of the form 'nic-$vm_id-$nic_id'. This
commit changes the identifier to just '$nic_id', since from now on
is unique for the lifetime of a NIC.
parent 6684c711
......@@ -95,7 +95,7 @@ def network_to_dict(network, user_id, detail=True):
d['status'] = network.state
d['public'] = network.public
attachments = [util.construct_nic_id(nic)
attachments = [nic.id
for nic in network.nics.filter(machine__userid=user_id)
.filter(state="ACTIVE")
.order_by('machine')]
......
......@@ -105,7 +105,7 @@ def metadata_item_demux(request, server_id, key):
def nic_to_dict(nic):
d = {'id': util.construct_nic_id(nic),
d = {'id': nic.id,
'network_id': str(nic.network.id),
'mac_address': nic.mac,
'ipv4': nic.ipv4,
......@@ -759,7 +759,7 @@ def set_firewall_profile(request, vm, args):
nic_id = args.get("nic")
if nic_id is None:
raise faults.BadRequest("Missing 'nic' attribute")
nic = util.get_nic(vm, nic_id)
nic = util.get_vm_nic(vm, nic_id)
servers.set_firewall_profile(vm, profile=profile, nic=nic)
return HttpResponse(status=202)
......@@ -873,15 +873,14 @@ def remove(request, net, args):
if attachment is None:
raise faults.BadRequest("Missing 'attachment' attribute.")
try:
# attachment string: nic-<vm-id>-<nic-id>
_, server_id, nic_id = attachment.split("-", 2)
server_id = int(server_id)
nic_id = int(nic_id)
nic_id = int(attachment)
except (ValueError, TypeError):
raise faults.BadRequest("Invalid 'attachment' attribute.")
nic = util.get_nic(nic_id=nic_id)
server_id = nic.machine_id
vm = util.get_vm(server_id, request.user_uniq, non_suspended=True)
nic = util.get_nic(vm, nic_id)
servers.disconnect(vm, nic)
return HttpResponse(status=202)
......
......@@ -35,7 +35,7 @@ import json
from mock import patch
from snf_django.utils.testing import BaseAPITest, mocked_quotaholder
from synnefo.db.models import Network, NetworkInterface
from synnefo.db.models import Network
from synnefo.db import models_factory as mfactory
from synnefo.cyclades_settings import cyclades_services
from synnefo.lib.services import get_service_path
......@@ -93,7 +93,7 @@ class NetworkAPITest(ComputeAPITest):
self.assertEqual(db_net.gateway6, api_net['gateway6'])
self.assertEqual(db_net.dhcp, api_net['dhcp'])
self.assertEqual(db_net.public, api_net['public'])
db_nics = ["nic-%d-%d" % (nic.machine.id, nic.id) for nic in
db_nics = [nic.id for nic in
db_net.nics.filter(machine__userid=db_net.userid)]
self.assertEqual(db_nics, api_net['attachments'])
......@@ -137,9 +137,9 @@ class NetworkAPITest(ComputeAPITest):
def test_invalid_data_3(self, mrapi):
"""Test unauthorized to create public network"""
request = {
'network': {'name': 'foo',
"public": "True",
"type": "MAC_FILTERED"}
'network': {"name": 'foo',
"public": "True",
"type": "MAC_FILTERED"}
}
response = self.mypost('networks/', 'user1',
json.dumps(request), 'json')
......@@ -148,7 +148,7 @@ class NetworkAPITest(ComputeAPITest):
def test_invalid_data_4(self, mrapi):
"""Test unauthorized to create network not in settings"""
request = {
'network': {'name': 'foo', 'type': 'CUSTOM'}
'network': {'name': 'foo', 'type': 'CUSTOM'}
}
response = self.mypost('networks/', 'user1',
json.dumps(request), 'json')
......@@ -407,7 +407,7 @@ class NetworkAPITest(ComputeAPITest):
net = mfactory.NetworkFactory(state='ACTIVE', userid=user)
nic = mfactory.NetworkInterfaceFactory(machine=vm, network=net)
mrapi().ModifyInstance.return_value = 1
request = {'remove': {'attachment': 'nic-%s-%s' % (vm.id, nic.id)}}
request = {'remove': {'attachment': "%s" % nic.id}}
response = self.mypost('networks/%d/action' % net.id,
net.userid, json.dumps(request), 'json')
self.assertEqual(response.status_code, 202)
......@@ -420,9 +420,7 @@ class NetworkAPITest(ComputeAPITest):
vm = mfactory.VirtualMachineFactory(name='yo', userid=user)
net = mfactory.NetworkFactory(state='ACTIVE', userid=user)
nic = mfactory.NetworkInterfaceFactory(machine=vm, network=net)
request = {'remove':
{'att234achment': 'nic-%s-%s' % (vm.id, nic.id)}
}
request = {'remove': {'att234achment': '%s' % nic.id}}
response = self.mypost('networks/%d/action' % net.id,
net.userid, json.dumps(request), 'json')
self.assertBadRequest(response)
......@@ -431,9 +429,7 @@ class NetworkAPITest(ComputeAPITest):
user = 'userr'
vm = mfactory.VirtualMachineFactory(name='yo', userid=user)
net = mfactory.NetworkFactory(state='ACTIVE', userid=user)
request = {'remove':
{'attachment': 'nic-%s' % vm.id}
}
request = {'remove': {'attachment': 'nic-%s' % vm.id}}
response = self.mypost('networks/%d/action' % net.id,
net.userid, json.dumps(request), 'json')
self.assertBadRequest(response)
......@@ -442,7 +438,7 @@ class NetworkAPITest(ComputeAPITest):
response = self.myget('nonexistent')
self.assertEqual(response.status_code, 400)
try:
error = json.loads(response.content)
json.loads(response.content)
except ValueError:
self.assertTrue(False)
......
......@@ -144,7 +144,7 @@ class ServerAPITest(ComputeAPITest):
self.assertEqual(api_nic['ipv4'], nic.ipv4)
self.assertEqual(api_nic['ipv6'], nic.ipv6)
self.assertEqual(api_nic['OS-EXT-IPS:type'], "fixed")
self.assertEqual(api_nic['id'], 'nic-%s-%s' % (db_vm.id, nic.id))
self.assertEqual(api_nic['id'], nic.id)
api_address = server["addresses"]
self.assertEqual(api_address[str(net.id)], [
{"version": 4, "addr": nic.ipv4, "OS-EXT-IPS:type": "fixed"},
......
......@@ -284,7 +284,7 @@ def get_network_free_address(network):
return address
def get_nic(vm, nic_id):
def get_vm_nic(vm, nic_id):
"""Get a VMs NIC by its ID."""
try:
return vm.nics.get(id=nic_id)
......@@ -292,6 +292,13 @@ def get_nic(vm, nic_id):
raise faults.ItemNotFound("NIC '%s' not found" % nic_id)
def get_nic(nic_id):
try:
return NetworkInterface.objects.get(id=nic_id)
except NetworkInterface.DoesNotExist:
raise faults.ItemNotFound("NIC '%s' not found" % nic_id)
def render_metadata(request, metadata, use_values=False, status=200):
if request.serialization == 'xml':
data = render_to_string('metadata.xml', {'metadata': metadata})
......@@ -313,10 +320,6 @@ def render_meta(request, meta, status=200):
return HttpResponse(data, status=status)
def construct_nic_id(nic):
return "-".join(["nic", unicode(nic.machine.id), unicode(nic.id)])
def verify_personality(personality):
"""Verify that a a list of personalities is well formed"""
if len(personality) > settings.MAX_PERSONALITY:
......
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