Revision 92d2d1ce snf-cyclades-app/synnefo/api/util.py
b/snf-cyclades-app/synnefo/api/util.py | ||
---|---|---|
234 | 234 |
raise faults.ItemNotFound("Floating IP does not exist.") |
235 | 235 |
|
236 | 236 |
|
237 |
def allocate_public_address(backend): |
|
237 |
def allocate_public_address(backend, userid):
|
|
238 | 238 |
"""Get a public IP for any available network of a backend.""" |
239 | 239 |
# Guarantee exclusive access to backend, because accessing the IP pools of |
240 | 240 |
# the backend networks may result in a deadlock with backend allocator |
241 | 241 |
# which also checks that backend networks have a free IP. |
242 | 242 |
backend = Backend.objects.select_for_update().get(id=backend.id) |
243 | 243 |
public_networks = backend_public_networks(backend) |
244 |
return get_free_ip(public_networks) |
|
244 |
return get_free_ip(public_networks, userid)
|
|
245 | 245 |
|
246 | 246 |
|
247 | 247 |
def backend_public_networks(backend): |
... | ... | |
255 | 255 |
network__public=True, |
256 | 256 |
network__deleted=False, |
257 | 257 |
network__floating_ip_pool=False, |
258 |
network__subnet__isnull=False, |
|
259 | 258 |
network__drained=False) |
260 | 259 |
return [b.network for b in bnets] |
261 | 260 |
|
262 | 261 |
|
263 |
def get_free_ip(networks): |
|
262 |
def get_free_ip(networks, userid):
|
|
264 | 263 |
for network in networks: |
265 | 264 |
try: |
266 |
address = get_network_free_address(network) |
|
267 |
return network, address |
|
265 |
return network.allocate_address(userid=userid) |
|
268 | 266 |
except faults.OverLimit: |
269 | 267 |
pass |
270 | 268 |
msg = "Can not allocate public IP. Public networks are full." |
... | ... | |
272 | 270 |
raise faults.OverLimit(msg) |
273 | 271 |
|
274 | 272 |
|
275 |
def get_network_free_address(network): |
|
273 |
def get_network_free_address(network, userid):
|
|
276 | 274 |
"""Reserve an IP address from the IP Pool of the network.""" |
277 | 275 |
|
278 |
pool = network.get_pool() |
|
279 | 276 |
try: |
280 |
address = pool.get()
|
|
277 |
return network.allocate_address(userid=userid)
|
|
281 | 278 |
except EmptyPool: |
282 | 279 |
raise faults.OverLimit("Network %s is full." % network.backend_id) |
283 |
pool.save() |
|
284 |
return address |
|
285 | 280 |
|
286 | 281 |
|
287 | 282 |
def get_vm_nic(vm, nic_id): |
Also available in: Unified diff