Revision 3aecadc8 snf-cyclades-app/synnefo/logic/ips.py

b/snf-cyclades-app/synnefo/logic/ips.py
61 61
                                (address, network.id))
62 62

  
63 63

  
64
def allocate_public_ip(userid, floating_ip=False, backend=None):
64
def allocate_public_ip(userid, floating_ip=False, backend=None, networks=None):
65 65
    """Try to allocate a public or floating IP address.
66 66

  
67 67
    Try to allocate a a public IPv4 address from one of the available networks.
......
78 78
        .filter(subnet__network__deleted=False)\
79 79
        .filter(subnet__network__public=True)\
80 80
        .filter(subnet__network__drained=False)
81
    if networks is not None:
82
        ip_pool_rows = ip_pool_rows.filter(subnet__network__in=networks)
81 83
    if floating_ip:
82 84
        ip_pool_rows = ip_pool_rows\
83 85
            .filter(subnet__network__floating_ip_pool=True)
......
99 101
            log_msg += " Backend: %s" % backend
100 102
        log.error(log_msg)
101 103
        exception_msg = "Can not allocate a %s IP address." % ip_type
102
        if floating_ip:
103
            raise faults.Conflict(exception_msg)
104
        else:
105
            raise faults.ServiceUnavailable(exception_msg)
104
        raise faults.Conflict(exception_msg)
106 105

  
107 106

  
108 107
@transaction.commit_on_success
......
131 130
    return floating_ip
132 131

  
133 132

  
133
def get_free_floating_ip(userid, network=None):
134
    """Get one of the free available floating IPs of the user.
135

  
136
    Get one of the users floating IPs that is not connected to any port
137
    or server. If network is specified, the floating IP must be from
138
    that network.
139

  
140
    """
141
    floating_ips = IPAddress.objects\
142
                            .filter(userid=userid, deleted=False, nic=None)
143
    if network is not None:
144
        floating_ips = floating_ips.filter(network=network)
145

  
146
    for floating_ip in floating_ips:
147
        floating_ip = IPAddress.objects.select_for_update()\
148
                                       .get(id=floating_ip.id)
149
        if floating_ip.nic is None:
150
            return floating_ip
151

  
152
    msg = "Cannot allocate a floating IP for connecting new server to"
153
    if network is not None:
154
        msg += " network '%s'." % network.id
155
    else:
156
        msg += " a public network."
157
    msg += " Please create more floating IPs."
158
    raise faults.Conflict(msg)
159

  
160

  
134 161
@transaction.commit_on_success
135 162
def delete_floating_ip(floating_ip):
136 163
    if floating_ip.nic:

Also available in: Unified diff