"Fossies" - the Fresh Open Source Software Archive

Member "cinder-13.0.7/cinder/api/v3/snapshots.py" (4 Oct 2019, 4979 Bytes) of package /linux/misc/openstack/cinder-13.0.7.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "snapshots.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 14.0.2_vs_15.0.0.

    1 # Copyright 2016 EMC Corporation
    2 # All Rights Reserved.
    3 #
    4 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    5 #    not use this file except in compliance with the License. You may obtain
    6 #    a copy of the License at
    7 #
    8 #         http://www.apache.org/licenses/LICENSE-2.0
    9 #
   10 #    Unless required by applicable law or agreed to in writing, software
   11 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   12 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   13 #    License for the specific language governing permissions and limitations
   14 #    under the License.
   15 
   16 """The volumes snapshots V3 API."""
   17 
   18 import ast
   19 
   20 from oslo_log import log as logging
   21 
   22 from cinder.api import common
   23 from cinder.api import microversions as mv
   24 from cinder.api.openstack import wsgi
   25 from cinder.api.v2 import snapshots as snapshots_v2
   26 from cinder.api.v3.views import snapshots as snapshot_views
   27 from cinder import utils
   28 
   29 LOG = logging.getLogger(__name__)
   30 
   31 
   32 class SnapshotsController(snapshots_v2.SnapshotsController):
   33     """The Snapshots API controller for the OpenStack API."""
   34 
   35     _view_builder_class = snapshot_views.ViewBuilder
   36 
   37     def _get_snapshot_filter_options(self):
   38         """returns tuple of valid filter options"""
   39 
   40         return 'status', 'volume_id', 'name', 'metadata'
   41 
   42     def _format_snapshot_filter_options(self, search_opts):
   43         """Convert valid filter options to correct expected format"""
   44 
   45         # Get the dict object out of queried metadata
   46         # convert metadata query value from string to dict
   47         if 'metadata' in search_opts.keys():
   48             try:
   49                 search_opts['metadata'] = ast.literal_eval(
   50                     search_opts['metadata'])
   51             except (ValueError, SyntaxError):
   52                 LOG.debug('Could not evaluate value %s, assuming string',
   53                           search_opts['metadata'])
   54 
   55     @common.process_general_filtering('snapshot')
   56     def _process_snapshot_filtering(self, context=None, filters=None,
   57                                     req_version=None):
   58         """Formats allowed filters"""
   59 
   60         # if the max version is less than SNAPSHOT_LIST_METADATA_FILTER
   61         # metadata based filtering is not supported
   62         if req_version.matches(
   63                 None, mv.get_prior_version(mv.SNAPSHOT_LIST_METADATA_FILTER)):
   64             filters.pop('metadata', None)
   65 
   66         # Filter out invalid options
   67         allowed_search_options = self._get_snapshot_filter_options()
   68 
   69         utils.remove_invalid_filter_options(context, filters,
   70                                             allowed_search_options)
   71 
   72     def _items(self, req, is_detail=True):
   73         """Returns a list of snapshots, transformed through view builder."""
   74         context = req.environ['cinder.context']
   75         # Pop out non search_opts and create local variables
   76         search_opts = req.GET.copy()
   77         sort_keys, sort_dirs = common.get_sort_params(search_opts)
   78         marker, limit, offset = common.get_pagination_params(search_opts)
   79 
   80         req_version = req.api_version_request
   81         show_count = False
   82         if req_version.matches(
   83                 mv.SUPPORT_COUNT_INFO) and 'with_count' in search_opts:
   84             show_count = utils.get_bool_param('with_count', search_opts)
   85             search_opts.pop('with_count')
   86 
   87         # process filters
   88         self._process_snapshot_filtering(context=context,
   89                                          filters=search_opts,
   90                                          req_version=req_version)
   91         # process snapshot filters to appropriate formats if required
   92         self._format_snapshot_filter_options(search_opts)
   93 
   94         req_version = req.api_version_request
   95         if req_version.matches(mv.SNAPSHOT_SORT, None) and 'name' in sort_keys:
   96             sort_keys[sort_keys.index('name')] = 'display_name'
   97 
   98         # NOTE(thingee): v3 API allows name instead of display_name
   99         if 'name' in search_opts:
  100             search_opts['display_name'] = search_opts.pop('name')
  101 
  102         snapshots = self.volume_api.get_all_snapshots(
  103             context,
  104             search_opts=search_opts.copy(),
  105             marker=marker,
  106             limit=limit,
  107             sort_keys=sort_keys,
  108             sort_dirs=sort_dirs,
  109             offset=offset)
  110         total_count = None
  111         if show_count:
  112             total_count = self.volume_api.calculate_resource_count(
  113                 context, 'snapshot', search_opts)
  114 
  115         req.cache_db_snapshots(snapshots.objects)
  116 
  117         if is_detail:
  118             snapshots = self._view_builder.detail_list(req, snapshots.objects,
  119                                                        total_count)
  120         else:
  121             snapshots = self._view_builder.summary_list(req, snapshots.objects,
  122                                                         total_count)
  123         return snapshots
  124 
  125 
  126 def create_resource(ext_mgr):
  127     return wsgi.Resource(SnapshotsController(ext_mgr))