# Copyright 2014 IBM Corp.
#
# 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 keystone.common import controller
from keystone.common import dependency
from keystone import notifications


@dependency.requires('policy_api', 'catalog_api', 'endpoint_policy_api')
class EndpointPolicyV3Controller(controller.V3Controller):
    collection_name = 'endpoints'
    member_name = 'endpoint'

    def __init__(self):
        super(EndpointPolicyV3Controller, self).__init__()
        notifications.register_event_callback(
            'deleted', 'endpoint', self._on_endpoint_delete)
        notifications.register_event_callback(
            'deleted', 'service', self._on_service_delete)
        notifications.register_event_callback(
            'deleted', 'region', self._on_region_delete)
        notifications.register_event_callback(
            'deleted', 'policy', self._on_policy_delete)

    def _on_endpoint_delete(self, service, resource_type, operation, payload):
        self.endpoint_policy_api.delete_association_by_endpoint(
            payload['resource_info'])

    def _on_service_delete(self, service, resource_type, operation, payload):
        self.endpoint_policy_api.delete_association_by_service(
            payload['resource_info'])

    def _on_region_delete(self, service, resource_type, operation, payload):
        self.endpoint_policy_api.delete_association_by_region(
            payload['resource_info'])

    def _on_policy_delete(self, service, resource_type, operation, payload):
        self.endpoint_policy_api.delete_association_by_policy(
            payload['resource_info'])

    @controller.protected()
    def create_policy_association_for_endpoint(self, context,
                                               policy_id, endpoint_id):
        """Create an association between a policy and an endpoint."""
        self.policy_api.get_policy(policy_id)
        self.catalog_api.get_endpoint(endpoint_id)
        self.endpoint_policy_api.create_policy_association(
            policy_id, endpoint_id=endpoint_id)

    @controller.protected()
    def check_policy_association_for_endpoint(self, context,
                                              policy_id, endpoint_id):
        """Check an association between a policy and an endpoint."""
        self.policy_api.get_policy(policy_id)
        self.catalog_api.get_endpoint(endpoint_id)
        self.endpoint_policy_api.check_policy_association(
            policy_id, endpoint_id=endpoint_id)

    @controller.protected()
    def delete_policy_association_for_endpoint(self, context,
                                               policy_id, endpoint_id):
        """Delete an association between a policy and an endpoint."""
        self.policy_api.get_policy(policy_id)
        self.catalog_api.get_endpoint(endpoint_id)
        self.endpoint_policy_api.delete_policy_association(
            policy_id, endpoint_id=endpoint_id)

    @controller.protected()
    def create_policy_association_for_service(self, context,
                                              policy_id, service_id):
        """Create an association between a policy and a service."""
        self.policy_api.get_policy(policy_id)
        self.catalog_api.get_service(service_id)
        self.endpoint_policy_api.create_policy_association(
            policy_id, service_id=service_id)

    @controller.protected()
    def check_policy_association_for_service(self, context,
                                             policy_id, service_id):
        """Check an association between a policy and a service."""
        self.policy_api.get_policy(policy_id)
        self.catalog_api.get_service(service_id)
        self.endpoint_policy_api.check_policy_association(
            policy_id, service_id=service_id)

    @controller.protected()
    def delete_policy_association_for_service(self, context,
                                              policy_id, service_id):
        """Delete an association between a policy and a service."""
        self.policy_api.get_policy(policy_id)
        self.catalog_api.get_service(service_id)
        self.endpoint_policy_api.delete_policy_association(
            policy_id, service_id=service_id)

    @controller.protected()
    def create_policy_association_for_region_and_service(
            self, context, policy_id, service_id, region_id):
        """Create an association between a policy and region+service."""
        self.policy_api.get_policy(policy_id)
        self.catalog_api.get_service(service_id)
        self.catalog_api.get_region(region_id)
        self.endpoint_policy_api.create_policy_association(
            policy_id, service_id=service_id, region_id=region_id)

    @controller.protected()
    def check_policy_association_for_region_and_service(
            self, context, policy_id, service_id, region_id):
        """Check an association between a policy and region+service."""
        self.policy_api.get_policy(policy_id)
        self.catalog_api.get_service(service_id)
        self.catalog_api.get_region(region_id)
        self.endpoint_policy_api.check_policy_association(
            policy_id, service_id=service_id, region_id=region_id)

    @controller.protected()
    def delete_policy_association_for_region_and_service(
            self, context, policy_id, service_id, region_id):
        """Delete an association between a policy and region+service."""
        self.policy_api.get_policy(policy_id)
        self.catalog_api.get_service(service_id)
        self.catalog_api.get_region(region_id)
        self.endpoint_policy_api.delete_policy_association(
            policy_id, service_id=service_id, region_id=region_id)

    @controller.protected()
    def get_policy_for_endpoint(self, context, endpoint_id):
        """Get the effective policy for an endpoint."""
        self.catalog_api.get_endpoint(endpoint_id)
        ref = self.endpoint_policy_api.get_policy_for_endpoint(endpoint_id)
        # NOTE(henry-nash): since the collection and member for this class is
        # set to endpoints, we have to handle wrapping this policy entity
        # ourselves.
        self._add_self_referential_link(context, ref)
        return {'policy': ref}

    # NOTE(henry-nash): As in the catalog controller, we must ensure that the
    # legacy_endpoint_id does not escape.

    @classmethod
    def filter_endpoint(cls, ref):
        if 'legacy_endpoint_id' in ref:
            ref.pop('legacy_endpoint_id')
        return ref

    @classmethod
    def wrap_member(cls, context, ref):
        ref = cls.filter_endpoint(ref)
        return super(EndpointPolicyV3Controller, cls).wrap_member(context, ref)

    @controller.protected()
    def list_endpoints_for_policy(self, context, policy_id):
        """List endpoints with the effective association to a policy."""
        self.policy_api.get_policy(policy_id)
        refs = self.endpoint_policy_api.list_endpoints_for_policy(policy_id)
        return EndpointPolicyV3Controller.wrap_collection(context, refs)

