Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / api / floating_ips.py @ 24ea227b

History | View | Annotate | Download (8.8 kB)

1 59183afc Christos Stavrakakis
# Copyright 2013 GRNET S.A. All rights reserved.
2 59183afc Christos Stavrakakis
#
3 59183afc Christos Stavrakakis
# Redistribution and use in source and binary forms, with or
4 59183afc Christos Stavrakakis
# without modification, are permitted provided that the following
5 59183afc Christos Stavrakakis
# conditions are met:
6 59183afc Christos Stavrakakis
#
7 59183afc Christos Stavrakakis
#   1. Redistributions of source code must retain the above
8 59183afc Christos Stavrakakis
#      copyright notice, this list of conditions and the following
9 59183afc Christos Stavrakakis
#      disclaimer.
10 59183afc Christos Stavrakakis
#
11 59183afc Christos Stavrakakis
#   2. Redistributions in binary form must reproduce the above
12 59183afc Christos Stavrakakis
#      copyright notice, this list of conditions and the following
13 59183afc Christos Stavrakakis
#      disclaimer in the documentation and/or other materials
14 59183afc Christos Stavrakakis
#      provided with the distribution.
15 59183afc Christos Stavrakakis
#
16 59183afc Christos Stavrakakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 59183afc Christos Stavrakakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 59183afc Christos Stavrakakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 59183afc Christos Stavrakakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 59183afc Christos Stavrakakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 59183afc Christos Stavrakakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 59183afc Christos Stavrakakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 59183afc Christos Stavrakakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 59183afc Christos Stavrakakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 59183afc Christos Stavrakakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 59183afc Christos Stavrakakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 59183afc Christos Stavrakakis
# POSSIBILITY OF SUCH DAMAGE.
28 59183afc Christos Stavrakakis
#
29 59183afc Christos Stavrakakis
# The views and conclusions contained in the software and
30 59183afc Christos Stavrakakis
# documentation are those of the authors and should not be
31 59183afc Christos Stavrakakis
# interpreted as representing official policies, either expressed
32 59183afc Christos Stavrakakis
# or implied, of GRNET S.A.
33 59183afc Christos Stavrakakis
34 59183afc Christos Stavrakakis
from django.conf.urls.defaults import patterns
35 59183afc Christos Stavrakakis
from django.db import transaction
36 59183afc Christos Stavrakakis
from django.http import HttpResponse
37 59183afc Christos Stavrakakis
from django.utils import simplejson as json
38 59183afc Christos Stavrakakis
39 59183afc Christos Stavrakakis
from snf_django.lib import api
40 59183afc Christos Stavrakakis
from snf_django.lib.api import faults, utils
41 59183afc Christos Stavrakakis
from synnefo.api import util
42 0292883e Christos Stavrakakis
from synnefo.logic import ips
43 fde2c1f7 Christos Stavrakakis
from synnefo.db.models import Network, IPAddress
44 59183afc Christos Stavrakakis
45 59183afc Christos Stavrakakis
from logging import getLogger
46 59183afc Christos Stavrakakis
log = getLogger(__name__)
47 59183afc Christos Stavrakakis
48 e4f484da Marios Kogias
'''
49 7d368e73 Christos Stavrakakis
ips_urlpatterns = patterns(
50 59183afc Christos Stavrakakis
    'synnefo.api.floating_ips',
51 59183afc Christos Stavrakakis
    (r'^(?:/|.json|.xml)?$', 'demux'),
52 59183afc Christos Stavrakakis
    (r'^/(\w+)(?:.json|.xml)?$', 'floating_ip_demux'),
53 59183afc Christos Stavrakakis
)
54 59183afc Christos Stavrakakis

55 7d368e73 Christos Stavrakakis
pools_urlpatterns = patterns(
56 7d368e73 Christos Stavrakakis
    "synnefo.api.floating_ips",
57 7d368e73 Christos Stavrakakis
    (r'^(?:/|.json|.xml)?$', 'list_floating_ip_pools'),
58 7d368e73 Christos Stavrakakis
)
59 e4f484da Marios Kogias
'''
60 e4f484da Marios Kogias
61 e4f484da Marios Kogias
ips_urlpatterns = patterns(
62 e4f484da Marios Kogias
    'synnefo.api.floating_ips',
63 e4f484da Marios Kogias
    (r'^(?:/|.json|.xml)?$', 'demux'),
64 e4f484da Marios Kogias
    (r'^/detail(?:.json|.xml)?$', 'list_floating_ips', {'detail': True}),
65 e4f484da Marios Kogias
    (r'^/(\w+)(?:/|.json|.xml)?$', 'floating_ip_demux'))
