Revision d610698e snf-cyclades-app/synnefo/api/floating_ips.py
b/snf-cyclades-app/synnefo/api/floating_ips.py | ||
---|---|---|
41 | 41 |
from synnefo.api import util |
42 | 42 |
from synnefo import quotas |
43 | 43 |
from synnefo.db.models import Network, IPAddress |
44 |
from synnefo.db import pools |
|
45 |
from synnefo.logic import servers, backend |
|
46 | 44 |
|
47 | 45 |
from logging import getLogger |
48 | 46 |
log = getLogger(__name__) |
... | ... | |
148 | 146 |
network_pool = api.utils.get_attribute(floating_ip_dict, |
149 | 147 |
"floating_network_id", |
150 | 148 |
required=True) |
151 |
device_id = api.utils.get_attribute(floating_ip_dict, "device_id", |
|
152 |
required=False) |
|
153 | 149 |
address = api.utils.get_attribute(floating_ip_dict, "floating_ip_address", |
154 | 150 |
required=False) |
155 | 151 |
|
156 |
if device_id: |
|
157 |
vm = util.get_vm(device_id, userid, non_deleted=False) |
|
158 |
|
|
159 | 152 |
try: |
160 | 153 |
network_id = int(network_pool) |
161 | 154 |
except ValueError: |
162 | 155 |
raise faults.BadRequest("Invalid networkd ID.") |
163 | 156 |
network = util.get_network(network_id, userid, for_update=True, |
164 | 157 |
non_deleted=True) |
158 |
|
|
165 | 159 |
if not network.floating_ip_pool: |
166 | 160 |
# Check that it is a floating IP pool |
167 | 161 |
raise faults.ItemNotFound("Floating IP pool %s does not exist." % |
168 | 162 |
network_id) |
163 |
|
|
164 |
# Allocate the floating IP |
|
169 | 165 |
floating_ip = util.allocate_ip(network, userid, address=address, |
170 | 166 |
floating_ip=True) |
171 |
|
|
167 |
# Issue commission (quotas) |
|
172 | 168 |
quotas.issue_and_accept_commission(floating_ip) |
173 | 169 |
transaction.commit() |
174 | 170 |
|
175 | 171 |
log.info("User '%s' allocated floating IP '%s'", userid, floating_ip) |
176 | 172 |
|
177 |
# connect to the given vm if any |
|
178 |
if device_id: |
|
179 |
nic, ipaddress = servers.create_nic(vm, ipaddress=floating_ip) |
|
180 |
servers.backend.connect_to_network(vm, nic) |
|
181 |
|
|
182 | 173 |
request.serialization = "json" |
183 | 174 |
data = json.dumps({"floating_ip": ip_to_dict(floating_ip)}) |
184 | 175 |
return HttpResponse(data, status=200) |
... | ... | |
221 | 212 |
@transaction.commit_on_success |
222 | 213 |
def update_floating_ip(request, floating_ip_id): |
223 | 214 |
"""Update a floating IP.""" |
224 |
userid = request.user_uniq |
|
225 |
log.info("update_floating_ip '%s'. User '%s'.", floating_ip_id, userid) |
|
226 |
|
|
227 |
req = utils.get_request_dict(request) |
|
228 |
info = api.utils.get_attribute(req, "floatingip", required=True) |
|
229 |
|
|
230 |
device_id = api.utils.get_attribute(info, "device_id", required=False) |
|
231 |
|
|
232 |
floating_ip = util.get_floating_ip_by_id(userid, floating_ip_id, |
|
233 |
for_update=True) |
|
234 |
if device_id: |
|
235 |
# attach |
|
236 |
vm = util.get_vm(device_id, userid) |
|
237 |
nic, floating_ip = servers.create_nic(vm, ipaddress=floating_ip) |
|
238 |
backend.connect_to_network(vm, nic) |
|
239 |
else: |
|
240 |
# dettach |
|
241 |
nic = floating_ip.nic |
|
242 |
if not nic: |
|
243 |
raise faults.BadRequest("The floating IP is not associated\ |
|
244 |
with any device") |
|
245 |
vm = nic.machine |
|
246 |
servers.disconnect(vm, nic) |
|
247 |
return HttpResponse(status=202) |
|
215 |
raise faults.NotImplented("Updating a floating IP is not supported.") |
|
216 |
#userid = request.user_uniq |
|
217 |
#log.info("update_floating_ip '%s'. User '%s'.", floating_ip_id, userid) |
|
218 |
|
|
219 |
#req = utils.get_request_dict(request) |
|
220 |
#info = api.utils.get_attribute(req, "floatingip", required=True) |
|
221 |
|
|
222 |
#device_id = api.utils.get_attribute(info, "device_id", required=False) |
|
223 |
|
|
224 |
#floating_ip = util.get_floating_ip_by_id(userid, floating_ip_id, |
|
225 |
# for_update=True) |
|
226 |
#if device_id: |
|
227 |
# # attach |
|
228 |
# vm = util.get_vm(device_id, userid) |
|
229 |
# nic, floating_ip = servers.create_nic(vm, ipaddress=floating_ip) |
|
230 |
# backend.connect_to_network(vm, nic) |
|
231 |
#else: |
|
232 |
# # dettach |
|
233 |
# nic = floating_ip.nic |
|
234 |
# if not nic: |
|
235 |
# raise faults.BadRequest("The floating IP is not associated\ |
|
236 |
# with any device") |
|
237 |
# vm = nic.machine |
|
238 |
# servers.disconnect(vm, nic) |
|
239 |
#return HttpResponse(status=202) |
|
248 | 240 |
|
249 | 241 |
|
250 | 242 |
# Floating IP pools |
... | ... | |
254 | 246 |
networks = Network.objects.filter(public=True, floating_ip_pool=True, |
255 | 247 |
deleted=False) |
256 | 248 |
networks = utils.filter_modified_since(request, objects=networks) |
257 |
pools = map(network_to_floating_ip_pool, networks) |
|
249 |
floating_ip_pools = map(network_to_floating_ip_pool, networks)
|
|
258 | 250 |
request.serialization = "json" |
259 |
data = json.dumps({"floating_ip_pools": pools}) |
|
251 |
data = json.dumps({"floating_ip_pools": floating_ip_pools})
|
|
260 | 252 |
request.serialization = "json" |
261 | 253 |
return HttpResponse(data, status=200) |
262 | 254 |
|
Also available in: Unified diff