Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / api / floating_ips.py @ 02353a1a

History | View | Annotate | Download (10.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.logic import ips
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
    (r'^/(\w+)/action(?:.json|.xml)?$', 'floating_ip_action_demux'),
67
)
68

    
69

    
70
def demux(request):
71
    if request.method == 'GET':
72
        return list_floating_ips(request)
73
    elif request.method == 'POST':
74
        return allocate_floating_ip(request)
75
    else:
76
        return api.api_method_not_allowed(request,
77
                                          allowed_methods=['GET', 'POST'])
78

    
79

    
80
def floating_ip_demux(request, floating_ip_id):
81
    if request.method == 'GET':
82
        return get_floating_ip(request, floating_ip_id)
83
    elif request.method == 'DELETE':
84
        return release_floating_ip(request, floating_ip_id)
85
    elif request.method == 'PUT':
86
        return update_floating_ip(request, floating_ip_id)
87
    else:
88
        return api.api_method_not_allowed(request,
89
                                          allowed_methods=['GET', 'DELETE'])
90

    
91

    
92
@api.api_method(http_method='POST', user_required=True, logger=log,
93
                serializations=["json"])
94
def floating_ip_action_demux(request, floating_ip_id):
95
    userid = request.user_uniq
96
    req = utils.get_request_dict(request)
97
    log.debug('floating_ip_action %s %s', floating_ip_id, req)
98
    if len(req) != 1:
99
        raise faults.BadRequest('Malformed request.')
100
    floating_ip = util.get_floating_ip_by_id(userid,
101
                                             floating_ip_id,
102
                                             for_update=True)
103
    action = req.keys()[0]
104
    try:
105
        f = FLOATING_IP_ACTIONS[action]
106
    except KeyError:
107
        raise faults.BadRequest("Action %s not supported." % action)
108
    action_args = req[action]
109
    if not isinstance(action_args, dict):
110
        raise faults.BadRequest("Invalid argument.")
111

    
112
    return f(request, floating_ip, action_args)
113

    
114

    
115
def ip_to_dict(floating_ip):
116
    machine_id = None
117
    port_id = None
118
    if floating_ip.nic is not None:
119
        machine_id = floating_ip.nic.machine_id
120
        port_id = floating_ip.nic.id
121
    return {"fixed_ip_address": None,
122
            "id": str(floating_ip.id),
123
            "instance_id": str(machine_id) if machine_id else None,
124
            "floating_ip_address": floating_ip.address,
125
            "port_id": str(port_id) if port_id else None,
126
            "floating_network_id": str(floating_ip.network_id),
127
            "user_id": floating_ip.userid,
128
            "tenant_id": floating_ip.project,
129
            "deleted": floating_ip.deleted}
130

    
131

    
132
@api.api_method(http_method="GET", user_required=True, logger=log,
133
                serializations=["json"])
134
def list_floating_ips(request):
135
    """Return user reserved floating IPs"""
136
    log.debug("list_floating_ips")
137

    
138
    userid = request.user_uniq
139
    floating_ips = IPAddress.objects.filter(userid=userid, deleted=False,
140
                                            floating_ip=True).order_by("id")\
141
                                    .select_related("nic")
142
    floating_ips = utils.filter_modified_since(request, objects=floating_ips)
143

    
144
    floating_ips = map(ip_to_dict, floating_ips)
145

    
146
    request.serialization = "json"
147
    data = json.dumps({"floatingips": floating_ips})
148

    
149
    return HttpResponse(data, status=200)
150

    
151

    
152
@api.api_method(http_method="GET", user_required=True, logger=log,
153
                serializations=["json"])
154
def get_floating_ip(request, floating_ip_id):
155
    """Return information for a floating IP."""
156
    userid = request.user_uniq
157
    floating_ip = util.get_floating_ip_by_id(userid, floating_ip_id)
158
    request.serialization = "json"
159
    data = json.dumps({"floatingip": ip_to_dict(floating_ip)})
160
    return HttpResponse(data, status=200)
161

    
162

    
163
@api.api_method(http_method='POST', user_required=True, logger=log,
164
                serializations=["json"])
165
@transaction.commit_on_success
166
def allocate_floating_ip(request):
167
    """Allocate a floating IP."""
168
    req = utils.get_request_dict(request)
169
    floating_ip_dict = api.utils.get_attribute(req, "floatingip",
170
                                               required=True, attr_type=dict)
171
    log.info('allocate_floating_ip %s', req)
172

    
173
    userid = request.user_uniq
174
    project = floating_ip_dict.get("project", None)
