"""Product ownership validation module.""" from oto import response from oto import status from owsrequest import request from sentry_sdk import capture_exception from availability.constants import error from availability.constants import header from availability.constants import service_name def check_products_ownership( product_ids, account_type, account_id, correlation_id): """Check ownership of a product. Args: product_ids (list): Product IDs to check ownership for. account_type (str): account type (either “subaccount” or “vendor”). account_id (str): account id. Cannot be empty. correlation_id (str): Correlation-Id header of request. Returns: response.Response: result of checking the ownership. """ request_url = '/{account_type}/{account_id}/product/{product_id}' request_headers = { header.CORRELATION_ID: correlation_id } for product_id in product_ids: product_url = request_url.format( account_type=account_type, account_id=account_id, product_id=product_id) try: ownership_response = request.head( service_name.OWS_PRODUCT, product_url, headers=request_headers) except Exception as e: capture_exception(e) return response.create_fatal_response() if not ownership_response.status_code == status.OK: return response.create_error_response( code=error.ERROR_CODE_AUTHORIZATION, status=status.FORBIDDEN, message=error.ERROR_MESSAGE_NOT_AUTHORIZED) return response.Response(status=status.NO_CONTENT)