Revision 79f4eec0 snf-cyclades-app/synnefo/logic/ports.py

b/snf-cyclades-app/synnefo/logic/ports.py
54 54

  
55 55

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

  
61
    Connect a server/router to a network by creating a new Port. If
62
    'ipaddress' argument is specified, the port will be assigned this
63
    IPAddress. Otherwise, an IPv4 address from the IPv4 subnet will be
64
    allocated.
65

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

  
63 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)
64 79
    #create the port
65 80
    port = NetworkInterface.objects.create(name=name,
66 81
                                           network=network,
......
72 87
    if security_groups:
73 88
        port.security_groups.add(*security_groups)
74 89

  
75
    ipaddress = None
76
    if network.subnets.filter(ipversion=4).exists():
77
            ipaddress = util.allocate_ip(network, user_id)
78
            ipaddress.nic = port
79
            ipaddress.save()
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()
80 98

  
81 99
    jobID = backend.connect_to_network(machine, port)
82 100

  

Also available in: Unified diff