175

    
176
    # the network_pool is a mandatory field
177
    network_id = api.utils.get_attribute(floating_ip_dict,
178
                                         "floating_network_id",
179
                                         required=False,
180
                                         attr_type=(basestring, int))
181
    if network_id is None:
182
        floating_ip = ips.create_floating_ip(userid, project=project)
183
    else:
184
        try:
185
            network_id = int(network_id)
186
        except ValueError:
187
            raise faults.BadRequest("Invalid networkd ID.")
188

    
189
        network = util.get_network(network_id, userid, for_update=True,
190
                                   non_deleted=True)
191
        address = api.utils.get_attribute(floating_ip_dict,
192
                                          "floating_ip_address",
193
                                          required=False,
194
                                          attr_type=basestring)
195
        floating_ip = ips.create_floating_ip(userid, network, address,
196
                                             project=project)
197

    
198
    log.info("User '%s' allocated floating IP '%s'", userid, floating_ip)
199
    request.serialization = "json"
200
    data = json.dumps({"floatingip": ip_to_dict(floating_ip)})
201
    return HttpResponse(data, status=200)
202

    
203

    
204
@api.api_method(http_method='DELETE', user_required=True, logger=log,
205
                serializations=["json"])
206
@transaction.commit_on_success
207
def release_floating_ip(request, floating_ip_id):
208
    """Release a floating IP."""
209
    userid = request.user_uniq
210
    log.info("release_floating_ip '%s'. User '%s'.", floating_ip_id, userid)
211

    
212
    floating_ip = util.get_floating_ip_by_id(userid, floating_ip_id,
213
                                             for_update=True)
214
    ips.delete_floating_ip(floating_ip)
215
    log.info("User '%s' released IP '%s", userid, floating_ip)
216

    
217
    return HttpResponse(status=204)
218

    
219

    
220
@api.api_method(http_method='PUT', user_required=True, logger=log,
221
                serializations=["json"])
222
@transaction.commit_on_success
223
def update_floating_ip(request, floating_ip_id):
224
    """Update a floating IP."""
225
    raise faults.NotImplemented("Updating a floating IP is not supported.")
226
    #userid = request.user_uniq
227
    #log.info("update_floating_ip '%s'. User '%s'.", floating_ip_id, userid)
228

    
229
    #req = utils.get_request_dict(request)
230
    #info = api.utils.get_attribute(req, "floatingip", required=True)
231

    
232
    #device_id = api.utils.get_attribute(info, "device_id", required=False)
233

    
234
    #floating_ip = util.get_floating_ip_by_id(userid, floating_ip_id,
235
    #                                         for_update=True)
236
    #if device_id:
237
    #    # attach
238
    #    vm = util.get_vm(device_id, userid)
239
    #    nic, floating_ip = servers.create_nic(vm, ipaddress=floating_ip)
240
    #    backend.connect_to_network(vm, nic)
241
    #else:
242
    #    # dettach
243
    #    nic = floating_ip.nic
244
    #    if not nic:
245
    #        raise faults.BadRequest("The floating IP is not associated\
246
    #                                with any device")
247
    #    vm = nic.machine
248
    #    servers.disconnect(vm, nic)
249
    #return HttpResponse(status=202)
250

    
251

    
252
# Floating IP pools
253
@api.api_method(http_method='GET', user_required=True, logger=log,
254
                serializations=["json"])
255
def list_floating_ip_pools(request):
256
    networks = Network.objects.filter(public=True, floating_ip_pool=True,
257
                                      deleted=False)
258
    networks = utils.filter_modified_since(request, objects=networks)
259
    floating_ip_pools = map(network_to_floating_ip_pool, networks)
260
    request.serialization = "json"
261
    data = json.dumps({"floating_ip_pools": floating_ip_pools})
262
    request.serialization = "json"
263
    return HttpResponse(data, status=200)
264

    
265

    
266
@transaction.commit_on_success
267
def reassign(request, floating_ip, args):
268
    project = args.get("project")
269
    if project is None:
270
        raise faults.BadRequest("Missing 'project' attribute.")
271
    ips.reassign_floating_ip(floating_ip, project)
272
    return HttpResponse(status=200)
273

    
274

    
275
FLOATING_IP_ACTIONS = {
276
    "reassign": reassign,
277
}
278

    
279

    
280
def network_to_floating_ip_pool(network):
281
    """Convert a 'Network' object to a floating IP pool dict."""
282
    total, free = network.ip_count()
283
    return {"name": str(network.id),
284
            "size": total,
285
            "free": free,
286
            "deleted": network.deleted}