"Fossies" - the Fresh Open Source Software Archive

Member "keystone-17.0.0/keystone/api/os_ep_filter.py" (13 May 2020, 13359 Bytes) of package /linux/misc/openstack/keystone-17.0.0.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 "os_ep_filter.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 16.0.1_vs_17.0.0.

    1 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    2 #    not use this file except in compliance with the License. You may obtain
    3 #    a copy of the License at
    4 #
    5 #         http://www.apache.org/licenses/LICENSE-2.0
    6 #
    7 #    Unless required by applicable law or agreed to in writing, software
    8 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    9 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   10 #    License for the specific language governing permissions and limitations
   11 #    under the License.
   12 
   13 # This file handles all flask-restful resources for /OS-EP-FILTER
   14 
   15 import flask_restful
   16 import http.client
   17 
   18 from keystone.api._shared import json_home_relations
   19 from keystone.api import endpoints as _endpoints_api
   20 from keystone.catalog import schema
   21 from keystone.common import json_home
   22 from keystone.common import provider_api
   23 from keystone.common import rbac_enforcer
   24 from keystone.common import validation
   25 from keystone import exception
   26 from keystone.i18n import _
   27 from keystone.server import flask as ks_flask
   28 
   29 
   30 ENFORCER = rbac_enforcer.RBACEnforcer
   31 PROVIDERS = provider_api.ProviderAPIs
   32 
   33 _build_resource_relation = json_home_relations.os_ep_filter_resource_rel_func
   34 _build_parameter_relation = json_home_relations.os_ep_filter_parameter_rel_func
   35 
   36 _ENDPOINT_GROUP_PARAMETER_RELATION = _build_parameter_relation(
   37     parameter_name='endpoint_group_id')
   38 
   39 
   40 # NOTE(morgan): This is shared from keystone.api.endpoint, this is a special
   41 # case where cross-api code is used. This pattern should not be replicated.
   42 _filter_endpoint = _endpoints_api._filter_endpoint
   43 
   44 
   45 class EndpointGroupsResource(ks_flask.ResourceBase):
   46     collection_key = 'endpoint_groups'
   47     member_key = 'endpoint_group'
   48     api_prefix = '/OS-EP-FILTER'
   49     json_home_resource_rel_func = _build_resource_relation
   50     json_home_parameter_rel_func = _build_parameter_relation
   51 
   52     @staticmethod
   53     def _require_valid_filter(endpoint_group):
   54         valid_filter_keys = ['service_id', 'region_id', 'interface']
   55 
   56         filters = endpoint_group.get('filters')
   57         for key in filters.keys():
   58             if key not in valid_filter_keys:
   59                 raise exception.ValidationError(
   60                     attribute=' or '.join(valid_filter_keys),
   61                     target='endpoint_group')
   62 
   63     def _get_endpoint_group(self, endpoint_group_id):
   64         ENFORCER.enforce_call(action='identity:get_endpoint_group')
   65         return self.wrap_member(
   66             PROVIDERS.catalog_api.get_endpoint_group(endpoint_group_id))
   67 
   68     def _list_endpoint_groups(self):
   69         filters = ('name')
   70         ENFORCER.enforce_call(action='identity:list_endpoint_groups',
   71                               filters=filters)
   72         hints = self.build_driver_hints(filters)
   73         refs = PROVIDERS.catalog_api.list_endpoint_groups(hints)
   74         return self.wrap_collection(refs, hints=hints)
   75 
   76     def get(self, endpoint_group_id=None):
   77         if endpoint_group_id is not None:
   78             return self._get_endpoint_group(endpoint_group_id)
   79         return self._list_endpoint_groups()
   80 
   81     def post(self):
   82         ENFORCER.enforce_call(action='identity:create_endpoint_group')
   83         ep_group = self.request_body_json.get('endpoint_group', {})
   84         validation.lazy_validate(schema.endpoint_group_create, ep_group)
   85         if not ep_group.get('filters'):
   86             # TODO(morgan): Make this not require substitution. Substitution is
   87             # done here due to String Freeze in the Rocky release.
   88             msg = _('%s field is required and cannot be empty') % 'filters'
   89             raise exception.ValidationError(message=msg)
   90         self._require_valid_filter(ep_group)
   91         ep_group = self._assign_unique_id(ep_group)
   92         return self.wrap_member(PROVIDERS.catalog_api.create_endpoint_group(
   93             ep_group['id'], ep_group)), http.client.CREATED
   94 
   95     def patch(self, endpoint_group_id):
   96         ENFORCER.enforce_call(action='identity:update_endpoint_group')
   97         ep_group = self.request_body_json.get('endpoint_group', {})
   98         validation.lazy_validate(schema.endpoint_group_update, ep_group)
   99         if 'filters' in ep_group:
  100             self._require_valid_filter(ep_group)
  101         self._require_matching_id(ep_group)
  102         return self.wrap_member(PROVIDERS.catalog_api.update_endpoint_group(
  103             endpoint_group_id, ep_group))
  104 
  105     def delete(self, endpoint_group_id):
  106         ENFORCER.enforce_call(action='identity:delete_endpoint_group')
  107         return (PROVIDERS.catalog_api.delete_endpoint_group(endpoint_group_id),
  108                 http.client.NO_CONTENT)
  109 
  110 
  111 class EPFilterEndpointProjectsResource(flask_restful.Resource):
  112     def get(self, endpoint_id):
  113         """"Return a list of projects associated with the endpoint."""
  114         ENFORCER.enforce_call(action='identity:list_projects_for_endpoint')
  115         PROVIDERS.catalog_api.get_endpoint(endpoint_id)
  116         refs = PROVIDERS.catalog_api.list_projects_for_endpoint(endpoint_id)
  117         projects = [PROVIDERS.resource_api.get_project(ref['project_id'])
  118                     for ref in refs]
  119         return ks_flask.ResourceBase.wrap_collection(
  120             projects, collection_name='projects')
  121 
  122 
  123 class EPFilterProjectsEndpointsResource(flask_restful.Resource):
  124     def get(self, project_id, endpoint_id):
  125         ENFORCER.enforce_call(action='identity:check_endpoint_in_project')
  126         PROVIDERS.catalog_api.get_endpoint(endpoint_id)
  127         PROVIDERS.resource_api.get_project(project_id)
  128         PROVIDERS.catalog_api.check_endpoint_in_project(
  129             endpoint_id, project_id)
  130         return None, http.client.NO_CONTENT
  131 
  132     def put(self, project_id, endpoint_id):
  133         ENFORCER.enforce_call(action='identity:add_endpoint_to_project')
  134         PROVIDERS.catalog_api.get_endpoint(endpoint_id)
  135         PROVIDERS.resource_api.get_project(project_id)
  136         PROVIDERS.catalog_api.add_endpoint_to_project(endpoint_id, project_id)
  137         return None, http.client.NO_CONTENT
  138 
  139     def delete(self, project_id, endpoint_id):
  140         ENFORCER.enforce_call(action='identity:remove_endpoint_from_project')
  141         return (PROVIDERS.catalog_api.remove_endpoint_from_project(
  142             endpoint_id, project_id), http.client.NO_CONTENT)
  143 
  144 
  145 class EPFilterProjectEndpointsListResource(flask_restful.Resource):
  146     def get(self, project_id):
  147         ENFORCER.enforce_call(action='identity:list_endpoints_for_project')
  148         PROVIDERS.resource_api.get_project(project_id)
  149         filtered_endpoints = PROVIDERS.catalog_api.list_endpoints_for_project(
  150             project_id)
  151 
  152         return ks_flask.ResourceBase.wrap_collection(
  153             [_filter_endpoint(v) for v in filtered_endpoints.values()],
  154             collection_name='endpoints')
  155 
  156 
  157 class EndpointFilterProjectEndpointGroupsListResource(flask_restful.Resource):
  158     def get(self, project_id):
  159         ENFORCER.enforce_call(
  160             action='identity:list_endpoint_groups_for_project')
  161         return EndpointGroupsResource.wrap_collection(
  162             PROVIDERS.catalog_api.get_endpoint_groups_for_project(project_id))
  163 
  164 
  165 class EndpointFilterEPGroupsProjects(flask_restful.Resource):
  166     def get(self, endpoint_group_id):
  167         ENFORCER.enforce_call(
  168             action='identity:list_projects_associated_with_endpoint_group')
  169         endpoint_group_refs = (PROVIDERS.catalog_api.
  170                                list_projects_associated_with_endpoint_group(
  171                                    endpoint_group_id))
  172         projects = []
  173         for endpoint_group_ref in endpoint_group_refs:
  174             project = PROVIDERS.resource_api.get_project(
  175                 endpoint_group_ref['project_id'])
  176             if project:
  177                 projects.append(project)
  178 
  179         return ks_flask.ResourceBase.wrap_collection(
  180             projects, collection_name='projects')
  181 
  182 
  183 class EndpointFilterEPGroupsEndpoints(flask_restful.Resource):
  184     def get(self, endpoint_group_id):
  185         ENFORCER.enforce_call(
  186             action='identity:list_endpoints_associated_with_endpoint_group')
  187         filtered_endpoints = (PROVIDERS.catalog_api.
  188                               get_endpoints_filtered_by_endpoint_group(
  189                                   endpoint_group_id))
  190         return ks_flask.ResourceBase.wrap_collection(
  191             [_filter_endpoint(e) for e in filtered_endpoints],
  192             collection_name='endpoints')
  193 
  194 
  195 class EPFilterGroupsProjectsResource(ks_flask.ResourceBase):
  196     collection_key = 'project_endpoint_groups'
  197     member_key = 'project_endpoint_group'
  198 
  199     @classmethod
  200     def _add_self_referential_link(cls, ref, collection_name=None):
  201         url = ('/OS-EP-FILTER/endpoint_groups/%(endpoint_group_id)s'
  202                '/projects/%(project_id)s' % {
  203                    'endpoint_group_id': ref['endpoint_group_id'],
  204                    'project_id': ref['project_id']})
  205         ref.setdefault('links', {})
  206         ref['links']['self'] = url
  207 
  208     def get(self, endpoint_group_id, project_id):
  209         ENFORCER.enforce_call(action='identity:get_endpoint_group_in_project')
  210         PROVIDERS.resource_api.get_project(project_id)
  211         PROVIDERS.catalog_api.get_endpoint_group(endpoint_group_id)
  212         ref = PROVIDERS.catalog_api.get_endpoint_group_in_project(
  213             endpoint_group_id, project_id)
  214         return self.wrap_member(ref)
  215 
  216     def put(self, endpoint_group_id, project_id):
  217         ENFORCER.enforce_call(action='identity:add_endpoint_group_to_project')
  218         PROVIDERS.resource_api.get_project(project_id)
  219         PROVIDERS.catalog_api.get_endpoint_group(endpoint_group_id)
  220         PROVIDERS.catalog_api.add_endpoint_group_to_project(
  221             endpoint_group_id, project_id)
  222         return None, http.client.NO_CONTENT
  223 
  224     def delete(self, endpoint_group_id, project_id):
  225         ENFORCER.enforce_call(
  226             action='identity:remove_endpoint_group_from_project')
  227         PROVIDERS.resource_api.get_project(project_id)
  228         PROVIDERS.catalog_api.get_endpoint_group(endpoint_group_id)
  229         PROVIDERS.catalog_api.remove_endpoint_group_from_project(
  230             endpoint_group_id, project_id)
  231         return None, http.client.NO_CONTENT
  232 
  233 
  234 class EPFilterAPI(ks_flask.APIBase):
  235     _name = 'OS-EP-FILTER'
  236     _import_name = __name__
  237     _api_url_prefix = '/OS-EP-FILTER'
  238     resources = [EndpointGroupsResource]
  239     resource_mapping = [
  240         ks_flask.construct_resource_map(
  241             resource=EPFilterEndpointProjectsResource,
  242             url='/endpoints/<string:endpoint_id>/projects',
  243             resource_kwargs={},
  244             rel='endpoint_projects',
  245             resource_relation_func=_build_resource_relation,
  246             path_vars={
  247                 'endpoint_id': json_home.Parameters.ENDPOINT_ID
  248             }),
  249         ks_flask.construct_resource_map(
  250             resource=EPFilterProjectsEndpointsResource,
  251             url='/projects/<string:project_id>/endpoints/<string:endpoint_id>',
  252             resource_kwargs={},
  253             rel='project_endpoint',
  254             resource_relation_func=_build_resource_relation,
  255             path_vars={
  256                 'endpoint_id': json_home.Parameters.ENDPOINT_ID,
  257                 'project_id': json_home.Parameters.PROJECT_ID}),
  258         ks_flask.construct_resource_map(
  259             resource=EPFilterProjectEndpointsListResource,
  260             url='/projects/<string:project_id>/endpoints',
  261             resource_kwargs={},
  262             rel='project_endpoints',
  263             resource_relation_func=_build_resource_relation,
  264             path_vars={'project_id': json_home.Parameters.PROJECT_ID}),
  265         ks_flask.construct_resource_map(
  266             resource=EndpointFilterProjectEndpointGroupsListResource,
  267             url='/projects/<string:project_id>/endpoint_groups',
  268             resource_kwargs={},
  269             rel='project_endpoint_groups',
  270             resource_relation_func=_build_resource_relation,
  271             path_vars={'project_id': json_home.Parameters.PROJECT_ID}),
  272         ks_flask.construct_resource_map(
  273             resource=EndpointFilterEPGroupsEndpoints,
  274             url='/endpoint_groups/<string:endpoint_group_id>/endpoints',
  275             resource_kwargs={},
  276             rel='endpoints_in_endpoint_group',
  277             resource_relation_func=_build_resource_relation,
  278             path_vars={
  279                 'endpoint_group_id': _ENDPOINT_GROUP_PARAMETER_RELATION}),
  280         ks_flask.construct_resource_map(
  281             resource=EndpointFilterEPGroupsProjects,
  282             url='/endpoint_groups/<string:endpoint_group_id>/projects',
  283             resource_kwargs={},
  284             rel='projects_associated_with_endpoint_group',
  285             resource_relation_func=_build_resource_relation,
  286             path_vars={
  287                 'endpoint_group_id': _ENDPOINT_GROUP_PARAMETER_RELATION}),
  288         ks_flask.construct_resource_map(
  289             resource=EPFilterGroupsProjectsResource,
  290             url=('/endpoint_groups/<string:endpoint_group_id>/projects/'
  291                  '<string:project_id>'),
  292             resource_kwargs={},
  293             rel='endpoint_group_to_project_association',
  294             resource_relation_func=_build_resource_relation,
  295             path_vars={'project_id': json_home.Parameters.PROJECT_ID,
  296                        'endpoint_group_id': _ENDPOINT_GROUP_PARAMETER_RELATION
  297                        }),
  298     ]
  299 
  300 
  301 APIs = (EPFilterAPI,)