Revision 6b8dc47c snf-cyclades-app/synnefo/logic/ports.py

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

Also available in: Unified diff