1
# Copyright 2012 Nebula, Inc.
3
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4
# not use this file except in compliance with the License. You may obtain
5
# a copy of the License at
7
# http://www.apache.org/licenses/LICENSE-2.0
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
# License for the specific language governing permissions and limitations
15
"""The Flavor Disabled API extension."""
17
from nova.api.openstack import extensions
18
from nova.api.openstack import wsgi
19
from nova.api.openstack import xmlutil
22
authorize = extensions.soft_extension_authorizer('compute', 'flavor_disabled')
25
class FlavorDisabledController(wsgi.Controller):
26
def _extend_flavors(self, req, flavors):
27
for flavor in flavors:
28
db_flavor = req.get_db_flavor(flavor['id'])
29
key = "%s:disabled" % Flavor_disabled.alias
30
flavor[key] = db_flavor['disabled']
32
def _show(self, req, resp_obj):
33
if not authorize(req.environ['nova.context']):
35
if 'flavor' in resp_obj.obj:
36
resp_obj.attach(xml=FlavorDisabledTemplate())
37
self._extend_flavors(req, [resp_obj.obj['flavor']])
40
def show(self, req, resp_obj, id):
41
return self._show(req, resp_obj)
43
@wsgi.extends(action='create')
44
def create(self, req, resp_obj, body):
45
return self._show(req, resp_obj)
48
def detail(self, req, resp_obj):
49
if not authorize(req.environ['nova.context']):
51
resp_obj.attach(xml=FlavorsDisabledTemplate())
52
self._extend_flavors(req, list(resp_obj.obj['flavors']))
55
class Flavor_disabled(extensions.ExtensionDescriptor):
56
"""Support to show the disabled status of a flavor"""
58
name = "FlavorDisabled"
59
alias = "OS-FLV-DISABLED"
60
namespace = ("http://docs.openstack.org/compute/ext/"
61
"flavor_disabled/api/v1.1")
62
updated = "2012-08-29T00:00:00+00:00"
64
def get_controller_extensions(self):
65
controller = FlavorDisabledController()
66
extension = extensions.ControllerExtension(self, 'flavors', controller)
70
def make_flavor(elem):
71
elem.set('{%s}disabled' % Flavor_disabled.namespace,
72
'%s:disabled' % Flavor_disabled.alias)
75
class FlavorDisabledTemplate(xmlutil.TemplateBuilder):
77
root = xmlutil.TemplateElement('flavor', selector='flavor')
79
return xmlutil.SlaveTemplate(root, 1, nsmap={
80
Flavor_disabled.alias: Flavor_disabled.namespace})
83
class FlavorsDisabledTemplate(xmlutil.TemplateBuilder):
85
root = xmlutil.TemplateElement('flavors')
86
elem = xmlutil.SubTemplateElement(root, 'flavor', selector='flavors')
88
return xmlutil.SlaveTemplate(root, 1, nsmap={
89
Flavor_disabled.alias: Flavor_disabled.namespace})