summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexei Kornienko <akornienko@mirantis.com>2013-06-27 10:49:44 +0300
committerAlexei Kornienko <akornienko@mirantis.com>2013-06-27 10:53:14 +0300
commitdfc58542d4fb87e9dcb0f4c969ccc18dbe1c24bd (patch)
tree71db570d700da7e586279e363ef5f375255c02a5
parentec66193e0363967d8e608606bcee8cdaff999745 (diff)
downloadnova-dfc58542d4fb87e9dcb0f4c969ccc18dbe1c24bd.tar.gz
nova-dfc58542d4fb87e9dcb0f4c969ccc18dbe1c24bd.tar.xz
nova-dfc58542d4fb87e9dcb0f4c969ccc18dbe1c24bd.zip
Port flavor_disabled extension to v3 API Part 1
This changeset only copies the v2 files (implementation and test) into the appropriate v3 directories unchanged. The copy as-is will not be loaded by either the v2 or v3 extension loaders. The second changeset will then make the changes required for it to work as a v3 extension. This is being done in order to make reviewing of extension porting easier as gerrit will display only what is actually changed for v3 rather than entirely new files Partially implements blueprint nova-v3-api Change-Id: I01688d7f9d976b7e406d46283c6d0010ddc3476a
-rw-r--r--nova/api/openstack/compute/plugins/v3/flavor_disabled.py89
-rw-r--r--nova/tests/api/openstack/compute/plugins/v3/test_flavor_disabled.py106
2 files changed, 195 insertions, 0 deletions
diff --git a/nova/api/openstack/compute/plugins/v3/flavor_disabled.py b/nova/api/openstack/compute/plugins/v3/flavor_disabled.py
new file mode 100644
index 000000000..62f902409
--- /dev/null
+++ b/nova/api/openstack/compute/plugins/v3/flavor_disabled.py
@@ -0,0 +1,89 @@
+# Copyright 2012 Nebula, Inc.
+#
+# 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.
+
+"""The Flavor Disabled API extension."""
+
+from nova.api.openstack import extensions
+from nova.api.openstack import wsgi
+from nova.api.openstack import xmlutil
+
+
+authorize = extensions.soft_extension_authorizer('compute', 'flavor_disabled')
+
+
+class FlavorDisabledController(wsgi.Controller):
+ def _extend_flavors(self, req, flavors):
+ for flavor in flavors:
+ db_flavor = req.get_db_flavor(flavor['id'])
+ key = "%s:disabled" % Flavor_disabled.alias
+ flavor[key] = db_flavor['disabled']
+
+ def _show(self, req, resp_obj):
+ if not authorize(req.environ['nova.context']):
+ return
+ if 'flavor' in resp_obj.obj:
+ resp_obj.attach(xml=FlavorDisabledTemplate())
+ self._extend_flavors(req, [resp_obj.obj['flavor']])
+
+ @wsgi.extends
+ def show(self, req, resp_obj, id):
+ return self._show(req, resp_obj)
+
+ @wsgi.extends(action='create')
+ def create(self, req, resp_obj, body):
+ return self._show(req, resp_obj)
+
+ @wsgi.extends
+ def detail(self, req, resp_obj):
+ if not authorize(req.environ['nova.context']):
+ return
+ resp_obj.attach(xml=FlavorsDisabledTemplate())
+ self._extend_flavors(req, list(resp_obj.obj['flavors']))
+
+
+class Flavor_disabled(extensions.ExtensionDescriptor):
+ """Support to show the disabled status of a flavor."""
+
+ name = "FlavorDisabled"
+ alias = "OS-FLV-DISABLED"
+ namespace = ("http://docs.openstack.org/compute/ext/"
+ "flavor_disabled/api/v1.1")
+ updated = "2012-08-29T00:00:00+00:00"
+
+ def get_controller_extensions(self):
+ controller = FlavorDisabledController()
+ extension = extensions.ControllerExtension(self, 'flavors', controller)
+ return [extension]
+
+
+def make_flavor(elem):
+ elem.set('{%s}disabled' % Flavor_disabled.namespace,
+ '%s:disabled' % Flavor_disabled.alias)
+
+
+class FlavorDisabledTemplate(xmlutil.TemplateBuilder):
+ def construct(self):
+ root = xmlutil.TemplateElement('flavor', selector='flavor')
+ make_flavor(root)
+ return xmlutil.SlaveTemplate(root, 1, nsmap={
+ Flavor_disabled.alias: Flavor_disabled.namespace})
+
+
+class FlavorsDisabledTemplate(xmlutil.TemplateBuilder):
+ def construct(self):
+ root = xmlutil.TemplateElement('flavors')
+ elem = xmlutil.SubTemplateElement(root, 'flavor', selector='flavors')
+ make_flavor(elem)
+ return xmlutil.SlaveTemplate(root, 1, nsmap={
+ Flavor_disabled.alias: Flavor_disabled.namespace})
diff --git a/nova/tests/api/openstack/compute/plugins/v3/test_flavor_disabled.py b/nova/tests/api/openstack/compute/plugins/v3/test_flavor_disabled.py
new file mode 100644
index 000000000..e46e02a44
--- /dev/null
+++ b/nova/tests/api/openstack/compute/plugins/v3/test_flavor_disabled.py
@@ -0,0 +1,106 @@
+# Copyright 2012 Nebula, Inc.
+#
+# 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.
+
+from lxml import etree
+import webob
+
+from nova.api.openstack.compute.contrib import flavor_disabled
+from nova.compute import flavors
+from nova.openstack.common import jsonutils
+from nova import test
+from nova.tests.api.openstack import fakes
+
+FAKE_FLAVORS = {
+ 'flavor 1': {
+ "flavorid": '1',
+ "name": 'flavor 1',
+ "memory_mb": '256',
+ "root_gb": '10',
+ "disabled": False,
+ },
+ 'flavor 2': {
+ "flavorid": '2',
+ "name": 'flavor 2',
+ "memory_mb": '512',
+ "root_gb": '20',
+ "disabled": True,
+ },
+}
+
+
+def fake_flavor_get_by_flavor_id(flavorid):
+ return FAKE_FLAVORS['flavor %s' % flavorid]
+
+
+def fake_flavor_get_all(*args, **kwargs):
+ return FAKE_FLAVORS
+
+
+class FlavorDisabledTest(test.TestCase):
+ content_type = 'application/json'
+ prefix = '%s:' % flavor_disabled.Flavor_disabled.alias
+
+ def setUp(self):
+ super(FlavorDisabledTest, self).setUp()
+ ext = ('nova.api.openstack.compute.contrib'
+ '.flavor_disabled.Flavor_disabled')
+ self.flags(osapi_compute_extension=[ext])
+ fakes.stub_out_nw_api(self.stubs)
+ self.stubs.Set(flavors, "get_all_flavors",
+ fake_flavor_get_all)
+ self.stubs.Set(flavors,
+ "get_flavor_by_flavor_id",
+ fake_flavor_get_by_flavor_id)
+
+ def _make_request(self, url):
+ req = webob.Request.blank(url)
+ req.headers['Accept'] = self.content_type
+ res = req.get_response(fakes.wsgi_app())
+ return res
+
+ def _get_flavor(self, body):
+ return jsonutils.loads(body).get('flavor')
+
+ def _get_flavors(self, body):
+ return jsonutils.loads(body).get('flavors')
+
+ def assertFlavorDisabled(self, flavor, disabled):
+ self.assertEqual(str(flavor.get('%sdisabled' % self.prefix)), disabled)
+
+ def test_show(self):
+ url = '/v2/fake/flavors/1'
+ res = self._make_request(url)
+
+ self.assertEqual(res.status_int, 200)
+ self.assertFlavorDisabled(self._get_flavor(res.body), 'False')
+
+ def test_detail(self):
+ url = '/v2/fake/flavors/detail'
+ res = self._make_request(url)
+
+ self.assertEqual(res.status_int, 200)
+ flavors = self._get_flavors(res.body)
+ self.assertFlavorDisabled(flavors[0], 'False')
+ self.assertFlavorDisabled(flavors[1], 'True')
+
+
+class FlavorDisabledXmlTest(FlavorDisabledTest):
+ content_type = 'application/xml'
+ prefix = '{%s}' % flavor_disabled.Flavor_disabled.namespace
+
+ def _get_flavor(self, body):
+ return etree.XML(body)
+
+ def _get_flavors(self, body):
+ return etree.XML(body).getchildren()