66 7d368e73 Christos Stavrakakis
67 59183afc Christos Stavrakakis
68 59183afc Christos Stavrakakis
def demux(request):
69 59183afc Christos Stavrakakis
    if request.method == 'GET':
70 59183afc Christos Stavrakakis
        return list_floating_ips(request)
71 59183afc Christos Stavrakakis
    elif request.method == 'POST':
72 59183afc Christos Stavrakakis
        return allocate_floating_ip(request)
73 59183afc Christos Stavrakakis
    else:
74 20a626e7 Giorgos Korfiatis
        return api.api_method_not_allowed(request)
75 59183afc Christos Stavrakakis
76 59183afc Christos Stavrakakis
77 59183afc Christos Stavrakakis
def floating_ip_demux(request, floating_ip_id):
78 59183afc Christos Stavrakakis
    if request.method == 'GET':
79 59183afc Christos Stavrakakis
        return get_floating_ip(request, floating_ip_id)
80 59183afc Christos Stavrakakis
    elif request.method == 'DELETE':
81 59183afc Christos Stavrakakis
        return release_floating_ip(request, floating_ip_id)
82 e4f484da Marios Kogias
    elif request.method == 'PUT':
83 e4f484da Marios Kogias
        return update_floating_ip(request, floating_ip_id)
84 59183afc Christos Stavrakakis
    else:
85 20a626e7 Giorgos Korfiatis
        return api.api_method_not_allowed(request)
86 59183afc Christos Stavrakakis
87 59183afc Christos Stavrakakis
88 59183afc Christos Stavrakakis
def ip_to_dict(floating_ip):
89 fde2c1f7 Christos Stavrakakis
    machine_id = None
90 e4f484da Marios Kogias
    port_id = None
91 fde2c1f7 Christos Stavrakakis
    if floating_ip.nic is not None:
92 fde2c1f7 Christos Stavrakakis
        machine_id = floating_ip.nic.machine_id
93 e4f484da Marios Kogias
        port_id = floating_ip.nic.id
94 e4f484da Marios Kogias
    return {"fixed_ip_address": None,
95 59183afc Christos Stavrakakis
            "id": str(floating_ip.id),
96 59183afc Christos Stavrakakis
            "instance_id": str(machine_id) if machine_id else None,
97 e4f484da Marios Kogias
            "floating_ip_address": floating_ip.address,
98 e4f484da Marios Kogias
            "port_id": str(floating_ip.nic.id) if port_id else None,
99 e4f484da Marios Kogias
            "floating_network_id": str(floating_ip.network_id)}
100 59183afc Christos Stavrakakis
101 59183afc Christos Stavrakakis
102 1cbe2522 Christos Stavrakakis
@api.api_method(http_method="GET", user_required=True, logger=log,
103 1cbe2522 Christos Stavrakakis
                serializations=["json"])
104 59183afc Christos Stavrakakis
def list_floating_ips(request):
105 59183afc Christos Stavrakakis
    """Return user reserved floating IPs"""
106 59183afc Christos Stavrakakis
    log.debug("list_floating_ips")
107 59183afc Christos Stavrakakis
108 59183afc Christos Stavrakakis
    userid = request.user_uniq
109 fde2c1f7 Christos Stavrakakis
    floating_ips = IPAddress.objects.filter(userid=userid, deleted=False,
110 fde2c1f7 Christos Stavrakakis
                                            floating_ip=True).order_by("id")\
111 fde2c1f7 Christos Stavrakakis
                                    .select_related("nic")
112 b6bc4afa Christos Stavrakakis
    floating_ips = utils.filter_modified_since(request, objects=floating_ips)
113 59183afc Christos Stavrakakis
114 59183afc Christos Stavrakakis
    floating_ips = map(ip_to_dict, floating_ips)
115 59183afc Christos Stavrakakis
116 59183afc Christos Stavrakakis
    request.serialization = "json"
117 53b0ea25 Christos Stavrakakis
    data = json.dumps({"floatingips": floating_ips})
118 59183afc Christos Stavrakakis
119 59183afc Christos Stavrakakis
    return HttpResponse(data, status=200)
120 59183afc Christos Stavrakakis
121 59183afc Christos Stavrakakis
122 1cbe2522 Christos Stavrakakis
@api.api_method(http_method="GET", user_required=True, logger=log,
123 1cbe2522 Christos Stavrakakis
                serializations=["json"])
124 59183afc Christos Stavrakakis
def get_floating_ip(request, floating_ip_id):
125 59183afc Christos Stavrakakis
    """Return information for a floating IP."""
