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 Swap 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_swap')
25
class FlavorSwapController(wsgi.Controller):
26
def _extend_flavors(self, req, flavors):
27
for flavor in flavors:
28
db_flavor = req.get_db_flavor(flavor['id'])
30
flavor[key] = db_flavor['swap'] or ""
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=FlavorSwapTemplate())
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=FlavorsSwapTemplate())
52
self._extend_flavors(req, list(resp_obj.obj['flavors']))
55
class Flavor_swap(extensions.ExtensionDescriptor):
56
"""Support to show the swap status of a flavor"""
59
alias = "os-flavor-swap"
60
namespace = ("http://docs.openstack.org/compute/ext/"
61
"flavor_swap/api/v1.1")
62
updated = "2012-08-29T00:00:00+00:00"
64
def get_controller_extensions(self):
65
controller = FlavorSwapController()
66
extension = extensions.ControllerExtension(self, 'flavors', controller)
70
def make_flavor(elem):
71
# NOTE(vish): this was originally added without a namespace
72
elem.set('swap', xmlutil.EmptyStringSelector('swap'))
75
class FlavorSwapTemplate(xmlutil.TemplateBuilder):
77
root = xmlutil.TemplateElement('flavor', selector='flavor')
79
return xmlutil.SlaveTemplate(root, 1)
82
class FlavorsSwapTemplate(xmlutil.TemplateBuilder):
84
root = xmlutil.TemplateElement('flavors')
85
elem = xmlutil.SubTemplateElement(root, 'flavor', selector='flavors')
87
return xmlutil.SlaveTemplate(root, 1)