from webob import exc
from cinder.api.openstack import wsgi
-from cinder.api.views import types as views_types
+from cinder.api.v2.views import types as views_types
from cinder.api import xmlutil
from cinder import exception
from cinder.i18n import _
elem.set('id')
elem.set('name')
elem.set('description')
+ elem.set('qos_specs_id')
extra_specs = xmlutil.make_flat_dict('extra_specs', selector='extra_specs')
elem.append(extra_specs)
--- /dev/null
+# Copyright 2012 Red Hat, Inc.
+# Copyright 2015 Intel Corporation
+# All Rights Reserved.
+#
+# 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 common
+
+
+class ViewBuilder(common.ViewBuilder):
+
+ def show(self, request, volume_type, brief=False):
+ """Trim away extraneous volume type attributes."""
+ context = request.environ['cinder.context']
+ trimmed = dict(id=volume_type.get('id'),
+ name=volume_type.get('name'),
+ is_public=volume_type.get('is_public'),
+ extra_specs=volume_type.get('extra_specs'),
+ description=volume_type.get('description'))
+ if context.is_admin:
+ trimmed['qos_specs_id'] = volume_type.get('qos_specs_id')
+ return trimmed if brief else dict(volume_type=trimmed)
+
+ def index(self, request, volume_types):
+ """Index over trimmed volume types."""
+ volume_types_list = [self.show(request, volume_type, True)
+ for volume_type in volume_types]
+ return dict(volume_types=volume_types_list)
import webob
from cinder.api.v2 import types
-from cinder.api.views import types as views_types
+from cinder.api.v2.views import types as views_types
from cinder import exception
from cinder import test
from cinder.tests.unit.api import fakes
raw_volume_type = dict(
name='new_type',
description='new_type_desc',
+ qos_specs_id='new_id',
is_public=True,
deleted=False,
created_at=now,
)
self.assertDictMatch(output['volume_type'], expected_volume_type)
+ def test_view_builder_show_admin(self):
+ view_builder = views_types.ViewBuilder()
+
+ now = timeutils.utcnow().isoformat()
+ raw_volume_type = dict(
+ name='new_type',
+ description='new_type_desc',
+ qos_specs_id='new_id',
+ is_public=True,
+ deleted=False,
+ created_at=now,
+ updated_at=now,
+ extra_specs={},
+ deleted_at=None,
+ id=42,
+ )
+
+ request = fakes.HTTPRequest.blank("/v2", use_admin_context=True)
+ output = view_builder.show(request, raw_volume_type)
+
+ self.assertIn('volume_type', output)
+ expected_volume_type = dict(
+ name='new_type',
+ description='new_type_desc',
+ qos_specs_id='new_id',
+ is_public=True,
+ extra_specs={},
+ id=42,
+ )
+ self.assertDictMatch(expected_volume_type, output['volume_type'])
+
def test_view_builder_list(self):
view_builder = views_types.ViewBuilder()
dict(
name='new_type',
description='new_type_desc',
+ qos_specs_id='new_id',
is_public=True,
deleted=False,
created_at=now,
self.assertDictMatch(output['volume_types'][i],
expected_volume_type)
+ def test_view_builder_list_admin(self):
+ view_builder = views_types.ViewBuilder()
+
+ now = timeutils.utcnow().isoformat()
+ raw_volume_types = []
+ for i in range(0, 10):
+ raw_volume_types.append(
+ dict(
+ name='new_type',
+ description='new_type_desc',
+ qos_specs_id='new_id',
+ is_public=True,
+ deleted=False,
+ created_at=now,
+ updated_at=now,
+ extra_specs={},
+ deleted_at=None,
+ id=42 + i
+ )
+ )
+
+ request = fakes.HTTPRequest.blank("/v2", use_admin_context=True)
+ output = view_builder.index(request, raw_volume_types)
+
+ self.assertIn('volume_types', output)
+ for i in range(0, 10):
+ expected_volume_type = dict(
+ name='new_type',
+ description='new_type_desc',
+ qos_specs_id='new_id',
+ is_public=True,
+ extra_specs={},
+ id=42 + i
+ )
+ self.assertDictMatch(expected_volume_type,
+ output['volume_types'][i])
+
class VolumeTypesSerializerTest(test.TestCase):
def _verify_volume_type(self, vtype, tree):