Пример #1
0
    def _get_security_services(self, req, is_detail):
        """Returns a transformed list of security services.

        The list gets transformed through view builder.
        """
        context = req.environ['manila.context']

        search_opts = {}
        search_opts.update(req.GET)

        # NOTE(vponomaryov): remove 'status' from search opts
        # since it was removed from security service model.
        search_opts.pop('status', None)
        if 'share_network_id' in search_opts:
            share_nw = db.share_network_get(context,
                                            search_opts['share_network_id'])
            security_services = share_nw['security_services']
            del search_opts['share_network_id']
        else:
            if context.is_admin and utils.is_all_tenants(search_opts):
                policy.check_policy(context, RESOURCE_NAME,
                                    'get_all_security_services')
                security_services = db.security_service_get_all(context)
            else:
                security_services = db.security_service_get_all_by_project(
                    context, context.project_id)
        search_opts.pop('all_tenants', None)
        common.remove_invalid_options(
            context, search_opts, self._get_security_services_search_options())
        if search_opts:
            results = []
            not_found = object()
            for ss in security_services:
                if all(
                        ss.get(opt, not_found) == value
                        for opt, value in search_opts.items()):
                    results.append(ss)
            security_services = results

        limited_list = common.limited(security_services, req)

        if is_detail:
            security_services = self._view_builder.detail_list(
                req, limited_list)
            for ss in security_services['security_services']:
                share_networks = db.share_network_get_all_by_security_service(
                    context, ss['id'])
                ss['share_networks'] = [sn['id'] for sn in share_networks]
        else:
            security_services = self._view_builder.summary_list(
                req, limited_list)
        return security_services
Пример #2
0
    def _get_share_networks(self, req, is_detail=True):
        """Returns a list of share networks."""
        context = req.environ['manila.context']
        search_opts = {}
        search_opts.update(req.GET)

        if 'security_service_id' in search_opts:
            networks = db_api.share_network_get_all_by_security_service(
                context, search_opts['security_service_id'])
        elif context.is_admin and 'project_id' in search_opts:
            networks = db_api.share_network_get_all_by_project(
                context, search_opts['project_id'])
        elif context.is_admin and utils.is_all_tenants(search_opts):
            networks = db_api.share_network_get_all(context)
        else:
            networks = db_api.share_network_get_all_by_project(
                context, context.project_id)

        date_parsing_error_msg = '''%s is not in yyyy-mm-dd format.'''
        if 'created_since' in search_opts:
            try:
                created_since = timeutils.parse_strtime(
                    search_opts['created_since'], fmt="%Y-%m-%d")
            except ValueError:
                msg = date_parsing_error_msg % search_opts['created_since']
                raise exc.HTTPBadRequest(explanation=msg)
            networks = [
                network for network in networks
                if network['created_at'] >= created_since
            ]
        if 'created_before' in search_opts:
            try:
                created_before = timeutils.parse_strtime(
                    search_opts['created_before'], fmt="%Y-%m-%d")
            except ValueError:
                msg = date_parsing_error_msg % search_opts['created_before']
                raise exc.HTTPBadRequest(explanation=msg)
            networks = [
                network for network in networks
                if network['created_at'] <= created_before
            ]
        opts_to_remove = [
            'all_tenants', 'created_since', 'created_before', 'limit',
            'offset', 'security_service_id', 'project_id'
        ]
        for opt in opts_to_remove:
            search_opts.pop(opt, None)
        if search_opts:
            for key, value in search_opts.items():
                if key in ['ip_version', 'segmentation_id']:
                    value = int(value)
                if (req.api_version_request >=
                        api_version.APIVersionRequest("2.36")):
                    networks = [
                        network for network in networks
                        if network.get(key) == value
                        or self._subnet_has_search_opt(key, value, network) or
                        (value in network.get(key.rstrip('~')) if key.endswith(
                            '~') and network.get(key.rstrip('~')) else ())
                    ]
                else:
                    networks = [
                        network for network in networks
                        if network.get(key) == value
                        or self._subnet_has_search_opt(
                            key, value, network, exact_value=True)
                    ]

        limited_list = common.limited(networks, req)
        return self._view_builder.build_share_networks(req, limited_list,
                                                       is_detail)
Пример #3
0
 def test_is_all_tenants_missing(self):
     self.assertFalse(utils.is_all_tenants({}))
Пример #4
0
 def test_is_all_tenants_false(self, value):
     search_opts = {'all_tenants': value}
     self.assertFalse(utils.is_all_tenants(search_opts))
     self.assertIn('all_tenants', search_opts)
