diff options
author | Michael Elkins <melkins@tislabs.com> | 2012-11-20 21:35:07 +0000 |
---|---|---|
committer | Michael Elkins <melkins@tislabs.com> | 2012-11-20 21:35:07 +0000 |
commit | 325920e9014c61eed600f4fb6b11963322720c20 (patch) | |
tree | 48e181d3ed814fa66d9a43c3a8a4d0af837ddaa3 /rpkid/rpki/gui/routeview/api.py | |
parent | cfddfa4faf359737ab90d6d063d2997a3d6c9add (diff) |
add support for client side route lookup in the roa creation form
see #329
svn path=/branches/tk329/; revision=4889
Diffstat (limited to 'rpkid/rpki/gui/routeview/api.py')
-rw-r--r-- | rpkid/rpki/gui/routeview/api.py | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/rpkid/rpki/gui/routeview/api.py b/rpkid/rpki/gui/routeview/api.py new file mode 100644 index 00000000..cf699c9a --- /dev/null +++ b/rpkid/rpki/gui/routeview/api.py @@ -0,0 +1,69 @@ +# Copyright (C) 2012 SPARTA, Inc. a Parsons Company +# +# Permission to use, copy, modify, and distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND SPARTA DISCLAIMS ALL WARRANTIES WITH +# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +# AND FITNESS. IN NO EVENT SHALL SPARTA BE LIABLE FOR ANY SPECIAL, DIRECT, +# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE +# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +# PERFORMANCE OF THIS SOFTWARE. + +__version__ = '$Id$' + +import json +from django import http +from rpki.gui.routeview.models import RouteOrigin, RouteOriginV6 +from rpki import resource_set +import rpki.exceptions + +def route_list(request): + """Implements the REST query against the route models to allow the client + to search for routes. + + The only search currently supported is returning all the routes covered by + the prefix given in the 'prefix__in=' query string parameter. + + By default, only returns up to 10 matching routes, but the client may + request a different limit with the 'count=' query string parameter. + + """ + hard_limit = 100 + + if request.method == 'GET' and 'prefix__in' in request.GET: + # find all routers covered by this prefix + match_prefix = request.GET.get('prefix__in') + # max number of items to return + limit = request.GET.get('count', 10) + if limit < 1 or limit > hard_limit: + return http.HttpResponseBadRequest('invalid value for count parameter') + + try: + if ':' in match_prefix: + # v6 + pfx = resource_set.resource_range_ipv6.parse_str(match_prefix) + manager = RouteOriginV6 + else: + # v4 + pfx = resource_set.resource_range_ipv4.parse_str(match_prefix) + manager = RouteOrigin + except (AssertionError, rpki.exceptions.BadIPResource), e: + return http.HttpResponseBadRequest(e) + + try: + qs = manager.objects.filter(prefix_min__gte=pfx.min, + prefix_max__lte=pfx.max)[:limit] + # FIXME - a REST API should really return the url of the resource, + # but since we are combining two separate tables, the .pk is not a + # unique identifier. + matches = [{'prefix': str(x.as_resource_range()), 'asn': x.asn} for x in qs] + except IndexError: + # no matches + matches = [] + + return http.HttpResponse(json.dumps(matches), content_type='text/javascript') + + return http.HttpResponseBadRequest() |