From 8431ed454b715910b943546589b0806f5831e064 Mon Sep 17 00:00:00 2001 From: He Jie Xu Date: Thu, 20 Jun 2013 10:51:44 +0800 Subject: [PATCH] Port hypervisor API into v3 part2 This patch contains the changes required to adapt the hypervisor extension and the corresponding unittest to the v3 framework. Partially implements bp v3-api-extension-versioning Change-Id: I3d98f8a9accf35fdecf67416813bb685e6d54edc --- etc/nova/policy.json | 1 + .../compute/plugins/v3/hypervisors.py | 16 ++-- .../compute/plugins/v3/test_hypervisors.py | 75 +++++++++++++++---- nova/tests/fake_policy.py | 3 +- setup.cfg | 1 + 5 files changed, 75 insertions(+), 21 deletions(-) diff --git a/etc/nova/policy.json b/etc/nova/policy.json index 69fbbb5846..c72506d391 100644 --- a/etc/nova/policy.json +++ b/etc/nova/policy.json @@ -79,6 +79,7 @@ "compute_extension:hide_server_addresses": "is_admin:False", "compute_extension:hosts": "rule:admin_api", "compute_extension:hypervisors": "rule:admin_api", + "compute_extension:v3:os-hypervisors": "rule:admin_api", "compute_extension:image_size": "", "compute_extension:v3:os-images": "", "compute_extension:instance_actions": "", diff --git a/nova/api/openstack/compute/plugins/v3/hypervisors.py b/nova/api/openstack/compute/plugins/v3/hypervisors.py index 26ef2c81b7..3d7834b508 100644 --- a/nova/api/openstack/compute/plugins/v3/hypervisors.py +++ b/nova/api/openstack/compute/plugins/v3/hypervisors.py @@ -24,7 +24,8 @@ from nova import compute from nova import exception -authorize = extensions.extension_authorizer('compute', 'hypervisors') +ALIAS = "os-hypervisors" +authorize = extensions.extension_authorizer('compute', 'v3:' + ALIAS) def make_hypervisor(elem, detail): @@ -243,16 +244,16 @@ class HypervisorsController(object): return dict(hypervisor_statistics=stats) -class Hypervisors(extensions.ExtensionDescriptor): +class Hypervisors(extensions.V3APIExtensionBase): """Admin-only hypervisor administration.""" name = "Hypervisors" - alias = "os-hypervisors" - namespace = "http://docs.openstack.org/compute/ext/hypervisors/api/v1.1" - updated = "2012-06-21T00:00:00+00:00" + alias = ALIAS + namespace = "http://docs.openstack.org/compute/ext/hypervisors/api/v3" + version = 1 def get_resources(self): - resources = [extensions.ResourceExtension('os-hypervisors', + resources = [extensions.ResourceExtension(ALIAS, HypervisorsController(), collection_actions={'detail': 'GET', 'statistics': 'GET'}, @@ -261,3 +262,6 @@ class Hypervisors(extensions.ExtensionDescriptor): 'servers': 'GET'})] return resources + + def get_controller_extensions(self): + return [] diff --git a/nova/tests/api/openstack/compute/plugins/v3/test_hypervisors.py b/nova/tests/api/openstack/compute/plugins/v3/test_hypervisors.py index 7fc7f9ba25..393e8fba41 100644 --- a/nova/tests/api/openstack/compute/plugins/v3/test_hypervisors.py +++ b/nova/tests/api/openstack/compute/plugins/v3/test_hypervisors.py @@ -16,7 +16,7 @@ from lxml import etree from webob import exc -from nova.api.openstack.compute.contrib import hypervisors +from nova.api.openstack.compute.plugins.v3 import hypervisors from nova import context from nova import db from nova.db.sqlalchemy import api as db_api @@ -133,7 +133,6 @@ def fake_instance_get_all_by_host(context, host): class HypervisorsTest(test.TestCase): def setUp(self): super(HypervisorsTest, self).setUp() - self.context = context.get_admin_context() self.controller = hypervisors.HypervisorsController() self.stubs.Set(db, 'compute_node_get_all', fake_compute_node_get_all) @@ -187,17 +186,22 @@ class HypervisorsTest(test.TestCase): dict(name="inst4", uuid="uuid4")])) def test_index(self): - req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors', - use_admin_context=True) + req = fakes.HTTPRequestV3.blank('/os-hypervisors', + use_admin_context=True) result = self.controller.index(req) self.assertEqual(result, dict(hypervisors=[ dict(id=1, hypervisor_hostname="hyper1"), dict(id=2, hypervisor_hostname="hyper2")])) + def test_index_non_admin(self): + req = fakes.HTTPRequestV3.blank('/os-hypervisors') + self.assertRaises(exception.PolicyNotAuthorized, + self.controller.index, req) + def test_detail(self): - req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/detail', - use_admin_context=True) + req = fakes.HTTPRequestV3.blank('/os-hypervisors/detail', + use_admin_context=True) result = self.controller.detail(req) self.assertEqual(result, dict(hypervisors=[ @@ -236,12 +240,24 @@ class HypervisorsTest(test.TestCase): cpu_info='cpu_info', disk_available_least=100)])) + def test_detail_non_admin(self): + req = fakes.HTTPRequestV3.blank('/os-hypervisors/detail') + self.assertRaises(exception.PolicyNotAuthorized, + self.controller.detail, req) + def test_show_noid(self): - req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/3') + req = fakes.HTTPRequestV3.blank('/os-hypervisors/3', + use_admin_context=True) self.assertRaises(exc.HTTPNotFound, self.controller.show, req, '3') + def test_show_non_integer_id(self): + req = fakes.HTTPRequestV3.blank('/os-hypervisors/abc', + use_admin_context=True) + self.assertRaises(exc.HTTPNotFound, self.controller.show, req, 'abc') + def test_show_withid(self): - req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/1') + req = fakes.HTTPRequestV3.blank('/os-hypervisors/1', + use_admin_context=True) result = self.controller.show(req, '1') self.assertEqual(result, dict(hypervisor=dict( @@ -263,8 +279,14 @@ class HypervisorsTest(test.TestCase): cpu_info='cpu_info', disk_available_least=100))) + def test_show_non_admin(self): + req = fakes.HTTPRequestV3.blank('/os-hypervisors/1') + self.assertRaises(exception.PolicyNotAuthorized, + self.controller.show, req, '1') + def test_uptime_noid(self): - req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/3') + req = fakes.HTTPRequestV3.blank('/os-hypervisors/3', + use_admin_context=True) self.assertRaises(exc.HTTPNotFound, self.controller.show, req, '3') def test_uptime_notimplemented(self): @@ -274,7 +296,8 @@ class HypervisorsTest(test.TestCase): self.stubs.Set(self.controller.host_api, 'get_host_uptime', fake_get_host_uptime) - req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/1') + req = fakes.HTTPRequestV3.blank('/os-hypervisors/1', + use_admin_context=True) self.assertRaises(exc.HTTPNotImplemented, self.controller.uptime, req, '1') @@ -285,7 +308,8 @@ class HypervisorsTest(test.TestCase): self.stubs.Set(self.controller.host_api, 'get_host_uptime', fake_get_host_uptime) - req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/1') + req = fakes.HTTPRequestV3.blank('/os-hypervisors/1', + use_admin_context=True) result = self.controller.uptime(req, '1') self.assertEqual(result, dict(hypervisor=dict( @@ -293,8 +317,19 @@ class HypervisorsTest(test.TestCase): hypervisor_hostname="hyper1", uptime="fake uptime"))) + def test_uptime_non_integer_id(self): + req = fakes.HTTPRequestV3.blank('/os-hypervisors/abc/uptime', + use_admin_context=True) + self.assertRaises(exc.HTTPNotFound, self.controller.uptime, req, 'abc') + + def test_uptime_non_admin(self): + req = fakes.HTTPRequestV3.blank('/os-hypervisors/1/uptime') + self.assertRaises(exception.PolicyNotAuthorized, self.controller.uptime, + req, '1') + def test_search(self): - req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/hyper/search') + req = fakes.HTTPRequestV3.blank('/os-hypervisors/hyper/search', + use_admin_context=True) result = self.controller.search(req, 'hyper') self.assertEqual(result, dict(hypervisors=[ @@ -302,7 +337,8 @@ class HypervisorsTest(test.TestCase): dict(id=2, hypervisor_hostname="hyper2")])) def test_servers(self): - req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/hyper/servers') + req = fakes.HTTPRequestV3.blank('/os-hypervisors/hyper/servers', + use_admin_context=True) result = self.controller.servers(req, 'hyper') self.assertEqual(result, dict(hypervisors=[ @@ -317,8 +353,14 @@ class HypervisorsTest(test.TestCase): dict(name="inst2", uuid="uuid2"), dict(name="inst4", uuid="uuid4")])])) + def test_servers_non_admin(self): + req = fakes.HTTPRequestV3.blank('/os-hypervisors/hyper/servers') + self.assertRaises(exception.PolicyNotAuthorized, + self.controller.servers, req, 'hyper') + def test_statistics(self): - req = fakes.HTTPRequest.blank('/v2/fake/os-hypervisors/statistics') + req = fakes.HTTPRequestV3.blank('/os-hypervisors/statistics', + use_admin_context=True) result = self.controller.statistics(req) self.assertEqual(result, dict(hypervisor_statistics=dict( @@ -335,6 +377,11 @@ class HypervisorsTest(test.TestCase): running_vms=4, disk_available_least=200))) + def test_statistics_non_admin(self): + req = fakes.HTTPRequestV3.blank('/os-hypervisors/statistics') + self.assertRaises(exception.PolicyNotAuthorized, + self.controller.statistics, req) + class HypervisorsSerializersTest(test.TestCase): def compare_to_exemplar(self, exemplar, hyper): diff --git a/nova/tests/fake_policy.py b/nova/tests/fake_policy.py index cbd856f4bb..402a2f39c8 100644 --- a/nova/tests/fake_policy.py +++ b/nova/tests/fake_policy.py @@ -17,7 +17,7 @@ policy_data = """ { - "admin_api": "role:admin", + "admin_api": "is_admin:True", "cells_scheduler_filter:TargetCellFilter": "is_admin:True", @@ -155,6 +155,7 @@ policy_data = """ "compute_extension:hide_server_addresses": "", "compute_extension:hosts": "", "compute_extension:hypervisors": "", + "compute_extension:v3:os-hypervisors": "rule:admin_api", "compute_extension:image_size": "", "compute_extension:v3:os-images": "", "compute_extension:instance_actions": "", diff --git a/setup.cfg b/setup.cfg index 0d7e474cd1..c557534e58 100644 --- a/setup.cfg +++ b/setup.cfg @@ -63,6 +63,7 @@ nova.api.v3.extensions = flavors = nova.api.openstack.compute.plugins.v3.flavors:Flavors flavor_access = nova.api.openstack.compute.plugins.v3.flavor_access:FlavorAccess flavor_disabled = nova.api.openstack.compute.plugins.v3.flavor_disabled:FlavorDisabled + hypervisors = nova.api.openstack.compute.plugins.v3.hypervisors:Hypervisors images = nova.api.openstack.compute.plugins.v3.images:Images ips = nova.api.openstack.compute.plugins.v3.ips:IPs keypairs = nova.api.openstack.compute.plugins.v3.keypairs:Keypairs