Revision c988fcca

b/snf-cyclades-app/synnefo/api/networks.py
95 95
        d['status'] = network.state
96 96
        d['public'] = network.public
97 97

  
98
        attachments = [util.construct_nic_id(nic)
98
        attachments = [nic.id
99 99
                       for nic in network.nics.filter(machine__userid=user_id)
100 100
                                              .filter(state="ACTIVE")
101 101
                                              .order_by('machine')]
b/snf-cyclades-app/synnefo/api/servers.py
105 105

  
106 106

  
107 107
def nic_to_dict(nic):
108
    d = {'id': util.construct_nic_id(nic),
108
    d = {'id': nic.id,
109 109
         'network_id': str(nic.network.id),
110 110
         'mac_address': nic.mac,
111 111
         'ipv4': nic.ipv4,
......
759 759
    nic_id = args.get("nic")
760 760
    if nic_id is None:
761 761
        raise faults.BadRequest("Missing 'nic' attribute")
762
    nic = util.get_nic(vm, nic_id)
762
    nic = util.get_vm_nic(vm, nic_id)
763 763
    servers.set_firewall_profile(vm, profile=profile, nic=nic)
764 764
    return HttpResponse(status=202)
765 765

  
......
873 873
    if attachment is None:
874 874
        raise faults.BadRequest("Missing 'attachment' attribute.")
875 875
    try:
876
        # attachment string: nic-<vm-id>-<nic-id>
877
        _, server_id, nic_id = attachment.split("-", 2)
878
        server_id = int(server_id)
879
        nic_id = int(nic_id)
876
        nic_id = int(attachment)
880 877
    except (ValueError, TypeError):
881 878
        raise faults.BadRequest("Invalid 'attachment' attribute.")
882 879

  
880
    nic = util.get_nic(nic_id=nic_id)
881
    server_id = nic.machine_id
883 882
    vm = util.get_vm(server_id, request.user_uniq, non_suspended=True)
884
    nic = util.get_nic(vm, nic_id)
883

  
885 884
    servers.disconnect(vm, nic)
886 885

  
887 886
    return HttpResponse(status=202)
b/snf-cyclades-app/synnefo/api/tests/networks.py
35 35
from mock import patch
36 36

  
37 37
from snf_django.utils.testing import BaseAPITest, mocked_quotaholder
38
from synnefo.db.models import Network, NetworkInterface
38
from synnefo.db.models import Network
39 39
from synnefo.db import models_factory as mfactory
40 40
from synnefo.cyclades_settings import cyclades_services
41 41
from synnefo.lib.services import get_service_path
......
93 93
            self.assertEqual(db_net.gateway6, api_net['gateway6'])
94 94
            self.assertEqual(db_net.dhcp, api_net['dhcp'])
95 95
            self.assertEqual(db_net.public, api_net['public'])
96
            db_nics = ["nic-%d-%d" % (nic.machine.id, nic.id) for nic in
96
            db_nics = [nic.id for nic in
97 97
                       db_net.nics.filter(machine__userid=db_net.userid)]
98 98
            self.assertEqual(db_nics, api_net['attachments'])
99 99

  
......
137 137
    def test_invalid_data_3(self, mrapi):
138 138
        """Test unauthorized to create public network"""
139 139
        request = {
140
                'network': {'name': 'foo',
141
                            "public": "True",
142
                            "type": "MAC_FILTERED"}
140
            'network': {"name": 'foo',
141
                        "public": "True",
142
                        "type": "MAC_FILTERED"}
143 143
            }
144 144
        response = self.mypost('networks/', 'user1',
145 145
                               json.dumps(request), 'json')
......
148 148
    def test_invalid_data_4(self, mrapi):
149 149
        """Test unauthorized to create network not in settings"""
150 150
        request = {
151
                'network': {'name': 'foo', 'type': 'CUSTOM'}
151
            'network': {'name': 'foo', 'type': 'CUSTOM'}
152 152
            }
153 153
        response = self.mypost('networks/', 'user1',
154 154
                               json.dumps(request), 'json')
......
407 407
        net = mfactory.NetworkFactory(state='ACTIVE', userid=user)
408 408
        nic = mfactory.NetworkInterfaceFactory(machine=vm, network=net)
409 409
        mrapi().ModifyInstance.return_value = 1
410
        request = {'remove': {'attachment': 'nic-%s-%s' % (vm.id, nic.id)}}
410
        request = {'remove': {'attachment': "%s" % nic.id}}
411 411
        response = self.mypost('networks/%d/action' % net.id,
412 412
                               net.userid, json.dumps(request), 'json')
413 413
        self.assertEqual(response.status_code, 202)
......
420 420
        vm = mfactory.VirtualMachineFactory(name='yo', userid=user)
421 421
        net = mfactory.NetworkFactory(state='ACTIVE', userid=user)
422 422
        nic = mfactory.NetworkInterfaceFactory(machine=vm, network=net)
423
        request = {'remove':
424
                    {'att234achment': 'nic-%s-%s' % (vm.id, nic.id)}
425
                  }
423
        request = {'remove': {'att234achment': '%s' % nic.id}}
426 424
        response = self.mypost('networks/%d/action' % net.id,
427 425
                               net.userid, json.dumps(request), 'json')
428 426
        self.assertBadRequest(response)
......
431 429
        user = 'userr'
432 430
        vm = mfactory.VirtualMachineFactory(name='yo', userid=user)
433 431
        net = mfactory.NetworkFactory(state='ACTIVE', userid=user)
434
        request = {'remove':
435
                    {'attachment': 'nic-%s' % vm.id}
436
                  }
432
        request = {'remove': {'attachment': 'nic-%s' % vm.id}}
437 433
        response = self.mypost('networks/%d/action' % net.id,
438 434
                               net.userid, json.dumps(request), 'json')
439 435
        self.assertBadRequest(response)
......
442 438
        response = self.myget('nonexistent')
443 439
        self.assertEqual(response.status_code, 400)
444 440
        try:
445
            error = json.loads(response.content)
441
            json.loads(response.content)
446 442
        except ValueError:
447 443
            self.assertTrue(False)
448 444

  
b/snf-cyclades-app/synnefo/api/tests/servers.py
144 144
        self.assertEqual(api_nic['ipv4'], nic.ipv4)
145 145
        self.assertEqual(api_nic['ipv6'], nic.ipv6)
146 146
        self.assertEqual(api_nic['OS-EXT-IPS:type'], "fixed")
147
        self.assertEqual(api_nic['id'], 'nic-%s-%s' % (db_vm.id, nic.id))
147
        self.assertEqual(api_nic['id'], nic.id)
148 148
        api_address = server["addresses"]
149 149
        self.assertEqual(api_address[str(net.id)], [
150 150
            {"version": 4, "addr": nic.ipv4, "OS-EXT-IPS:type": "fixed"},
b/snf-cyclades-app/synnefo/api/util.py
284 284
    return address
285 285

  
286 286

  
287
def get_nic(vm, nic_id):
287
def get_vm_nic(vm, nic_id):
288 288
    """Get a VMs NIC by its ID."""
289 289
    try:
290 290
        return vm.nics.get(id=nic_id)
......
292 292
        raise faults.ItemNotFound("NIC '%s' not found" % nic_id)
293 293

  
294 294

  
295
def get_nic(nic_id):
296
    try:
297
        return NetworkInterface.objects.get(id=nic_id)
298
    except NetworkInterface.DoesNotExist:
299
        raise faults.ItemNotFound("NIC '%s' not found" % nic_id)
300

  
301

  
295 302
def render_metadata(request, metadata, use_values=False, status=200):
296 303
    if request.serialization == 'xml':
297 304
        data = render_to_string('metadata.xml', {'metadata': metadata})
......
313 320
    return HttpResponse(data, status=status)
314 321

  
315 322

  
316
def construct_nic_id(nic):
317
    return "-".join(["nic", unicode(nic.machine.id), unicode(nic.id)])
318

  
319

  
320 323
def verify_personality(personality):
321 324
    """Verify that a a list of personalities is well formed"""
322 325
    if len(personality) > settings.MAX_PERSONALITY:

Also available in: Unified diff