"""PIP lookup handlers for Permission Platform.""" from connector_neo4j import Neo4jSession from ddtrace import tracer from owsresponse import response from owsresponse.adaptors.flask import flaskify from account.api import app from account.constants import error from account.constants.connectors import NEO4J_DATABASE_NAME from account.logic import company_brand, feature, subaccount, vendor from account.utils.api_utils import validate_request_data from account.validation.schemas.lookup import ( LookupCompanyBrandsByIds, LookupCompanyBrandsByUuids, LookupSubaccountFeaturesByUuids, LookupSubaccountsBySubaccountIds, LookupSubaccountsByUuids, LookupVendorFeaturesByUuids, LookupVendorsByUuids, LookupVendorsByVendorIds, ) @tracer.wrap() @app.route('/lookup/vendors/uuids/', methods=['POST']) @Neo4jSession(use_v2=True, database=NEO4J_DATABASE_NAME) @validate_request_data(LookupVendorsByUuids()) def lookup_vendors_by_uuids(deserialize_schema): """Get list of vendor ids using their uuids. NOTE: This endpoint is configured to require NO access rule checks. This is because it is serving as a Policy Information Point for Permission Platform. Do not expose other properties or attributes via this endpoint. Returns: Flask.response. """ uuids = deserialize_schema['uuids'] fetch_flags_enum = deserialize_schema.get('fetch_flags', []) fetch_flags = [flag.name for flag in fetch_flags_enum] if not uuids: return flaskify( response.create_error_response( error.ERROR_CODE_INVALID_INPUT, error.ERROR_MESSAGE_INVALID_REQUEST ) ) return flaskify(vendor.lookup_vendors_by_uuids(uuids, fetch_flags)) @tracer.wrap() @app.route('/lookup/vendors/vendor-ids/', methods=['POST']) @Neo4jSession(use_v2=True, database=NEO4J_DATABASE_NAME) @validate_request_data(LookupVendorsByVendorIds()) def lookup_vendors_by_vendor_ids(deserialize_schema): """Get list of vendor uuids using their vendor_ids. NOTE: This endpoint is configured to require NO access rule checks. This is because it is serving as a Policy Information Point for Permission Platform. Do not expose other properties or attributes via this endpoint. Returns: Flask.response. """ vendor_ids = deserialize_schema['vendor_ids'] fetch_flags_enum = deserialize_schema.get('fetch_flags', []) fetch_flags = [flag.name for flag in fetch_flags_enum] if not vendor_ids: return flaskify( response.create_error_response( error.ERROR_CODE_INVALID_INPUT, error.ERROR_MESSAGE_INVALID_REQUEST ) ) return flaskify(vendor.lookup_vendors_by_vendor_ids(vendor_ids, fetch_flags)) @tracer.wrap() @app.route('/lookup/vendors/features/uuids/', methods=['POST']) @validate_request_data(LookupVendorFeaturesByUuids()) def lookup_vendors_features_by_uuids(deserialize_schema): """Get enabled feature ids for vendors using their uuids. NOTE: This endpoint is configured to require NO access rule checks. This is because it is serving as a Policy Information Point for Permission Platform. Do not expose other properties or attributes via this endpoint. Returns: Flask.response. """ uuids = deserialize_schema['uuids'] if not uuids: return flaskify( response.create_error_response( error.ERROR_CODE_INVALID_INPUT, error.ERROR_MESSAGE_INVALID_REQUEST ) ) return flaskify(feature.lookup_features_by_vendor_uuids([str(uuid) for uuid in uuids])) @tracer.wrap() @app.route('/lookup/subaccounts/features/uuids/', methods=['POST']) @validate_request_data(LookupSubaccountFeaturesByUuids()) def lookup_subaccount_features_by_uuids(deserialize_schema): """Get enabled feature ids for subaccounts using their uuids. NOTE: This endpoint is configured to require NO access rule checks. This is because it is serving as a Policy Information Point for Permission Platform. Do not expose other properties or attributes via this endpoint. Returns: Flask.response. """ uuids = deserialize_schema['uuids'] if not uuids: return flaskify( response.create_error_response( error.ERROR_CODE_INVALID_INPUT, error.ERROR_MESSAGE_INVALID_REQUEST ) ) return flaskify(feature.lookup_features_by_subaccount_uuids([str(uuid) for uuid in uuids])) @tracer.wrap() @app.route('/lookup/subaccounts/uuids/', methods=['POST']) @validate_request_data(LookupSubaccountsByUuids()) def lookup_subaccounts_by_uuids(deserialize_schema): """Get list of subaccount ids using their uuids. NOTE: This endpoint is configured to require NO access rule checks. This is because it is serving as a Policy Information Point for Permission Platform. Do not expose other properties or attributes via this endpoint. Returns: Flask.response. """ uuids = deserialize_schema['uuids'] fetch_flags_enum = deserialize_schema.get('fetch_flags', []) fetch_flags = [flag.name for flag in fetch_flags_enum] if not uuids: return flaskify( response.create_error_response( error.ERROR_CODE_INVALID_INPUT, error.ERROR_MESSAGE_INVALID_REQUEST ) ) return flaskify(subaccount.lookup_subaccounts_by_uuids(uuids, fetch_flags)) @app.route('/lookup/subaccounts/subaccount-ids/', methods=['POST']) @validate_request_data(LookupSubaccountsBySubaccountIds()) def lookup_subaccounts_by_ids(deserialize_schema): """Get list of subaccount uuids using their subaccount_ids. NOTE: This endpoint is configured to require NO access rule checks. This is because it is serving as a Policy Information Point for Permission Platform. Do not expose other properties or attributes via this endpoint. Returns: Flask.response. """ subaccount_ids = deserialize_schema.get('subaccount_ids', []) fetch_flags_enum = deserialize_schema.get('fetch_flags', []) fetch_flags = [flag.name for flag in fetch_flags_enum] if not subaccount_ids: return flaskify( response.create_error_response( error.ERROR_CODE_INVALID_INPUT, error.ERROR_MESSAGE_INVALID_REQUEST ) ) return flaskify(subaccount.lookup_subaccounts_by_subaccount_ids(subaccount_ids, fetch_flags)) @tracer.wrap() @app.route('/lookup/company-brands/uuids/', methods=['POST']) @validate_request_data(LookupCompanyBrandsByUuids()) def lookup_company_brands_by_uuids(deserialize_schema): """Get list of company brands ids using their uuids. NOTE: This endpoint is configured to require NO access rule checks. This is because it is serving as a Policy Information Point for Permission Platform. Do not expose other properties or attributes via this endpoint. Returns: Flask.response. """ uuids = deserialize_schema['uuids'] if not uuids: return flaskify( response.create_error_response( error.ERROR_CODE_INVALID_INPUT, error.ERROR_MESSAGE_INVALID_REQUEST ) ) return flaskify(company_brand.lookup_company_brands_by_uuids(uuids)) @tracer.wrap() @app.route('/lookup/company-brands/company-brand-ids/', methods=['POST']) @validate_request_data(LookupCompanyBrandsByIds()) def lookup_company_brands_by_ids(deserialize_schema): """Get list of company brands ids using their ids. NOTE: This endpoint is configured to require NO access rule checks. This is because it is serving as a Policy Information Point for Permission Platform. Do not expose other properties or attributes via this endpoint. Returns: Flask.response. """ company_brand_ids = deserialize_schema['company_brand_ids'] if not company_brand_ids: return flaskify( response.create_error_response( error.ERROR_CODE_INVALID_INPUT, error.ERROR_MESSAGE_INVALID_REQUEST ) ) return flaskify(company_brand.lookup_company_brands_by_ids(company_brand_ids))