def _get_backups(self, req, is_detail): """Returns a list of backups, transformed through view builder.""" context = req.environ['cinder.context'] filters = req.params.copy() marker, limit, offset = common.get_pagination_params(filters) sort_keys, sort_dirs = common.get_sort_params(filters) utils.remove_invalid_filter_options(context, filters, self._get_backup_filter_options()) if 'name' in filters: filters['display_name'] = filters['name'] del filters['name'] backups = self.backup_api.get_all(context, search_opts=filters, marker=marker, limit=limit, offset=offset, sort_keys=sort_keys, sort_dirs=sort_dirs, ) req.cache_db_backups(backups.objects) if is_detail: backups = self._view_builder.detail_list(req, backups.objects) else: backups = self._view_builder.summary_list(req, backups.objects) return backups
def _items(self, req): """Return a list of attachments, transformed through view builder.""" context = req.environ['cinder.context'] req_version = req.api_version_request # Pop out non search_opts and create local variables search_opts = req.GET.copy() sort_keys, sort_dirs = common.get_sort_params(search_opts) marker, limit, offset = common.get_pagination_params(search_opts) self._process_attachment_filtering(context=context, filters=search_opts, req_version=req_version) if search_opts.get('instance_id', None): search_opts['instance_uuid'] = search_opts.pop('instance_id', None) if context.is_admin and 'all_tenants' in search_opts: del search_opts['all_tenants'] return objects.VolumeAttachmentList.get_all( context, search_opts=search_opts, marker=marker, limit=limit, offset=offset, sort_keys=sort_keys, sort_direction=sort_dirs) else: return objects.VolumeAttachmentList.get_all_by_project( context, context.project_id, search_opts=search_opts, marker=marker, limit=limit, offset=offset, sort_keys=sort_keys, sort_direction=sort_dirs)
def index(self, req): """Returns a list of messages, transformed through view builder.""" context = req.environ['cinder.context'] api_version = req.api_version_request context.authorize(policy.GET_ALL_POLICY) filters = None marker = None limit = None offset = None sort_keys = None sort_dirs = None if api_version.matches(mv.MESSAGES_PAGINATION): filters = req.params.copy() marker, limit, offset = common.get_pagination_params(filters) sort_keys, sort_dirs = common.get_sort_params(filters) if api_version.matches(mv.RESOURCE_FILTER): support_like = (True if api_version.matches( mv.LIKE_FILTER) else False) common.reject_invalid_filters(context, filters, 'message', support_like) messages = self.message_api.get_all(context, filters=filters, marker=marker, limit=limit, offset=offset, sort_keys=sort_keys, sort_dirs=sort_dirs) for message in messages: self._build_user_message(message) messages = self._view_builder.index(req, messages) return messages
def test_no_params(self, mock_cfg): """Test no params.""" mock_cfg.osapi_max_limit = 100 req = webob.Request.blank('/') expected = (None, 100, 0) self.assertEqual(expected, common.get_pagination_params(req.GET.copy()))
def _get_group_types(self, req): """Helper function that returns a list of type dicts.""" params = req.params.copy() marker, limit, offset = common.get_pagination_params(params) sort_keys, sort_dirs = common.get_sort_params(params) filters = {} context = req.environ['cinder.context'] if context.is_admin: # Only admin has query access to all group types filters['is_public'] = self._parse_is_public( req.params.get('is_public', None)) else: filters['is_public'] = True utils.remove_invalid_filter_options(context, filters, self._get_grp_type_filter_options() ) limited_types = group_types.get_all_group_types(context, filters=filters, marker=marker, limit=limit, sort_keys=sort_keys, sort_dirs=sort_dirs, offset=offset, list_result=True) return limited_types
def _get_backups(self, req, is_detail): """Returns a list of backups, transformed through view builder.""" context = req.environ['cinder.context'] filters = req.params.copy() req_version = req.api_version_request marker, limit, offset = common.get_pagination_params(filters) sort_keys, sort_dirs = common.get_sort_params(filters) self._convert_sort_name(req_version, sort_keys) self._process_backup_filtering(context=context, filters=filters, req_version=req_version) if 'name' in filters: filters['display_name'] = filters.pop('name') backups = self.backup_api.get_all(context, search_opts=filters, marker=marker, limit=limit, offset=offset, sort_keys=sort_keys, sort_dirs=sort_dirs, ) req.cache_db_backups(backups.objects) if is_detail: backups = self._view_builder.detail_list(req, backups.objects) else: backups = self._view_builder.summary_list(req, backups.objects) return backups
def get_manageable_resources(req, is_detail, function_get_manageable, view_builder): context = req.environ["cinder.context"] params = req.params.copy() cluster_name, host = common.get_cluster_host(req, params, "3.17") marker, limit, offset = common.get_pagination_params(params) sort_keys, sort_dirs = common.get_sort_params(params, default_key="reference") # These parameters are generally validated at the DB layer, but in this # case sorting is not done by the DB valid_sort_keys = ("reference", "size") invalid_keys = [key for key in sort_keys if key not in valid_sort_keys] if invalid_keys: msg = _("Invalid sort keys passed: %s") % ", ".join(invalid_keys) raise exception.InvalidParameterValue(err=msg) valid_sort_dirs = ("asc", "desc") invalid_dirs = [d for d in sort_dirs if d not in valid_sort_dirs] if invalid_dirs: msg = _("Invalid sort dirs passed: %s") % ", ".join(invalid_dirs) raise exception.InvalidParameterValue(err=msg) resources = function_get_manageable( context, host, cluster_name, marker=marker, limit=limit, offset=offset, sort_keys=sort_keys, sort_dirs=sort_dirs ) resource_count = len(resources) if is_detail: resources = view_builder.detail_list(req, resources, resource_count) else: resources = view_builder.summary_list(req, resources, resource_count) return resources
def _items(self, req, is_detail=True): """Returns a list of snapshots, transformed through view builder.""" context = req.environ['cinder.context'] # Pop out non search_opts and create local variables search_opts = req.GET.copy() sort_keys, sort_dirs = common.get_sort_params(search_opts) marker, limit, offset = common.get_pagination_params(search_opts) # Filter out invalid options allowed_search_options = ('status', 'volume_id', 'name') utils.remove_invalid_filter_options(context, search_opts, allowed_search_options) # NOTE(thingee): v2 API allows name instead of display_name if 'name' in search_opts: search_opts['display_name'] = search_opts.pop('name') snapshots = self.volume_api.get_all_snapshots(context, search_opts=search_opts, marker=marker, limit=limit, sort_keys=sort_keys, sort_dirs=sort_dirs, offset=offset) req.cache_db_snapshots(snapshots.objects) if is_detail: snapshots = self._view_builder.detail_list(req, snapshots.objects) else: snapshots = self._view_builder.summary_list(req, snapshots.objects) return snapshots
def _get_resources(self, req, is_detail): self._ensure_min_version(req, '3.8') context = req.environ['cinder.context'] self._authorizer(context) params = req.params.copy() cluster_name, host = common.get_cluster_host(req, params, '3.17') marker, limit, offset = common.get_pagination_params(params) sort_keys, sort_dirs = common.get_sort_params(params, default_key='reference') # These parameters are generally validated at the DB layer, but in this # case sorting is not done by the DB invalid_keys = set(sort_keys).difference(self.VALID_SORT_KEYS) if invalid_keys: msg = _("Invalid sort keys passed: %s") % ', '.join(invalid_keys) raise exception.InvalidParameterValue(err=msg) invalid_dirs = set(sort_dirs).difference(self.VALID_SORT_DIRS) if invalid_dirs: msg = _("Invalid sort dirs passed: %s") % ', '.join(invalid_dirs) raise exception.InvalidParameterValue(err=msg) resources = self.get_manageable(context, host, cluster_name, marker=marker, limit=limit, offset=offset, sort_keys=sort_keys, sort_dirs=sort_dirs) view_builder = getattr(self._list_manageable_view, 'detail_list' if is_detail else 'summary_list') return view_builder(req, resources, len(resources))
def _items(self, req): """Return a list of attachments, transformed through view builder.""" context = req.environ['cinder.context'] # Pop out non search_opts and create local variables search_opts = req.GET.copy() sort_keys, sort_dirs = common.get_sort_params(search_opts) marker, limit, offset = common.get_pagination_params(search_opts) filters = search_opts allowed = self.allowed_filters if not allowed.issuperset(filters): invalid_keys = set(filters).difference(allowed) msg = _('Invalid filter keys: %s') % ', '.join(invalid_keys) raise exception.InvalidInput(reason=msg) # Filter out invalid options allowed_search_options = ('status', 'volume_id', 'instance_id') if search_opts.get('instance_id', None): search_opts['instance_uuid'] = search_opts.get('instance_id') utils.remove_invalid_filter_options(context, search_opts, allowed_search_options) return objects.VolumeAttachmentList.get_all(context, search_opts=search_opts, marker=marker, limit=limit, offset=offset, sort_keys=sort_keys, sort_direction=sort_dirs)
def test_valid_limit_and_marker(self): """Test valid limit and marker parameters.""" marker = '263abb28-1de6-412f-b00b-f0ee0c4333c2' req = webob.Request.blank('/?limit=20&marker=%s' % marker) expected = (marker, 20, 0) self.assertEqual(expected, common.get_pagination_params(req.GET.copy()))
def test_valid_marker(self): """Test valid marker param.""" marker = '263abb28-1de6-412f-b00b-f0ee0c4333c2' req = webob.Request.blank('/?marker=' + marker) expected = (marker, CONF.osapi_max_limit, 0) self.assertEqual(expected, common.get_pagination_params(req.GET.copy()))
def _get_groups(self, req, is_detail): """Returns a list of groups through view builder.""" context = req.environ['cinder.context'] filters = req.params.copy() marker, limit, offset = common.get_pagination_params(filters) sort_keys, sort_dirs = common.get_sort_params(filters) filters.pop('list_volume', None) groups = self.group_api.get_all( context, filters=filters, marker=marker, limit=limit, offset=offset, sort_keys=sort_keys, sort_dirs=sort_dirs) new_groups = [] for grp in groups: try: # Only show groups not migrated from CGs self._check_default_cgsnapshot_type(grp.group_type_id) new_groups.append(grp) except exc.HTTPBadRequest: # Skip migrated group pass if is_detail: groups = self._view_builder.detail_list( req, new_groups) else: groups = self._view_builder.summary_list( req, new_groups) return groups
def index(self, req): """Returns a list of messages, transformed through view builder.""" context = req.environ['cinder.context'] check_policy(context, 'get_all') filters = None marker = None limit = None offset = None sort_keys = None sort_dirs = None if (req.api_version_request.matches("3.5")): filters = req.params.copy() marker, limit, offset = common.get_pagination_params(filters) sort_keys, sort_dirs = common.get_sort_params(filters) messages = self.message_api.get_all(context, filters=filters, marker=marker, limit=limit, offset=offset, sort_keys=sort_keys, sort_dirs=sort_dirs) for message in messages: # Fetches message text based on event id passed to it. user_message = defined_messages.get_message_text( message['event_id']) message['user_message'] = user_message messages = self._view_builder.index(req, messages) return messages
def _get_volume_types(self, req): """Helper function that returns a list of type dicts.""" params = req.params.copy() marker, limit, offset = common.get_pagination_params(params) sort_keys, sort_dirs = common.get_sort_params(params) # NOTE(wanghao): Currently, we still only support to filter by # is_public. If we want to filter by more args, we should set params # to filters. filters = {} context = req.environ['cinder.context'] if context.is_admin: # Only admin has query access to all volume types filters['is_public'] = self._parse_is_public( req.params.get('is_public', None)) else: filters['is_public'] = True utils.remove_invalid_filter_options(context, filters, self._get_vol_type_filter_options() ) limited_types = volume_types.get_all_types(context, filters=filters, marker=marker, limit=limit, sort_keys=sort_keys, sort_dirs=sort_dirs, offset=offset, list_result=True) return limited_types
def _get_transfers(self, req, is_detail): """Returns a list of transfers, transformed through view builder.""" context = req.environ['cinder.context'] req_version = req.api_version_request params = req.params.copy() marker = limit = offset = None if req_version.matches(mv.SUPPORT_TRANSFER_PAGINATION): marker, limit, offset = common.get_pagination_params(params) sort_keys, sort_dirs = common.get_sort_params(params) else: # NOTE(yikun): After microversion SUPPORT_TRANSFER_PAGINATION, # transfers list api use the ['created_at'], ['asc'] # as default order, but we should keep the compatible in here. sort_keys, sort_dirs = ['created_at', 'id'], ['asc', 'asc'] filters = params LOG.debug('Listing volume transfers') transfers = self.transfer_api.get_all(context, marker=marker, limit=limit, sort_keys=sort_keys, sort_dirs=sort_dirs, filters=filters, offset=offset) transfer_count = len(transfers) limited_list = common.limited(transfers, req) if is_detail: transfers = self._view_builder.detail_list(req, limited_list, transfer_count) else: transfers = self._view_builder.summary_list(req, limited_list, transfer_count) return transfers
def index(self, req): """Returns a list of messages, transformed through view builder.""" context = req.environ['cinder.context'] check_policy(context, 'get_all') filters = None marker = None limit = None offset = None sort_keys = None sort_dirs = None if (req.api_version_request.matches("3.5")): filters = req.params.copy() marker, limit, offset = common.get_pagination_params(filters) sort_keys, sort_dirs = common.get_sort_params(filters) if req.api_version_request.matches(common.FILTERING_VERSION): common.reject_invalid_filters(context, filters, 'message') messages = self.message_api.get_all(context, filters=filters, marker=marker, limit=limit, offset=offset, sort_keys=sort_keys, sort_dirs=sort_dirs) for message in messages: # Fetches message text based on event id passed to it. user_message = defined_messages.get_message_text( message['event_id']) message['user_message'] = user_message messages = self._view_builder.index(req, messages) return messages
def _get_groups(self, req, is_detail): """Returns a list of groups through view builder.""" context = req.environ['cinder.context'] filters = req.params.copy() marker, limit, offset = common.get_pagination_params(filters) sort_keys, sort_dirs = common.get_sort_params(filters) groups = self.group_api.get_all( context, filters=filters, marker=marker, limit=limit, offset=offset, sort_keys=sort_keys, sort_dirs=sort_dirs) new_groups = [] for grp in groups: try: # Only show groups not migrated from CGs self._check_default_cgsnapshot_type(grp.group_type_id) new_groups.append(grp) except exc.HTTPBadRequest: # Skip migrated group pass if is_detail: groups = self._view_builder.detail_list( req, new_groups) else: groups = self._view_builder.summary_list( req, new_groups) return groups
def _items(self, req, is_detail=True): """Returns a list of snapshots, transformed through view builder.""" context = req.environ['cinder.context'] # Pop out non search_opts and create local variables search_opts = req.GET.copy() sort_keys, sort_dirs = common.get_sort_params(search_opts) marker, limit, offset = common.get_pagination_params(search_opts) # Filter out invalid options allowed_search_options = ('status', 'volume_id', 'name') utils.remove_invalid_filter_options(context, search_opts, allowed_search_options) # NOTE(thingee): v2 API allows name instead of display_name if 'name' in search_opts: search_opts['display_name'] = search_opts['name'] del search_opts['name'] snapshots = self.volume_api.get_all_snapshots(context, search_opts=search_opts, marker=marker, limit=limit, sort_keys=sort_keys, sort_dirs=sort_dirs, offset=offset) req.cache_db_snapshots(snapshots.objects) if is_detail: snapshots = self._view_builder.detail_list(req, snapshots.objects) else: snapshots = self._view_builder.summary_list(req, snapshots.objects) return snapshots
def index(self, req): """Returns a list of messages, transformed through view builder.""" context = req.environ['cinder.context'] api_version = req.api_version_request check_policy(context, 'get_all') filters = None marker = None limit = None offset = None sort_keys = None sort_dirs = None if api_version.matches("3.5"): filters = req.params.copy() marker, limit, offset = common.get_pagination_params(filters) sort_keys, sort_dirs = common.get_sort_params(filters) if api_version.matches(common.FILTERING_VERSION): support_like = (True if api_version.matches( common.LIKE_FILTER_VERSION) else False) common.reject_invalid_filters(context, filters, 'message', support_like) messages = self.message_api.get_all(context, filters=filters, marker=marker, limit=limit, offset=offset, sort_keys=sort_keys, sort_dirs=sort_dirs) for message in messages: self._build_user_message(message) messages = self._view_builder.index(req, messages) return messages
def _get_group_types(self, req): """Helper function that returns a list of type dicts.""" params = req.params.copy() marker, limit, offset = common.get_pagination_params(params) sort_keys, sort_dirs = common.get_sort_params(params) filters = {} context = req.environ['cinder.context'] if context.is_admin: # Only admin has query access to all group types filters['is_public'] = api_utils._parse_is_public( req.params.get('is_public', None)) else: filters['is_public'] = True api_utils.remove_invalid_filter_options( context, filters, self._get_grp_type_filter_options()) limited_types = group_types.get_all_group_types(context, filters=filters, marker=marker, limit=limit, sort_keys=sort_keys, sort_dirs=sort_dirs, offset=offset, list_result=True) return limited_types
def _get_consistencygroups(self, req, is_detail): """Returns a list of consistency groups through view builder.""" context = req.environ['cinder.context'] context.authorize(group_policy.GET_ALL_POLICY) filters = req.params.copy() # make another copy of filters, since it is being modified in # consistencygroup_api while getting consistencygroups marker, limit, offset = common.get_pagination_params(filters) sort_keys, sort_dirs = common.get_sort_params(filters) groups = self.group_api.get_all(context, filters=filters, marker=marker, limit=limit, offset=offset, sort_keys=sort_keys, sort_dirs=sort_dirs) if is_detail: groups = self._view_builder.detail_list(req, groups) else: groups = self._view_builder.summary_list(req, groups) return groups
def _get_backups(self, req, is_detail): """Returns a list of backups, transformed through view builder.""" context = req.environ['cinder.context'] filters = req.params.copy() marker, limit, offset = common.get_pagination_params(filters) sort_keys, sort_dirs = common.get_sort_params(filters) utils.remove_invalid_filter_options(context, filters, self._get_backup_filter_options()) if 'name' in filters: filters['display_name'] = filters['name'] del filters['name'] backups = self.backup_api.get_all( context, search_opts=filters, marker=marker, limit=limit, offset=offset, sort_keys=sort_keys, sort_dirs=sort_dirs, ) req.cache_db_backups(backups.objects) if is_detail: backups = self._view_builder.detail_list(req, backups.objects) else: backups = self._view_builder.summary_list(req, backups.objects) return backups
def _get_volume_types(self, req): """Helper function that returns a list of type dicts.""" params = req.params.copy() marker, limit, offset = common.get_pagination_params(params) sort_keys, sort_dirs = common.get_sort_params(params) # NOTE(wanghao): Currently, we still only support to filter by # is_public. If we want to filter by more args, we should set params # to filters. filters = {} context = req.environ['cinder.context'] if not context.is_admin and self._validate_policy(context): context = ctx.get_admin_context() if context.is_admin: # Only admin has query access to all volume types filters['is_public'] = self._parse_is_public( req.params.get('is_public', None)) else: filters['is_public'] = True utils.remove_invalid_filter_options( context, filters, self._get_vol_type_filter_options()) limited_types = volume_types.get_all_types(context, filters=filters, marker=marker, limit=limit, sort_keys=sort_keys, sort_dirs=sort_dirs, offset=offset, list_result=True) return limited_types
def index(self, req): """Returns a list of messages, transformed through view builder.""" context = req.environ['cinder.context'] api_version = req.api_version_request context.authorize(policy.GET_ALL_POLICY) filters = None marker = None limit = None offset = None sort_keys = None sort_dirs = None if api_version.matches(mv.MESSAGES_PAGINATION): filters = req.params.copy() marker, limit, offset = common.get_pagination_params(filters) sort_keys, sort_dirs = common.get_sort_params(filters) if api_version.matches(mv.RESOURCE_FILTER): support_like = (True if api_version.matches(mv.LIKE_FILTER) else False) common.reject_invalid_filters(context, filters, 'message', support_like) messages = self.message_api.get_all(context, filters=filters, marker=marker, limit=limit, offset=offset, sort_keys=sort_keys, sort_dirs=sort_dirs) for message in messages: self._build_user_message(message) messages = self._view_builder.index(req, messages) return messages
def _get_transfers(self, req, is_detail): """Returns a list of transfers, transformed through view builder.""" context = req.environ['cinder.context'] req_version = req.api_version_request params = req.params.copy() marker = limit = offset = None if req_version.matches(mv.SUPPORT_TRANSFER_PAGINATION): marker, limit, offset = common.get_pagination_params(params) sort_keys, sort_dirs = common.get_sort_params(params) else: # NOTE(yikun): After microversion SUPPORT_TRANSFER_PAGINATION, # transfers list api use the ['created_at'], ['asc'] # as default order, but we should keep the compatible in here. sort_keys, sort_dirs = ['created_at', 'id'], ['asc', 'asc'] filters = params LOG.debug('Listing volume transfers') transfers = self.transfer_api.get_all(context, marker=marker, limit=limit, sort_keys=sort_keys, sort_dirs=sort_dirs, filters=filters, offset=offset) transfer_count = len(transfers) limited_list = common.limited(transfers, req) if is_detail: transfers = self._view_builder.detail_list(req, limited_list, transfer_count) else: transfers = self._view_builder.summary_list( req, limited_list, transfer_count) return transfers
def test_valid_limit_and_marker(self): """ Test valid limit and marker parameters. """ marker = '263abb28-1de6-412f-b00b-f0ee0c4333c2' req = webob.Request.blank('/?limit=20&marker=%s' % marker) self.assertEqual(common.get_pagination_params(req), { 'marker': marker, 'limit': 20 })
def _get_volumes(self, req, is_detail): """Returns a list of volumes, transformed through view builder.""" context = req.environ['cinder.context'] req_version = req.api_version_request params = req.params.copy() marker, limit, offset = common.get_pagination_params(params) sort_keys, sort_dirs = common.get_sort_params(params) filters = params show_count = False if req_version.matches( mv.SUPPORT_COUNT_INFO) and 'with_count' in filters: show_count = utils.get_bool_param('with_count', filters) filters.pop('with_count') self._process_volume_filtering(context=context, filters=filters, req_version=req_version) # NOTE(thingee): v2 API allows name instead of display_name if 'name' in sort_keys: sort_keys[sort_keys.index('name')] = 'display_name' if 'name' in filters: filters['display_name'] = filters.pop('name') self._handle_time_comparison_filters(filters) strict = req.api_version_request.matches(mv.VOLUME_LIST_BOOTABLE, None) self.volume_api.check_volume_filters(filters, strict) volumes = self.volume_api.get_all(context, marker, limit, sort_keys=sort_keys, sort_dirs=sort_dirs, filters=filters.copy(), viewable_admin_meta=True, offset=offset) total_count = None if show_count: total_count = self.volume_api.calculate_resource_count( context, 'volume', filters) for volume in volumes: api_utils.add_visible_admin_metadata(volume) req.cache_db_volumes(volumes.objects) if is_detail: volumes = self._view_builder.detail_list(req, volumes, total_count) else: volumes = self._view_builder.summary_list(req, volumes, total_count) return volumes
def _get_volumes(self, req, is_detail): """Returns a list of volumes, transformed through view builder.""" context = req.environ['cinder.context'] req_version = req.api_version_request params = req.params.copy() marker, limit, offset = common.get_pagination_params(params) sort_keys, sort_dirs = common.get_sort_params(params) filters = params show_count = False if req_version.matches( mv.SUPPORT_COUNT_INFO) and 'with_count' in filters: show_count = utils.get_bool_param('with_count', filters) filters.pop('with_count') self._process_volume_filtering(context=context, filters=filters, req_version=req_version) # NOTE(thingee): v2 API allows name instead of display_name if 'name' in sort_keys: sort_keys[sort_keys.index('name')] = 'display_name' if 'name' in filters: filters['display_name'] = filters.pop('name') strict = req.api_version_request.matches( mv.VOLUME_LIST_BOOTABLE, None) self.volume_api.check_volume_filters(filters, strict) volumes = self.volume_api.get_all(context, marker, limit, sort_keys=sort_keys, sort_dirs=sort_dirs, filters=filters.copy(), viewable_admin_meta=True, offset=offset) total_count = None if show_count: total_count = self.volume_api.calculate_resource_count( context, 'volume', filters) for volume in volumes: utils.add_visible_admin_metadata(volume) req.cache_db_volumes(volumes.objects) if is_detail: volumes = self._view_builder.detail_list( req, volumes, total_count) else: volumes = self._view_builder.summary_list( req, volumes, total_count) return volumes
def _items(self, req, is_detail=True): """Returns a list of snapshots, transformed through view builder.""" context = req.environ['cinder.context'] req_version = req.api_version_request # Pop out non search_opts and create local variables search_opts = req.GET.copy() sort_keys, sort_dirs = common.get_sort_params(search_opts) marker, limit, offset = common.get_pagination_params(search_opts) req_version = req.api_version_request show_count = False if req_version.matches( mv.SUPPORT_COUNT_INFO) and 'with_count' in search_opts: show_count = utils.get_bool_param('with_count', search_opts) search_opts.pop('with_count') # process filters self._process_snapshot_filtering(context=context, filters=search_opts, req_version=req_version) # process snapshot filters to appropriate formats if required self._format_snapshot_filter_options(search_opts) req_version = req.api_version_request if req_version.matches(mv.SNAPSHOT_SORT, None) and 'name' in sort_keys: sort_keys[sort_keys.index('name')] = 'display_name' # NOTE(thingee): v3 API allows name instead of display_name if 'name' in search_opts: search_opts['display_name'] = search_opts.pop('name') snapshots = self.volume_api.get_all_snapshots( context, search_opts=search_opts.copy(), marker=marker, limit=limit, sort_keys=sort_keys, sort_dirs=sort_dirs, offset=offset) total_count = None if show_count: total_count = self.volume_api.calculate_resource_count( context, 'snapshot', search_opts) req.cache_db_snapshots(snapshots.objects) if is_detail: snapshots = self._view_builder.detail_list(req, snapshots.objects, total_count) else: snapshots = self._view_builder.summary_list(req, snapshots.objects, total_count) return snapshots
def _items(self, req, is_detail=True): """Returns a list of snapshots, transformed through view builder.""" context = req.environ['cinder.context'] req_version = req.api_version_request # Pop out non search_opts and create local variables search_opts = req.GET.copy() sort_keys, sort_dirs = common.get_sort_params(search_opts) marker, limit, offset = common.get_pagination_params(search_opts) req_version = req.api_version_request show_count = False if req_version.matches( mv.SUPPORT_COUNT_INFO) and 'with_count' in search_opts: show_count = utils.get_bool_param('with_count', search_opts) search_opts.pop('with_count') # process filters self._process_snapshot_filtering(context=context, filters=search_opts, req_version=req_version) # process snapshot filters to appropriate formats if required self._format_snapshot_filter_options(search_opts) req_version = req.api_version_request if req_version.matches(mv.SNAPSHOT_SORT, None) and 'name' in sort_keys: sort_keys[sort_keys.index('name')] = 'display_name' # NOTE(thingee): v3 API allows name instead of display_name if 'name' in search_opts: search_opts['display_name'] = search_opts.pop('name') snapshots = self.volume_api.get_all_snapshots( context, search_opts=search_opts.copy(), marker=marker, limit=limit, sort_keys=sort_keys, sort_dirs=sort_dirs, offset=offset) total_count = None if show_count: total_count = self.volume_api.calculate_resource_count( context, 'snapshot', search_opts) req.cache_db_snapshots(snapshots.objects) if is_detail: snapshots = self._view_builder.detail_list(req, snapshots.objects, total_count) else: snapshots = self._view_builder.summary_list( req, snapshots.objects, total_count) return snapshots
def _get_volumes(self, req, is_detail): """Returns a list of volumes, transformed through view builder.""" context = req.environ['cinder.context'] req_version = req.api_version_request params = req.params.copy() marker, limit, offset = common.get_pagination_params(params) sort_keys, sort_dirs = common.get_sort_params(params) filters = params if req_version.matches(None, "3.3"): filters.pop('glance_metadata', None) if req_version.matches(None, "3.9"): filters.pop('group_id', None) utils.remove_invalid_filter_options(context, filters, self._get_volume_filter_options()) # NOTE(thingee): v2 API allows name instead of display_name if 'name' in sort_keys: sort_keys[sort_keys.index('name')] = 'display_name' if 'name' in filters: filters['display_name'] = filters.pop('name') if 'group_id' in filters: filters['consistencygroup_id'] = filters.pop('group_id') strict = req.api_version_request.matches("3.2", None) self.volume_api.check_volume_filters(filters, strict) volumes = self.volume_api.get_all(context, marker, limit, sort_keys=sort_keys, sort_dirs=sort_dirs, filters=filters, viewable_admin_meta=True, offset=offset) for volume in volumes: utils.add_visible_admin_metadata(volume) req.cache_db_volumes(volumes.objects) if is_detail: volumes = self._view_builder.detail_list(req, volumes) else: volumes = self._view_builder.summary_list(req, volumes) return volumes
def _get_volume_types(self, req): """Helper function that returns a list of type dicts.""" params = req.params.copy() marker, limit, offset = common.get_pagination_params(params) sort_keys, sort_dirs = common.get_sort_params(params) filters = params context = req.environ['cinder.context'] req_version = req.api_version_request if req_version.matches(mv.SUPPORT_VOLUME_TYPE_FILTER): self._process_volume_type_filtering(context=context, filters=filters, req_version=req_version) else: api_utils.remove_invalid_filter_options( context, filters, self._get_vol_type_filter_options()) if context.is_admin: # Only admin has query access to all volume types filters['is_public'] = api_utils._parse_is_public( req.params.get('is_public', None)) else: filters['is_public'] = True if 'extra_specs' in filters: try: filters['extra_specs'] = ast.literal_eval( filters['extra_specs']) except (ValueError, SyntaxError): LOG.debug( 'Could not evaluate "extra_specs" %s, assuming ' 'dictionary string.', filters['extra_specs']) # Do not allow sensitive extra specs to be used in a filter if # the context only allows access to user visible extra specs. # Removing the filter would yield inaccurate results, so an # empty result is returned because as far as an unauthorized # user goes, the list of volume-types meeting their filtering # criteria is empty. if not context.authorize(extra_specs_policy.READ_SENSITIVE_POLICY, fatal=False): for k in filters['extra_specs'].keys(): if k not in extra_specs_policy.USER_VISIBLE_EXTRA_SPECS: return [] limited_types = volume_types.get_all_types(context, filters=filters, marker=marker, limit=limit, sort_keys=sort_keys, sort_dirs=sort_dirs, offset=offset, list_result=True) return limited_types
def _get_volumes(self, req, is_detail): """Returns a list of volumes, transformed through view builder.""" context = req.environ['cinder.context'] params = req.params.copy() marker, limit, offset = common.get_pagination_params(params) sort_keys, sort_dirs = common.get_sort_params(params) filters = params utils.remove_invalid_filter_options(context, filters, self._get_volume_filter_options()) # NOTE(thingee): v2 API allows name instead of display_name if 'name' in sort_keys: sort_keys[sort_keys.index('name')] = 'display_name' if 'name' in filters: filters['display_name'] = filters['name'] del filters['name'] for k, v in filters.items(): try: filters[k] = ast.literal_eval(v) except (ValueError, SyntaxError): LOG.debug('Could not evaluate value %s, assuming string', v) self.volume_api.check_volume_filters(filters) volumes = self.volume_api.get_all(context, marker, limit, sort_keys=sort_keys, sort_dirs=sort_dirs, filters=filters, viewable_admin_meta=True, offset=offset) volumes = [dict(vol) for vol in volumes] for volume in volumes: utils.add_visible_admin_metadata(volume) req.cache_db_volumes(volumes) if is_detail: volumes = self._view_builder.detail_list(req, volumes) else: volumes = self._view_builder.summary_list(req, volumes) return volumes
def _get_volumes(self, req, is_detail): """Returns a list of volumes, transformed through view builder.""" context = req.environ['cinder.context'] params = req.params.copy() marker, limit, offset = common.get_pagination_params(params) sort_keys, sort_dirs = common.get_sort_params(params) filters = params utils.remove_invalid_filter_options(context, filters, self._get_volume_filter_options()) # NOTE(thingee): v2 API allows name instead of display_name if 'name' in sort_keys: sort_keys[sort_keys.index('name')] = 'display_name' if 'name' in filters: filters['display_name'] = filters['name'] del filters['name'] for k, v in filters.items(): try: filters[k] = ast.literal_eval(v) except (ValueError, SyntaxError): LOG.debug('Could not evaluate value %s, assuming string', v) volumes = self.volume_api.get_all(context, marker, limit, sort_keys=sort_keys, sort_dirs=sort_dirs, filters=filters, viewable_admin_meta=True, offset=offset) volumes = [dict(vol) for vol in volumes] for volume in volumes: utils.add_visible_admin_metadata(volume) req.cache_db_volumes(volumes) if is_detail: volumes = self._view_builder.detail_list(req, volumes) else: volumes = self._view_builder.summary_list(req, volumes) return volumes
def _get_group_snapshots(self, req, is_detail): """Returns a list of group_snapshots through view builder.""" context = req.environ['cinder.context'] req_version = req.api_version_request filters = marker = limit = offset = sort_keys = sort_dirs = None if req_version.matches("3.29"): filters = req.params.copy() marker, limit, offset = common.get_pagination_params(filters) sort_keys, sort_dirs = common.get_sort_params(filters) if req_version.matches(common.FILTERING_VERSION): support_like = (True if req_version.matches( common.LIKE_FILTER_VERSION) else False) common.reject_invalid_filters(context, filters, 'group_snapshot', support_like) group_snapshots = self.group_snapshot_api.get_all_group_snapshots( context, filters=filters, marker=marker, limit=limit, offset=offset, sort_keys=sort_keys, sort_dirs=sort_dirs) if is_detail: group_snapshots = self._view_builder.detail_list( req, group_snapshots) else: group_snapshots = self._view_builder.summary_list( req, group_snapshots) new_group_snapshots = [] for grp_snap in group_snapshots['group_snapshots']: try: # Only show group snapshots not migrated from CG snapshots self._check_default_cgsnapshot_type(grp_snap['group_type_id']) if not is_detail: grp_snap.pop('group_type_id', None) new_group_snapshots.append(grp_snap) except exc.HTTPBadRequest: # Skip migrated group snapshot pass group_snapshots['group_snapshots'] = new_group_snapshots return group_snapshots
def _get_consistencygroups(self, req, is_detail): """Returns a list of consistency groups through view builder.""" context = req.environ['cinder.context'] filters = req.params.copy() marker, limit, offset = common.get_pagination_params(filters) sort_keys, sort_dirs = common.get_sort_params(filters) consistencygroups = self.consistencygroup_api.get_all( context, filters=filters, marker=marker, limit=limit, offset=offset, sort_keys=sort_keys, sort_dirs=sort_dirs) if is_detail: consistencygroups = self._view_builder.detail_list( req, consistencygroups) else: consistencygroups = self._view_builder.summary_list( req, consistencygroups) return consistencygroups
def _get_volumes(self, req, is_detail): """Returns a list of volumes, transformed through view builder.""" context = req.environ['cinder.context'] req_version = req.api_version_request params = req.params.copy() marker, limit, offset = common.get_pagination_params(params) sort_keys, sort_dirs = common.get_sort_params(params) filters = params self._process_volume_filtering(context=context, filters=filters, req_version=req_version) # NOTE(thingee): v2 API allows name instead of display_name if 'name' in sort_keys: sort_keys[sort_keys.index('name')] = 'display_name' if 'name' in filters: filters['display_name'] = filters.pop('name') strict = req.api_version_request.matches(mv.VOLUME_LIST_BOOTABLE, None) self.volume_api.check_volume_filters(filters, strict) volumes = self.volume_api.get_all(context, marker, limit, sort_keys=sort_keys, sort_dirs=sort_dirs, filters=filters, viewable_admin_meta=True, offset=offset) for volume in volumes: utils.add_visible_admin_metadata(volume) req.cache_db_volumes(volumes.objects) if is_detail: volumes = self._view_builder.detail_list(req, volumes) else: volumes = self._view_builder.summary_list(req, volumes) return volumes
def _get_volumes(self, req, is_detail): """Returns a list of volumes, transformed through view builder.""" context = req.environ['cinder.context'] params = req.params.copy() marker, limit, offset = common.get_pagination_params(params) sort_keys, sort_dirs = common.get_sort_params(params) filters = params # NOTE(wanghao): Always removing glance_metadata since we support it # only in API version >= 3.4. filters.pop('glance_metadata', None) utils.remove_invalid_filter_options(context, filters, self._get_volume_filter_options()) # NOTE(thingee): v2 API allows name instead of display_name if 'name' in sort_keys: sort_keys[sort_keys.index('name')] = 'display_name' if 'name' in filters: filters['display_name'] = filters['name'] del filters['name'] self.volume_api.check_volume_filters(filters) volumes = self.volume_api.get_all(context, marker, limit, sort_keys=sort_keys, sort_dirs=sort_dirs, filters=filters, viewable_admin_meta=True, offset=offset) for volume in volumes: utils.add_visible_admin_metadata(volume) req.cache_db_volumes(volumes.objects) if is_detail: volumes = self._view_builder.detail_list(req, volumes) else: volumes = self._view_builder.summary_list(req, volumes) return volumes
def _get_backups(self, req, is_detail): """Returns a list of backups, transformed through view builder.""" context = req.environ['cinder.context'] filters = req.params.copy() req_version = req.api_version_request marker, limit, offset = common.get_pagination_params(filters) sort_keys, sort_dirs = common.get_sort_params(filters) show_count = False if req_version.matches( mv.SUPPORT_COUNT_INFO) and 'with_count' in filters: show_count = utils.get_bool_param('with_count', filters) filters.pop('with_count') self._convert_sort_name(req_version, sort_keys) self._process_backup_filtering(context=context, filters=filters, req_version=req_version) if 'name' in filters: filters['display_name'] = filters.pop('name') backups = self.backup_api.get_all( context, search_opts=filters.copy(), marker=marker, limit=limit, offset=offset, sort_keys=sort_keys, sort_dirs=sort_dirs, ) total_count = None if show_count: total_count = self.volume_api.calculate_resource_count( context, 'backup', filters) req.cache_db_backups(backups.objects) if is_detail: backups = self._view_builder.detail_list(req, backups.objects, total_count) else: backups = self._view_builder.summary_list(req, backups.objects, total_count) return backups
def get_manageable_resources(req, is_detail, function_get_manageable, view_builder): context = req.environ['cinder.context'] params = req.params.copy() cluster_name, host = common.get_cluster_host(req, params, '3.17') marker, limit, offset = common.get_pagination_params(params) sort_keys, sort_dirs = common.get_sort_params(params, default_key='reference') # These parameters are generally validated at the DB layer, but in this # case sorting is not done by the DB valid_sort_keys = ('reference', 'size') invalid_keys = [key for key in sort_keys if key not in valid_sort_keys] if invalid_keys: msg = _("Invalid sort keys passed: %s") % ', '.join(invalid_keys) raise exception.InvalidParameterValue(err=msg) valid_sort_dirs = ('asc', 'desc') invalid_dirs = [d for d in sort_dirs if d not in valid_sort_dirs] if invalid_dirs: msg = _("Invalid sort dirs passed: %s") % ', '.join(invalid_dirs) raise exception.InvalidParameterValue(err=msg) try: resources = function_get_manageable(context, host, cluster_name, marker=marker, limit=limit, offset=offset, sort_keys=sort_keys, sort_dirs=sort_dirs) except messaging.RemoteError as err: if err.exc_type == "InvalidInput": raise exception.InvalidInput(err.value) raise resource_count = len(resources) if is_detail: resources = view_builder.detail_list(req, resources, resource_count) else: resources = view_builder.summary_list(req, resources, resource_count) return resources
def index(self, req): """Returns the list of qos_specs.""" context = req.environ['cinder.context'] authorize(context) params = req.params.copy() marker, limit, offset = common.get_pagination_params(params) sort_keys, sort_dirs = common.get_sort_params(params) filters = params allowed_search_options = ('id', 'name', 'consumer') utils.remove_invalid_filter_options(context, filters, allowed_search_options) specs = qos_specs.get_all_specs(context, filters=filters, marker=marker, limit=limit, offset=offset, sort_keys=sort_keys, sort_dirs=sort_dirs) return self._view_builder.summary_list(req, specs)
def _get_volumes(self, req, is_detail): """Returns a list of volumes, transformed through view builder.""" context = req.environ["cinder.context"] params = req.params.copy() marker, limit, offset = common.get_pagination_params(params) sort_keys, sort_dirs = common.get_sort_params(params) filters = params utils.remove_invalid_filter_options(context, filters, self._get_volume_filter_options()) # NOTE(thingee): v2 API allows name instead of display_name if "name" in sort_keys: sort_keys[sort_keys.index("name")] = "display_name" if "name" in filters: filters["display_name"] = filters["name"] del filters["name"] self.volume_api.check_volume_filters(filters) volumes = self.volume_api.get_all( context, marker, limit, sort_keys=sort_keys, sort_dirs=sort_dirs, filters=filters, viewable_admin_meta=True, offset=offset, ) for volume in volumes: utils.add_visible_admin_metadata(volume) req.cache_db_volumes(volumes.objects) if is_detail: volumes = self._view_builder.detail_list(req, volumes) else: volumes = self._view_builder.summary_list(req, volumes) return volumes
def _get_consistencygroups(self, req, is_detail): """Returns a list of consistency groups through view builder.""" context = req.environ['cinder.context'] filters = req.params.copy() # make another copy of filters, since it is being modified in # consistencygroup_api while getting consistencygroups marker, limit, offset = common.get_pagination_params(filters) sort_keys, sort_dirs = common.get_sort_params(filters) groups = self.group_api.get_all( context, filters=filters, marker=marker, limit=limit, offset=offset, sort_keys=sort_keys, sort_dirs=sort_dirs) if is_detail: groups = self._view_builder.detail_list(req, groups) else: groups = self._view_builder.summary_list(req, groups) return groups
def _get_group_snapshots(self, req, is_detail): """Returns a list of group_snapshots through view builder.""" context = req.environ['cinder.context'] req_version = req.api_version_request filters = marker = limit = offset = sort_keys = sort_dirs = None if req_version.matches(mv.GROUP_SNAPSHOT_PAGINATION): filters = req.params.copy() marker, limit, offset = common.get_pagination_params(filters) sort_keys, sort_dirs = common.get_sort_params(filters) if req_version.matches(mv.RESOURCE_FILTER): support_like = (True if req_version.matches( mv.LIKE_FILTER) else False) common.reject_invalid_filters(context, filters, 'group_snapshot', support_like) group_snapshots = self.group_snapshot_api.get_all_group_snapshots( context, filters=filters, marker=marker, limit=limit, offset=offset, sort_keys=sort_keys, sort_dirs=sort_dirs) if is_detail: group_snapshots = self._view_builder.detail_list(req, group_snapshots) else: group_snapshots = self._view_builder.summary_list(req, group_snapshots) new_group_snapshots = [] for grp_snap in group_snapshots['group_snapshots']: try: # Only show group snapshots not migrated from CG snapshots self._check_default_cgsnapshot_type(grp_snap['group_type_id']) if not is_detail: grp_snap.pop('group_type_id', None) new_group_snapshots.append(grp_snap) except exc.HTTPBadRequest: # Skip migrated group snapshot pass group_snapshots['group_snapshots'] = new_group_snapshots return group_snapshots
def _get_backups(self, req, is_detail): """Returns a list of backups, transformed through view builder.""" context = req.environ['cinder.context'] filters = req.params.copy() req_version = req.api_version_request marker, limit, offset = common.get_pagination_params(filters) sort_keys, sort_dirs = common.get_sort_params(filters) show_count = False if req_version.matches( mv.SUPPORT_COUNT_INFO) and 'with_count' in filters: show_count = utils.get_bool_param('with_count', filters) filters.pop('with_count') self._convert_sort_name(req_version, sort_keys) self._process_backup_filtering(context=context, filters=filters, req_version=req_version) if 'name' in filters: filters['display_name'] = filters.pop('name') backups = self.backup_api.get_all(context, search_opts=filters.copy(), marker=marker, limit=limit, offset=offset, sort_keys=sort_keys, sort_dirs=sort_dirs, ) total_count = None if show_count: total_count = self.volume_api.calculate_resource_count( context, 'backup', filters) req.cache_db_backups(backups.objects) if is_detail: backups = self._view_builder.detail_list(req, backups.objects, total_count) else: backups = self._view_builder.summary_list(req, backups.objects, total_count) return backups
def get_manageable_resources(req, is_detail, function_get_manageable, view_builder): context = req.environ['cinder.context'] params = req.params.copy() cluster_name, host = common.get_cluster_host( req, params, mv.MANAGE_EXISTING_CLUSTER) marker, limit, offset = common.get_pagination_params(params) sort_keys, sort_dirs = common.get_sort_params(params, default_key='reference') # These parameters are generally validated at the DB layer, but in this # case sorting is not done by the DB valid_sort_keys = ('reference', 'size') invalid_keys = [key for key in sort_keys if key not in valid_sort_keys] if invalid_keys: msg = _("Invalid sort keys passed: %s") % ', '.join(invalid_keys) raise exception.InvalidParameterValue(err=msg) valid_sort_dirs = ('asc', 'desc') invalid_dirs = [d for d in sort_dirs if d not in valid_sort_dirs] if invalid_dirs: msg = _("Invalid sort dirs passed: %s") % ', '.join(invalid_dirs) raise exception.InvalidParameterValue(err=msg) try: resources = function_get_manageable(context, host, cluster_name, marker=marker, limit=limit, offset=offset, sort_keys=sort_keys, sort_dirs=sort_dirs) except messaging.RemoteError as err: if err.exc_type == "InvalidInput": raise exception.InvalidInput(err.value) raise resource_count = len(resources) if is_detail: resources = view_builder.detail_list(req, resources, resource_count) else: resources = view_builder.summary_list(req, resources, resource_count) return resources
def get_manageable_resources(req, is_detail, function_get_manageable, view_builder): context = req.environ['cinder.context'] params = req.params.copy() host = params.get('host') if host is None: raise exception.InvalidHost( reason=_("Host must be specified in query parameters")) marker, limit, offset = common.get_pagination_params(params) sort_keys, sort_dirs = common.get_sort_params(params, default_key='reference') # These parameters are generally validated at the DB layer, but in this # case sorting is not done by the DB valid_sort_keys = ('reference', 'size') invalid_keys = [key for key in sort_keys if key not in valid_sort_keys] if invalid_keys: msg = _("Invalid sort keys passed: %s") % ', '.join(invalid_keys) raise exception.InvalidParameterValue(err=msg) valid_sort_dirs = ('asc', 'desc') invalid_dirs = [d for d in sort_dirs if d not in valid_sort_dirs] if invalid_dirs: msg = _("Invalid sort dirs passed: %s") % ', '.join(invalid_dirs) raise exception.InvalidParameterValue(err=msg) resources = function_get_manageable(context, host, marker=marker, limit=limit, offset=offset, sort_keys=sort_keys, sort_dirs=sort_dirs) resource_count = len(resources) if is_detail: resources = view_builder.detail_list(req, resources, resource_count) else: resources = view_builder.summary_list(req, resources, resource_count) return resources
def _get_groups(self, req, is_detail): """Returns a list of groups through view builder.""" context = req.environ['cinder.context'] filters = req.params.copy() api_version = req.api_version_request marker, limit, offset = common.get_pagination_params(filters) sort_keys, sort_dirs = common.get_sort_params(filters) filters.pop('list_volume', None) if api_version.matches(common.FILTERING_VERSION): support_like = (True if api_version.matches( common.LIKE_FILTER_VERSION) else False) common.reject_invalid_filters(context, filters, 'group', support_like) groups = self.group_api.get_all( context, filters=filters, marker=marker, limit=limit, offset=offset, sort_keys=sort_keys, sort_dirs=sort_dirs) new_groups = [] for grp in groups: try: # Only show groups not migrated from CGs self._check_default_cgsnapshot_type(grp.group_type_id) new_groups.append(grp) except exc.HTTPBadRequest: # Skip migrated group pass if is_detail: groups = self._view_builder.detail_list( req, new_groups) else: groups = self._view_builder.summary_list( req, new_groups) return groups