126 59183afc Christos Stavrakakis
    userid = request.user_uniq
127 fde2c1f7 Christos Stavrakakis
    floating_ip = util.get_floating_ip_by_id(userid, floating_ip_id)
128 59183afc Christos Stavrakakis
    request.serialization = "json"
129 53b0ea25 Christos Stavrakakis
    data = json.dumps({"floatingip": ip_to_dict(floating_ip)})
130 59183afc Christos Stavrakakis
    return HttpResponse(data, status=200)
131 59183afc Christos Stavrakakis
132 59183afc Christos Stavrakakis
133 1cbe2522 Christos Stavrakakis
@api.api_method(http_method='POST', user_required=True, logger=log,
134 1cbe2522 Christos Stavrakakis
                serializations=["json"])
135 fde2c1f7 Christos Stavrakakis
@transaction.commit_on_success
136 59183afc Christos Stavrakakis
def allocate_floating_ip(request):
137 59183afc Christos Stavrakakis
    """Allocate a floating IP."""
138 59183afc Christos Stavrakakis
    req = utils.get_request_dict(request)
139 e4f484da Marios Kogias
    floating_ip_dict = api.utils.get_attribute(req, "floatingip",
140 e4f484da Marios Kogias
                                               required=True)
141 59183afc Christos Stavrakakis
    log.info('allocate_floating_ip %s', req)
142 59183afc Christos Stavrakakis
143 59183afc Christos Stavrakakis
    userid = request.user_uniq
144 e4f484da Marios Kogias
145 e4f484da Marios Kogias
    # the network_pool is a mandatory field
146 4edfc376 Christos Stavrakakis
    network_id = api.utils.get_attribute(floating_ip_dict,
147 4edfc376 Christos Stavrakakis
                                         "floating_network_id",
148 4edfc376 Christos Stavrakakis
                                         required=False)
149 4edfc376 Christos Stavrakakis
    if network_id is None:
150 0292883e Christos Stavrakakis
        floating_ip = ips.create_floating_ip(userid)
151 4edfc376 Christos Stavrakakis
    else:
152 4edfc376 Christos Stavrakakis
        try:
153 4edfc376 Christos Stavrakakis
            network_id = int(network_id)
154 4edfc376 Christos Stavrakakis
        except ValueError:
155 4edfc376 Christos Stavrakakis
            raise faults.BadRequest("Invalid networkd ID.")
156 4edfc376 Christos Stavrakakis
157 4edfc376 Christos Stavrakakis
        network = util.get_network(network_id, userid, for_update=True,
158 4edfc376 Christos Stavrakakis
                                   non_deleted=True)
159 4edfc376 Christos Stavrakakis
        address = api.utils.get_attribute(floating_ip_dict,
160 4edfc376 Christos Stavrakakis
                                          "floating_ip_address",
161 4edfc376 Christos Stavrakakis
                                          required=False)
162 0292883e Christos Stavrakakis
        floating_ip = ips.create_floating_ip(userid, network, address)
163 fde2c1f7 Christos Stavrakakis
164 fde2c1f7 Christos Stavrakakis
    log.info("User '%s' allocated floating IP '%s'", userid, floating_ip)
165 59183afc Christos Stavrakakis
    request.serialization = "json"
166 53b0ea25 Christos Stavrakakis
    data = json.dumps({"floatingip": ip_to_dict(floating_ip)})
167 59183afc Christos Stavrakakis
    return HttpResponse(data, status=200)
168 59183afc Christos Stavrakakis
169 59183afc Christos Stavrakakis
170 1cbe2522 Christos Stavrakakis
@api.api_method(http_method='DELETE', user_required=True, logger=log,
171 1cbe2522 Christos Stavrakakis
                serializations=["json"])
172 59183afc Christos Stavrakakis
@transaction.commit_on_success
173 59183afc Christos Stavrakakis
def release_floating_ip(request, floating_ip_id):
174 59183afc Christos Stavrakakis
    """Release a floating IP."""
175 59183afc Christos Stavrakakis
    userid = request.user_uniq
176 59183afc Christos Stavrakakis
    log.info("release_floating_ip '%s'. User '%s'.", floating_ip_id, userid)
177 fde2c1f7 Christos Stavrakakis
178 fde2c1f7 Christos Stavrakakis
    floating_ip = util.get_floating_ip_by_id(userid, floating_ip_id,
179 fde2c1f7 Christos Stavrakakis
                                             for_update=True)
180 0292883e Christos Stavrakakis
    ips.delete_floating_ip(floating_ip)
181 59183afc Christos Stavrakakis
    log.info("User '%s' released IP '%s", userid, floating_ip)
