Revision 6b8dc47c

b/snf-cyclades-app/synnefo/logic/ports.py
32 32
# or implied, of GRNET S.A.
33 33
from functools import wraps
34 34
from django.db import transaction
35

  
36
#from django.conf import settings
37
from synnefo.api import util
38
from snf_django.lib.api import faults
39
from synnefo.db.models import NetworkInterface
40
from synnefo.logic import backend
35
from synnefo.logic import servers
41 36

  
42 37
from logging import getLogger
43 38
log = getLogger(__name__)
......
54 49

  
55 50

  
56 51
@transaction.commit_on_success
57
def create(network, machine, ipaddress, name="", security_groups=None,
52
def create(network, machine, ipaddress=None, name="", security_groups=None,
58 53
           device_owner='vm'):
59 54
    """Create a new port connecting a server/router to a network.
60 55

  
......
64 59
    allocated.
65 60

  
66 61
    """
67
    if network.state != 'ACTIVE':
68
        raise faults.Conflict('Network build in process')
69

  
70
    user_id = machine.userid
71

  
72
    if ipaddress is None:
73
        if network.subnets.filter(ipversion=4).exists():
74
            ipaddress = util.allocate_ip(network, user_id)
75
    else:
76
        if ipaddress.nic is not None:
77
            raise faults.BadRequest("Address '%s' already in use." %
78
                                    ipaddress.address)
79
    #create the port
80
    port = NetworkInterface.objects.create(name=name,
81
                                           network=network,
82
                                           machine=machine,
83
                                           userid=user_id,
84
                                           device_owner=device_owner,
85
                                           state="BUILD")
86
    #add the security groups if any
62
    port, ipaddress = servers.create_nic(machine, network, ipaddress=ipaddress,
63
                                         name=name)
64

  
65
    # add the security groups if any
87 66
    if security_groups:
88 67
        port.security_groups.add(*security_groups)
89 68

  
90
    # If no IPAddress is specified, try to allocate one
91
    if ipaddress is None and network.subnets.filter(ipversion=4).exists():
92
        ipaddress = util.allocate_ip(network, user_id)
93

  
94
    # Associate the IPAddress with the NIC
95
    if ipaddress is not None:
96
        ipaddress.nic = port
97
        ipaddress.save()
98

  
99
    jobID = backend.connect_to_network(machine, port)
69
    machine = servers.connect(machine, network, port)
70
    jobID = machine.task_job_id
100 71

  
101 72
    log.info("Created Port %s with IP Address: %s. Job: %s",
102 73
             port, ipaddress, jobID)
......
109 80

  
110 81
@transaction.commit_on_success
111 82
def delete(port):
112
    """Delete a port by removing the NIC card the instance.
83
    """Delete a port by removing the NIC card from the instance.
113 84

  
114 85
    Send a Job to remove the NIC card from the instance. The port
115 86
    will be deleted and the associated IPv4 addressess will be released
......
117 88

  
118 89
    """
119 90

  
120
    jobID = backend.disconnect_from_network(port.machine, port)
121
    log.info("Removing port %s, Job: %s", port, jobID)
91
    vm = servers.disconnect(port.machine, port)
92
    log.info("Removing port %s, Job: %s", port, vm.task_job_id)
93

  
122 94
    return port
b/snf-cyclades-app/synnefo/logic/servers.py
276 276
    return nics
277 277

  
278 278

  
279
def create_nic(vm, network=None, ipaddress=None, address=None):
279
def create_nic(vm, network=None, ipaddress=None, address=None, name=None):
280 280
    """Helper functions for create NIC objects.
281 281

  
282 282
    Create a NetworkInterface connecting a VirtualMachine to a network with the
......
292 292
                ipaddress = util.allocate_ip(network, userid=userid,
293 293
                                             address=address)
294 294
            except pools.ValueNotAvailable:
295
                raise faults.badRequest("Address '%s' is not available." %
296
                                        address)
295
                raise faults.Conflict("Address '%s' is not available." %
296
                                      address)
297 297

  
298 298
    if ipaddress is not None and ipaddress.nic is not None:
299 299
        raise faults.Conflict("IP address '%s' already in use" %
......
309 309
    device_owner = "vm"
310 310
    nic = NetworkInterface.objects.create(machine=vm, network=network,
311 311
                                          state="BUILD",
312
                                          device_owner=device_owner)
312
                                          userid=vm.userid,
313
                                          device_owner=device_owner,
314
                                          name=name)
313 315
    if ipaddress is not None:
314 316
        ipaddress.nic = nic
315 317
        ipaddress.save()
......
381 383

  
382 384

  
383 385
@server_command("CONNECT")
384
def connect(vm, network):
385
    nic, ipaddress = create_nic(vm, network)
386
def connect(vm, network, port=None):
387
    if port is None:
388
        nic, ipaddress = create_nic(vm, network)
389
    else:
390
        nic = port
391
        ipaddress = port.ips.all()[0]
386 392

  
387 393
    log.info("Creating NIC %s with IPAddress %s", nic, ipaddress)
388 394

  

Also available in: Unified diff