From f1e19b46cb6efdcd4b446095ef5efcb2a7c8359c Mon Sep 17 00:00:00 2001 From: Chris Yeoh Date: Wed, 24 Oct 2012 16:42:03 +1030 Subject: Adds REST API support for Fixed IPs This adds an extension that provides a REST API for getting information about a fixed ip, reserving a fixed ip and unreserving a fixed ip. The interface is accessed via /v2/{tenant_id}/os-fixed-ips/ # GET ip info /v2/{tenant_id}/os-fixed-ips//action # POST reserve/unreserve ip This forms part of the work to provide APIs for functionality currently implemented by nova-manage that needs direct db access so nova-manage can eventually be removed Adds db function fixed_ip_get_by_address_detailed in order to optimise being able to get the instance and network information for a fixed ip at the same time as the rest of the fixed ip information Change-Id: I92edf4e6b74b14bb9c49d5bc0c79e40d3a496bc5 Implements: blueprint apis-for-nova-manage DocImpact --- nova/api/openstack/compute/contrib/fixed_ips.py | 100 ++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 nova/api/openstack/compute/contrib/fixed_ips.py (limited to 'nova/api') diff --git a/nova/api/openstack/compute/contrib/fixed_ips.py b/nova/api/openstack/compute/contrib/fixed_ips.py new file mode 100644 index 000000000..178847d64 --- /dev/null +++ b/nova/api/openstack/compute/contrib/fixed_ips.py @@ -0,0 +1,100 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2012 IBM +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import urllib +import webob.exc + +from nova.api.openstack import extensions +from nova.api.openstack import wsgi +from nova import db +from nova import exception +from nova.openstack.common import log as logging + +LOG = logging.getLogger(__name__) +authorize = extensions.extension_authorizer('compute', 'fixed_ips') + + +class FixedIPController(object): + def show(self, req, id): + """Return data about the given fixed ip.""" + context = req.environ['nova.context'] + authorize(context) + + try: + fixed_ip = db.fixed_ip_get_by_address_detailed(context, id) + except exception.FixedIpNotFoundForAddress as ex: + raise webob.exc.HTTPNotFound(explanation=str(ex)) + + fixed_ip_info = {"fixed_ip": {}} + if fixed_ip[1] is None: + msg = _("Fixed IP %s has been deleted") % id + raise webob.exc.HTTPNotFound(explanation=msg) + + fixed_ip_info['fixed_ip']['cidr'] = fixed_ip[1]['cidr'] + fixed_ip_info['fixed_ip']['address'] = fixed_ip[0]['address'] + + if fixed_ip[2]: + fixed_ip_info['fixed_ip']['hostname'] = fixed_ip[2]['hostname'] + fixed_ip_info['fixed_ip']['host'] = fixed_ip[2]['host'] + else: + fixed_ip_info['fixed_ip']['hostname'] = None + fixed_ip_info['fixed_ip']['host'] = None + + return fixed_ip_info + + def action(self, req, id, body): + context = req.environ['nova.context'] + authorize(context) + if 'reserve' in body: + return self._set_reserved(context, id, True) + elif 'unreserve' in body: + return self._set_reserved(context, id, False) + else: + raise webob.exc.HTTPBadRequest( + explanation="No valid action specified") + + def _set_reserved(self, context, address, reserved): + try: + fixed_ip = db.fixed_ip_get_by_address(context, address) + db.fixed_ip_update(context, fixed_ip['address'], + {'reserved': reserved}) + except exception.FixedIpNotFoundForAddress as ex: + msg = _("Fixed IP %s not found") % address + raise webob.exc.HTTPNotFound(explanation=msg) + + return webob.exc.HTTPAccepted() + + +class Fixed_ips(extensions.ExtensionDescriptor): + """Fixed IPs support""" + + name = "FixedIPs" + alias = "os-fixed-ips" + namespace = "http://docs.openstack.org/compute/ext/fixed_ips/api/v2" + updated = "2012-10-18T13:25:27-06:00" + + def __init__(self, ext_mgr): + ext_mgr.register(self) + + def get_resources(self): + member_actions = {'action': 'POST'} + resources = [] + resource = extensions.ResourceExtension('os-fixed-ips', + FixedIPController(), + member_actions=member_actions) + resources.append(resource) + return resources -- cgit