Пример #5
0
    def _get_share_networks(self, req, is_detail=True):
        """Returns a list of share networks."""
        context = req.environ['manila.context']
        search_opts = {}
        search_opts.update(req.GET)
        filters = {}

        # if not context.is_admin, will ignore project_id and all_tenants here,
        # in database will auto add context.project_id to search_opts.
        if context.is_admin:
            if 'project_id' in search_opts:
                # if specified project_id, will not use all_tenants
                filters['project_id'] = search_opts['project_id']
            elif not utils.is_all_tenants(search_opts):
                # if not specified project_id and all_tenants, will get
                # share networks in admin project.
                filters['project_id'] = context.project_id

        date_parsing_error_msg = '''%s is not in yyyy-mm-dd format.'''
        for time_comparison_filter in ['created_since', 'created_before']:
            if time_comparison_filter in search_opts:
                time_str = search_opts.get(time_comparison_filter)
                try:
                    parsed_time = timeutils.parse_strtime(time_str,
                                                          fmt="%Y-%m-%d")
                except ValueError:
                    msg = date_parsing_error_msg % time_str
                    raise exc.HTTPBadRequest(explanation=msg)

                filters[time_comparison_filter] = parsed_time

        if 'security_service_id' in search_opts:
            filters['security_service_id'] = search_opts.get(
                'security_service_id')

        networks = db_api.share_network_get_all_by_filter(context,
                                                          filters=filters)

        opts_to_remove = [
            'all_tenants', 'created_since', 'created_before', 'limit',
            'offset', 'security_service_id', 'project_id'
        ]
        for opt in opts_to_remove:
            search_opts.pop(opt, None)
        if search_opts:
            for key, value in search_opts.items():
                if key in ['ip_version', 'segmentation_id']:
                    value = int(value)
                if (req.api_version_request >=
                        api_version.APIVersionRequest("2.36")):
                    networks = [
                        network for network in networks
                        if network.get(key) == value
                        or self._subnet_has_search_opt(key, value, network) or
                        (value in network.get(key.rstrip('~')) if key.endswith(
                            '~') and network.get(key.rstrip('~')) else ())
                    ]
                else:
                    networks = [
                        network for network in networks
                        if network.get(key) == value
                        or self._subnet_has_search_opt(
                            key, value, network, exact_value=True)
                    ]

        limited_list = common.limited(networks, req)
        return self._view_builder.build_share_networks(req, limited_list,
                                                       is_detail)
Пример #6
0
    def _get_share_networks(self, req, is_detail=True):
        """Returns a list of share networks."""
        context = req.environ['manila.context']
        search_opts = {}
        search_opts.update(req.GET)

        if 'security_service_id' in search_opts:
            networks = db_api.share_network_get_all_by_security_service(
                context, search_opts['security_service_id'])
        elif context.is_admin and 'project_id' in search_opts:
            networks = db_api.share_network_get_all_by_project(
                context, search_opts['project_id'])
        elif context.is_admin and utils.is_all_tenants(search_opts):
            networks = db_api.share_network_get_all(context)
        else:
            networks = db_api.share_network_get_all_by_project(
                context,
                context.project_id)

        date_parsing_error_msg = '''%s is not in yyyy-mm-dd format.'''
        if 'created_since' in search_opts:
            try:
                created_since = timeutils.parse_strtime(
                    search_opts['created_since'],
                    fmt="%Y-%m-%d")
            except ValueError:
                msg = date_parsing_error_msg % search_opts['created_since']
                raise exc.HTTPBadRequest(explanation=msg)
            networks = [network for network in networks
                        if network['created_at'] >= created_since]
        if 'created_before' in search_opts:
            try:
                created_before = timeutils.parse_strtime(
                    search_opts['created_before'],
                    fmt="%Y-%m-%d")
            except ValueError:
                msg = date_parsing_error_msg % search_opts['created_before']
                raise exc.HTTPBadRequest(explanation=msg)
            networks = [network for network in networks
                        if network['created_at'] <= created_before]
        opts_to_remove = [
            'all_tenants',
            'created_since',
            'created_before',
            'limit',
            'offset',
            'security_service_id',
            'project_id'
        ]
        for opt in opts_to_remove:
            search_opts.pop(opt, None)
        if search_opts:
            for key, value in search_opts.items():
                if key in ['ip_version', 'segmentation_id']:
                    value = int(value)
                if (req.api_version_request >=
                        api_version.APIVersionRequest("2.36")):
                    networks = [network for network in networks
                                if network.get(key) == value or
                                (value in network.get(key.rstrip('~'))
                                 if key.endswith('~') and
                                 network.get(key.rstrip('~')) else ())]
                else:
                    networks = [network for network in networks
                                if network.get(key) == value]

        limited_list = common.limited(networks, req)
        return self._view_builder.build_share_networks(
            req, limited_list, is_detail)