root / snf-cyclades-app / synnefo / api / floating_ips.py @ d610698e
History | View | Annotate | Download (9.6 kB)
1 |
# Copyright 2013 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from django.conf.urls.defaults import patterns |
35 |
from django.db import transaction |
36 |
from django.http import HttpResponse |
37 |
from django.utils import simplejson as json |
38 |
|
39 |
from snf_django.lib import api |
40 |
from snf_django.lib.api import faults, utils |
41 |
from synnefo.api import util |
42 |
from synnefo import quotas |
43 |
from synnefo.db.models import Network, IPAddress |
44 |
|
45 |
from logging import getLogger |
46 |
log = getLogger(__name__) |
47 |
|
48 |
'''
|
49 |
ips_urlpatterns = patterns(
|
50 |
'synnefo.api.floating_ips',
|
51 |
(r'^(?:/|.json|.xml)?$', 'demux'),
|
52 |
(r'^/(\w+)(?:.json|.xml)?$', 'floating_ip_demux'),
|
53 |
)
|
54 |
|
55 |
pools_urlpatterns = patterns(
|
56 |
"synnefo.api.floating_ips",
|
57 |
(r'^(?:/|.json|.xml)?$', 'list_floating_ip_pools'),
|
58 |
)
|
59 |
'''
|
60 |
|
61 |
ips_urlpatterns = patterns( |
62 |
'synnefo.api.floating_ips',
|
63 |
(r'^(?:/|.json|.xml)?$', 'demux'), |
64 |
(r'^/detail(?:.json|.xml)?$', 'list_floating_ips', {'detail': True}), |
65 |
(r'^/(\w+)(?:/|.json|.xml)?$', 'floating_ip_demux')) |
66 |
|
67 |
|
68 |
def demux(request): |
69 |
if request.method == 'GET': |
70 |
return list_floating_ips(request)
|
71 |
elif request.method == 'POST': |
72 |
return allocate_floating_ip(request)
|
73 |
else:
|
74 |
return api.api_method_not_allowed(request)
|
75 |
|
76 |
|
77 |
def floating_ip_demux(request, floating_ip_id): |
78 |
if request.method == 'GET': |
79 |
return get_floating_ip(request, floating_ip_id)
|
80 |
elif request.method == 'DELETE': |
81 |
return release_floating_ip(request, floating_ip_id)
|
82 |
elif request.method == 'PUT': |
83 |
return update_floating_ip(request, floating_ip_id)
|
84 |
else:
|
85 |
return api.api_method_not_allowed(request)
|
86 |
|
87 |
|
88 |
def ip_to_dict(floating_ip): |
89 |
machine_id = None
|
90 |
port_id = None
|
91 |
if floating_ip.nic is not None: |
92 |
machine_id = floating_ip.nic.machine_id |
93 |
port_id = floating_ip.nic.id |
94 |
return {"fixed_ip_address": None, |
95 |
"id": str(floating_ip.id), |
96 |
"instance_id": str(machine_id) if machine_id else None, |
97 |
"floating_ip_address": floating_ip.address,
|
98 |
"port_id": str(floating_ip.nic.id) if port_id else None, |
99 |
"floating_network_id": str(floating_ip.network_id)} |
100 |
|
101 |
|
102 |
@api.api_method(http_method="GET", user_required=True, logger=log, |
103 |
serializations=["json"])
|
104 |
def list_floating_ips(request): |
105 |
"""Return user reserved floating IPs"""
|
106 |
log.debug("list_floating_ips")
|
107 |
|
108 |
userid = request.user_uniq |
109 |
floating_ips = IPAddress.objects.filter(userid=userid, deleted=False,
|
110 |
floating_ip=True).order_by("id")\ |
111 |
.select_related("nic")
|
112 |
floating_ips = utils.filter_modified_since(request, objects=floating_ips) |
113 |
|
114 |
floating_ips = map(ip_to_dict, floating_ips)
|
115 |
|
116 |
request.serialization = "json"
|
117 |
data = json.dumps({"floating_ips": floating_ips})
|
118 |
|
119 |
return HttpResponse(data, status=200) |
120 |
|
121 |
|
122 |
@api.api_method(http_method="GET", user_required=True, logger=log, |
123 |
serializations=["json"])
|
124 |
def get_floating_ip(request, floating_ip_id): |
125 |
"""Return information for a floating IP."""
|
126 |
userid = request.user_uniq |
127 |
floating_ip = util.get_floating_ip_by_id(userid, floating_ip_id) |
128 |
request.serialization = "json"
|
129 |
data = json.dumps({"floating_ip": ip_to_dict(floating_ip)})
|
130 |
return HttpResponse(data, status=200) |
131 |
|
132 |
|
133 |
@api.api_method(http_method='POST', user_required=True, logger=log, |
134 |
serializations=["json"])
|
135 |
@transaction.commit_on_success
|
136 |
def allocate_floating_ip(request): |
137 |
"""Allocate a floating IP."""
|
138 |
req = utils.get_request_dict(request) |
139 |
floating_ip_dict = api.utils.get_attribute(req, "floatingip",
|
140 |
required=True)
|
141 |
log.info('allocate_floating_ip %s', req)
|
142 |
|
143 |
userid = request.user_uniq |
144 |
|
145 |
# the network_pool is a mandatory field
|
146 |
network_pool = api.utils.get_attribute(floating_ip_dict, |
147 |
"floating_network_id",
|
148 |
required=True)
|
149 |
address = api.utils.get_attribute(floating_ip_dict, "floating_ip_address",
|
150 |
required=False)
|
151 |
|
152 |
try:
|
153 |
network_id = int(network_pool)
|
154 |
except ValueError: |
155 |
raise faults.BadRequest("Invalid networkd ID.") |
156 |
network = util.get_network(network_id, userid, for_update=True,
|
157 |
non_deleted=True)
|
158 |
|
159 |
if not network.floating_ip_pool: |
160 |
# Check that it is a floating IP pool
|
161 |
raise faults.ItemNotFound("Floating IP pool %s does not exist." % |
162 |
network_id) |
163 |
|
164 |
# Allocate the floating IP
|
165 |
floating_ip = util.allocate_ip(network, userid, address=address, |
166 |
floating_ip=True)
|
167 |
# Issue commission (quotas)
|
168 |
quotas.issue_and_accept_commission(floating_ip) |
169 |
transaction.commit() |
170 |
|
171 |
log.info("User '%s' allocated floating IP '%s'", userid, floating_ip)
|
172 |
|
173 |
request.serialization = "json"
|
174 |
data = json.dumps({"floating_ip": ip_to_dict(floating_ip)})
|
175 |
return HttpResponse(data, status=200) |
176 |
|
177 |
|
178 |
@api.api_method(http_method='DELETE', user_required=True, logger=log, |
179 |
serializations=["json"])
|
180 |
@transaction.commit_on_success
|
181 |
def release_floating_ip(request, floating_ip_id): |
182 |
"""Release a floating IP."""
|
183 |
userid = request.user_uniq |
184 |
log.info("release_floating_ip '%s'. User '%s'.", floating_ip_id, userid)
|
185 |
|
186 |
floating_ip = util.get_floating_ip_by_id(userid, floating_ip_id, |
187 |
for_update=True)
|
188 |
if floating_ip.nic:
|
189 |
# This is safe, you also need for_update to attach floating IP to
|
190 |
# instance.
|
191 |
msg = "Floating IP '%s' is attached to instance." % floating_ip.id
|
192 |
raise faults.Conflict(msg)
|
193 |
|
194 |
# Return the address of the floating IP back to pool
|
195 |
floating_ip.release_address() |
196 |
# And mark the floating IP as deleted
|
197 |
floating_ip.deleted = True
|
198 |
floating_ip.save() |
199 |
# Release quota for floating IP
|
200 |
quotas.issue_and_accept_commission(floating_ip, delete=True)
|
201 |
transaction.commit() |
202 |
# Delete the floating IP from DB
|
203 |
floating_ip.delete() |
204 |
|
205 |
log.info("User '%s' released IP '%s", userid, floating_ip)
|
206 |
|
207 |
return HttpResponse(status=204) |
208 |
|
209 |
|
210 |
@api.api_method(http_method='PUT', user_required=True, logger=log, |
211 |
serializations=["json"])
|
212 |
@transaction.commit_on_success
|
213 |
def update_floating_ip(request, floating_ip_id): |
214 |
"""Update a floating IP."""
|
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)
|
240 |
|
241 |
|
242 |
# Floating IP pools
|
243 |
@api.api_method(http_method='GET', user_required=True, logger=log, |
244 |
serializations=["json"])
|
245 |
def list_floating_ip_pools(request): |
246 |
networks = Network.objects.filter(public=True, floating_ip_pool=True, |
247 |
deleted=False)
|
248 |
networks = utils.filter_modified_since(request, objects=networks) |
249 |
floating_ip_pools = map(network_to_floating_ip_pool, networks)
|
250 |
request.serialization = "json"
|
251 |
data = json.dumps({"floating_ip_pools": floating_ip_pools})
|
252 |
request.serialization = "json"
|
253 |
return HttpResponse(data, status=200) |
254 |
|
255 |
|
256 |
def network_to_floating_ip_pool(network): |
257 |
"""Convert a 'Network' object to a floating IP pool dict."""
|
258 |
total, free = network.ip_count() |
259 |
return {"name": str(network.id), |
260 |
"size": total,
|
261 |
"free": free}
|