182 59183afc Christos Stavrakakis
183 59183afc Christos Stavrakakis
    return HttpResponse(status=204)
184 7d368e73 Christos Stavrakakis
185 7d368e73 Christos Stavrakakis
186 e4f484da Marios Kogias
@api.api_method(http_method='PUT', user_required=True, logger=log,
187 e4f484da Marios Kogias
                serializations=["json"])
188 e4f484da Marios Kogias
@transaction.commit_on_success
189 e4f484da Marios Kogias
def update_floating_ip(request, floating_ip_id):
190 e4f484da Marios Kogias
    """Update a floating IP."""
191 1fb3293d Christos Stavrakakis
    raise faults.NotImplemented("Updating a floating IP is not supported.")
192 d610698e Christos Stavrakakis
    #userid = request.user_uniq
193 d610698e Christos Stavrakakis
    #log.info("update_floating_ip '%s'. User '%s'.", floating_ip_id, userid)
194 d610698e Christos Stavrakakis
195 d610698e Christos Stavrakakis
    #req = utils.get_request_dict(request)
196 d610698e Christos Stavrakakis
    #info = api.utils.get_attribute(req, "floatingip", required=True)
197 d610698e Christos Stavrakakis
198 d610698e Christos Stavrakakis
    #device_id = api.utils.get_attribute(info, "device_id", required=False)
199 d610698e Christos Stavrakakis
200 d610698e Christos Stavrakakis
    #floating_ip = util.get_floating_ip_by_id(userid, floating_ip_id,
201 d610698e Christos Stavrakakis
    #                                         for_update=True)
202 d610698e Christos Stavrakakis
    #if device_id:
203 d610698e Christos Stavrakakis
    #    # attach
204 d610698e Christos Stavrakakis
    #    vm = util.get_vm(device_id, userid)
205 d610698e Christos Stavrakakis
    #    nic, floating_ip = servers.create_nic(vm, ipaddress=floating_ip)
206 d610698e Christos Stavrakakis
    #    backend.connect_to_network(vm, nic)
207 d610698e Christos Stavrakakis
    #else:
208 d610698e Christos Stavrakakis
    #    # dettach
209 d610698e Christos Stavrakakis
    #    nic = floating_ip.nic
210 d610698e Christos Stavrakakis
    #    if not nic:
211 d610698e Christos Stavrakakis
    #        raise faults.BadRequest("The floating IP is not associated\
212 d610698e Christos Stavrakakis
    #                                with any device")
213 d610698e Christos Stavrakakis
    #    vm = nic.machine
214 d610698e Christos Stavrakakis
    #    servers.disconnect(vm, nic)
215 d610698e Christos Stavrakakis
    #return HttpResponse(status=202)
216 e4f484da Marios Kogias
217 e4f484da Marios Kogias
218 fde2c1f7 Christos Stavrakakis
# Floating IP pools
219 1cbe2522 Christos Stavrakakis
@api.api_method(http_method='GET', user_required=True, logger=log,
220 1cbe2522 Christos Stavrakakis
                serializations=["json"])
221 7d368e73 Christos Stavrakakis
def list_floating_ip_pools(request):
222 fde2c1f7 Christos Stavrakakis
    networks = Network.objects.filter(public=True, floating_ip_pool=True,
223 fde2c1f7 Christos Stavrakakis
                                      deleted=False)
224 b6bc4afa Christos Stavrakakis
    networks = utils.filter_modified_since(request, objects=networks)
225 d610698e Christos Stavrakakis
    floating_ip_pools = map(network_to_floating_ip_pool, networks)
226 7d368e73 Christos Stavrakakis
    request.serialization = "json"
227 d610698e Christos Stavrakakis
    data = json.dumps({"floating_ip_pools": floating_ip_pools})
228 7d368e73 Christos Stavrakakis
    request.serialization = "json"
229 7d368e73 Christos Stavrakakis
    return HttpResponse(data, status=200)
230 fde2c1f7 Christos Stavrakakis
231 fde2c1f7 Christos Stavrakakis
232 fde2c1f7 Christos Stavrakakis
def network_to_floating_ip_pool(network):
233 fde2c1f7 Christos Stavrakakis
    """Convert a 'Network' object to a floating IP pool dict."""
234 fde2c1f7 Christos Stavrakakis
    total, free = network.ip_count()
235 fde2c1f7 Christos Stavrakakis
    return {"name": str(network.id),
236 fde2c1f7 Christos Stavrakakis
            "size": total,
237 fde2c1f7 Christos Stavrakakis
            "free": free}