**** CubicPower OpenStack Study ****
# Copyright 2012 OpenStack Foundation
#
# 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 cinder.api import extensions
from cinder.api.openstack import wsgi
from cinder.api import xmlutil
from cinder import volume
authorize = extensions.soft_extension_authorizer('volume',
'volume_tenant_attribute')
**** CubicPower OpenStack Study ****
class VolumeTenantAttributeController(wsgi.Controller):
**** CubicPower OpenStack Study ****
def __init__(self, *args, **kwargs):
super(VolumeTenantAttributeController, self).__init__(*args, **kwargs)
self.volume_api = volume.API()
**** CubicPower OpenStack Study ****
def _add_volume_tenant_attribute(self, context, req, resp_volume):
db_volume = req.cached_resource_by_id(resp_volume['id'])
key = "%s:tenant_id" % Volume_tenant_attribute.alias
resp_volume[key] = db_volume['project_id']
@wsgi.extends
**** CubicPower OpenStack Study ****
def show(self, req, resp_obj, id):
context = req.environ['cinder.context']
if authorize(context):
resp_obj.attach(xml=VolumeTenantAttributeTemplate())
volume = resp_obj.obj['volume']
self._add_volume_tenant_attribute(context, req, volume)
@wsgi.extends
**** CubicPower OpenStack Study ****
def detail(self, req, resp_obj):
context = req.environ['cinder.context']
if authorize(context):
resp_obj.attach(xml=VolumeListTenantAttributeTemplate())
for volume in list(resp_obj.obj['volumes']):
self._add_volume_tenant_attribute(context, req, volume)
**** CubicPower OpenStack Study ****
class Volume_tenant_attribute(extensions.ExtensionDescriptor):
"""Expose the internal project_id as an attribute of a volume."""
name = "VolumeTenantAttribute"
alias = "os-vol-tenant-attr"
namespace = ("http://docs.openstack.org/volume/ext/"
"volume_tenant_attribute/api/v1")
updated = "2011-11-03T00:00:00+00:00"
**** CubicPower OpenStack Study ****
def get_controller_extensions(self):
controller = VolumeTenantAttributeController()
extension = extensions.ControllerExtension(self, 'volumes', controller)
return [extension]
def make_volume(elem):
elem.set('{%s}tenant_id' % Volume_tenant_attribute.namespace,
'%s:tenant_id' % Volume_tenant_attribute.alias)
**** CubicPower OpenStack Study ****
def make_volume(elem):
elem.set('{%s}tenant_id' % Volume_tenant_attribute.namespace,
'%s:tenant_id' % Volume_tenant_attribute.alias)
**** CubicPower OpenStack Study ****
class VolumeTenantAttributeTemplate(xmlutil.TemplateBuilder):
**** CubicPower OpenStack Study ****
def construct(self):
root = xmlutil.TemplateElement('volume', selector='volume')
make_volume(root)
alias = Volume_tenant_attribute.alias
namespace = Volume_tenant_attribute.namespace
return xmlutil.SlaveTemplate(root, 1, nsmap={alias: namespace})
**** CubicPower OpenStack Study ****
class VolumeListTenantAttributeTemplate(xmlutil.TemplateBuilder):
**** CubicPower OpenStack Study ****
def construct(self):
root = xmlutil.TemplateElement('volumes')
elem = xmlutil.SubTemplateElement(root, 'volume', selector='volumes')
make_volume(elem)
alias = Volume_tenant_attribute.alias
namespace = Volume_tenant_attribute.namespace
return xmlutil.SlaveTemplate(root, 1, nsmap={alias: namespace})