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 --- .../openstack/compute/contrib/test_fixed_ips.py | 164 +++++++++++++++++++++ .../tests/api/openstack/compute/test_extensions.py | 1 + .../all_extensions/extensions-get-resp.json.tpl | 8 + .../all_extensions/extensions-get-resp.xml.tpl | 3 + nova/tests/policy.json | 1 + 5 files changed, 177 insertions(+) create mode 100644 nova/tests/api/openstack/compute/contrib/test_fixed_ips.py (limited to 'nova/tests') diff --git a/nova/tests/api/openstack/compute/contrib/test_fixed_ips.py b/nova/tests/api/openstack/compute/contrib/test_fixed_ips.py new file mode 100644 index 000000000..4d5ca5e5c --- /dev/null +++ b/nova/tests/api/openstack/compute/contrib/test_fixed_ips.py @@ -0,0 +1,164 @@ +# 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 webob + +from nova.api.openstack.compute.contrib import fixed_ips +from nova import context +from nova import db +from nova import exception +from nova.openstack.common import jsonutils +from nova import test +from nova.tests.api.openstack import fakes + + +fake_fixed_ips = [{'id': 1, + 'address': '192.168.1.1', + 'network_id': 1, + 'virtual_interface_id': 1, + 'instance_uuid': '1', + 'allocated': False, + 'leased': False, + 'reserved': False, + 'host': None}, + {'id': 2, + 'address': '192.168.1.2', + 'network_id': 1, + 'virtual_interface_id': 2, + 'instance_uuid': '2', + 'allocated': False, + 'leased': False, + 'reserved': False, + 'host': None}, + ] + + +def fake_fixed_ip_get_by_address(context, address): + for fixed_ip in fake_fixed_ips: + if fixed_ip['address'] == address: + return fixed_ip + raise exception.FixedIpNotFoundForAddress(address=address) + + +def fake_fixed_ip_get_by_address_detailed(context, address): + network = {'id': 1, + 'cidr': "192.168.1.0/24"} + for fixed_ip in fake_fixed_ips: + if fixed_ip['address'] == address: + return (fixed_ip, FakeModel(network), None) + raise exception.FixedIpNotFoundForAddress(address=address) + + +def fake_fixed_ip_update(context, address, values): + fixed_ip = fake_fixed_ip_get_by_address(context, address) + if fixed_ip is None: + raise exception.FixedIpNotFoundForAddress(address=address) + else: + for key in values: + fixed_ip[key] = values[key] + + +class FakeModel(object): + """Stubs out for model.""" + def __init__(self, values): + self.values = values + + def __getattr__(self, name): + return self.values[name] + + def __getitem__(self, key): + if key in self.values: + return self.values[key] + else: + raise NotImplementedError() + + def __repr__(self): + return '' % self.values + + +def fake_network_get_all(context): + network = {'id': 1, + 'cidr': "192.168.1.0/24"} + return [FakeModel(network)] + + +class FixedIpTest(test.TestCase): + + def setUp(self): + super(FixedIpTest, self).setUp() + + self.stubs.Set(db, "fixed_ip_get_by_address", + fake_fixed_ip_get_by_address) + self.stubs.Set(db, "fixed_ip_get_by_address_detailed", + fake_fixed_ip_get_by_address_detailed) + self.stubs.Set(db, "fixed_ip_update", fake_fixed_ip_update) + + self.context = context.get_admin_context() + self.controller = fixed_ips.FixedIPController() + + def tearDown(self): + super(FixedIpTest, self).tearDown() + + def test_fixed_ips_get(self): + req = fakes.HTTPRequest.blank('/v2/fake/os-fixed-ips/192.168.1.1') + res_dict = self.controller.show(req, '192.168.1.1') + response = {'fixed_ip': {'cidr': '192.168.1.0/24', + 'hostname': None, + 'host': None, + 'address': '192.168.1.1'}} + self.assertEqual(response, res_dict) + + def test_fixed_ips_get_fail(self): + req = fakes.HTTPRequest.blank('/v2/fake/os-fixed-ips/10.0.0.1') + self.assertRaises(webob.exc.HTTPNotFound, self.controller.show, req, + '10.0.0.1') + + def test_fixed_ip_reserve(self): + fake_fixed_ips[0]['reserved'] = False + ip_addr = '192.168.1.1' + body = {'reserve': None} + req = fakes.HTTPRequest.blank( + '/v2/fake/os-fixed-ips/192.168.1.1/action') + result = self.controller.action(req, "192.168.1.1", body) + + self.assertEqual('202 Accepted', result.status) + self.assertEqual(fake_fixed_ips[0]['reserved'], True) + + def test_fixed_ip_reserve_bad_ip(self): + ip_addr = '10.0.0.1' + body = {'reserve': None} + req = fakes.HTTPRequest.blank( + '/v2/fake/os-fixed-ips/10.0.0.1/action') + self.assertRaises(webob.exc.HTTPNotFound, self.controller.action, req, + '10.0.0.1', body) + + def test_fixed_ip_unreserve(self): + fake_fixed_ips[0]['reserved'] = True + ip_addr = '192.168.1.1' + body = {'unreserve': None} + req = fakes.HTTPRequest.blank( + '/v2/fake/os-fixed-ips/192.168.1.1/action') + result = self.controller.action(req, "192.168.1.1", body) + + self.assertEqual('202 Accepted', result.status) + self.assertEqual(fake_fixed_ips[0]['reserved'], False) + + def test_fixed_ip_unreserve_bad_ip(self): + ip_addr = '10.0.0.1' + body = {'unreserve': None} + req = fakes.HTTPRequest.blank( + '/v2/fake/os-fixed-ips/10.0.0.1/action') + self.assertRaises(webob.exc.HTTPNotFound, self.controller.action, req, + '10.0.0.1', body) diff --git a/nova/tests/api/openstack/compute/test_extensions.py b/nova/tests/api/openstack/compute/test_extensions.py index f3e65a150..8282c4c19 100644 --- a/nova/tests/api/openstack/compute/test_extensions.py +++ b/nova/tests/api/openstack/compute/test_extensions.py @@ -167,6 +167,7 @@ class ExtensionControllerTest(ExtensionTestCase): "DiskConfig", "ExtendedStatus", "ExtendedServerAttributes", + "FixedIPs", "FlavorAccess", "FlavorDisabled", "FlavorExtraSpecs", diff --git a/nova/tests/integrated/api_samples/all_extensions/extensions-get-resp.json.tpl b/nova/tests/integrated/api_samples/all_extensions/extensions-get-resp.json.tpl index 34f305967..2ed3486eb 100644 --- a/nova/tests/integrated/api_samples/all_extensions/extensions-get-resp.json.tpl +++ b/nova/tests/integrated/api_samples/all_extensions/extensions-get-resp.json.tpl @@ -136,6 +136,14 @@ "namespace": "http://docs.openstack.org/compute/ext/deferred-delete/api/v1.1", "updated": "%(timestamp)s" }, + { + "alias": "os-fixed-ips", + "description": "Fixed IPs support", + "links": [], + "name": "FixedIPs", + "namespace": "http://docs.openstack.org/compute/ext/fixed_ips/api/v2", + "updated": "2012-10-18T13:25:27-06:00" + }, { "alias": "os-flavor-access", "description": "%(text)s", diff --git a/nova/tests/integrated/api_samples/all_extensions/extensions-get-resp.xml.tpl b/nova/tests/integrated/api_samples/all_extensions/extensions-get-resp.xml.tpl index 44aa5ee81..3962ebe9b 100644 --- a/nova/tests/integrated/api_samples/all_extensions/extensions-get-resp.xml.tpl +++ b/nova/tests/integrated/api_samples/all_extensions/extensions-get-resp.xml.tpl @@ -51,6 +51,9 @@ %(text)s + + Fixed IPs support + %(text)s diff --git a/nova/tests/policy.json b/nova/tests/policy.json index bf94d4e49..6381a9031 100644 --- a/nova/tests/policy.json +++ b/nova/tests/policy.json @@ -94,6 +94,7 @@ "compute_extension:disk_config": "", "compute_extension:extended_server_attributes": "", "compute_extension:extended_status": "", + "compute_extension:fixed_ips": "", "compute_extension:flavor_access": "", "compute_extension:flavor_disabled": "", "compute_extension:flavor_rxtx": "", -- cgit