[((20, 1, 20, 72), 'keystone.common.dependency.requires', 'dependency.requires', ({(20, 21, 20, 33): '"""policy_api"""', (20, 35, 20, 48): '"""catalog_api"""', (20, 50, 20, 71): '"""endpoint_policy_api"""'}, {}), "('policy_api', 'catalog_api', 'endpoint_policy_api')", False, 'from keystone.common import dependency\n'), ((52, 5, 52, 27), 'keystone.common.controller.protected', 'controller.protected', ({}, {}), '()', False, 'from keystone.common import controller\n'), ((61, 5, 61, 27), 'keystone.common.controller.protected', 'controller.protected', ({}, {}), '()', False, 'from keystone.common import controller\n'), ((70, 5, 70, 27), 'keystone.common.controller.protected', 'controller.protected', ({}, {}), '()', False, 'from keystone.common import controller\n'), ((79, 5, 79, 27), 'keystone.common.controller.protected', 'controller.protected', ({}, {}), '()', False, 'from keystone.common import controller\n'), ((88, 5, 88, 27), 'keystone.common.controller.protected', 'controller.protected', ({}, {}), '()', False, 'from keystone.common import controller\n'), ((97, 5, 97, 27), 'keystone.common.controller.protected', 'controller.protected', ({}, {}), '()', False, 'from keystone.common import controller\n'), ((106, 5, 106, 27), 'keystone.common.controller.protected', 'controller.protected', ({}, {}), '()', False, 'from keystone.common import controller\n'), ((116, 5, 116, 27), 'keystone.common.controller.protected', 'controller.protected', ({}, {}), '()', False, 'from keystone.common import controller\n'), ((126, 5, 126, 27), 'keystone.common.controller.protected', 'controller.protected', ({}, {}), '()', False, 'from keystone.common import controller\n'), ((136, 5, 136, 27), 'keystone.common.controller.protected', 'controller.protected', ({}, {}), '()', False, 'from keystone.common import controller\n'), ((161, 5, 161, 27), 'keystone.common.controller.protected', 'controller.protected', ({}, {}), '()', False, 'from keystone.common import controller\n'), ((27, 8, 28, 60), 'keystone.notifications.register_event_callback', 'notifications.register_event_callback', ({(28, 12, 28, 21): '"""deleted"""', (28, 23, 28, 33): '"""endpoint"""', (28, 35, 28, 59): 'self._on_endpoint_delete'}, {}), "('deleted', 'endpoint', self.\n    _on_endpoint_delete)", False, 'from keystone import notifications\n'), ((29, 8, 30, 58), 'keystone.notifications.register_event_callback', 'notifications.register_event_callback', ({(30, 12, 30, 21): '"""deleted"""', (30, 23, 30, 32): '"""service"""', (30, 34, 30, 57): 'self._on_service_delete'}, {}), "('deleted', 'service', self.\n    _on_service_delete)", False, 'from keystone import notifications\n'), ((31, 8, 32, 56), 'keystone.notifications.register_event_callback', 'notifications.register_event_callback', ({(32, 12, 32, 21): '"""deleted"""', (32, 23, 32, 31): '"""region"""', (32, 33, 32, 55): 'self._on_region_delete'}, {}), "('deleted', 'region', self.\n    _on_region_delete)", False, 'from keystone import notifications\n'), ((33, 8, 34, 56), 'keystone.notifications.register_event_callback', 'notifications.register_event_callback', ({(34, 12, 34, 21): '"""deleted"""', (34, 23, 34, 31): '"""policy"""', (34, 33, 34, 55): 'self._on_policy_delete'}, {}), "('deleted', 'policy', self.\n    _on_policy_delete)", False, 'from keystone import notifications\n')]