"""Lambda update_mappings_sme_analytics function module.""" from config import app_logger as logger from marshmallow import EXCLUDE from snowflake.connector.errors import OperationalError import constants.general as constants from helpers.snowflake_utils import execute_snowflake_query from src.schemas.update_mappings_schema import UpdateMappingsSchema class MappingExist(Exception): """Mapping already exists, and it is different from the new one. Cannot retry Exception. """ pass class RetryException(Exception): """Retry Exception.""" pass def handler(event, context): """Lambda entry point.""" input_data = event or {} try: # validate input and exclude extra properties that are not in schema. mapping = UpdateMappingsSchema().load(input_data, unknown=EXCLUDE) logger.info( 'Successfully validated the request for updating mapping for ' f"vendor {mapping['vendor_id']} " f"and subaccount {mapping['subaccount_id']}") logger.info(f'Checking existing mapping: {mapping}') check_mapping = execute_snowflake_query( constants.CHECK_VENDOR_AND_SUBACCOUNT_SQL, mapping) if check_mapping: raise MappingExist( 'Mapping already exists - ', check_mapping) update_mapping = execute_snowflake_query( constants.UPDATE_VENDOR_AND_SUBACCOUNT_SQL, mapping) logger.info('Mapping has been successfully updated!') return update_mapping except OperationalError as err: raise RetryException('Snowflake connection issue.', err)