""" Validation logic. ================= Validation of ownership change request, received from SQS. """ import validictory from ytownership import config from ytownership.validation.schema import schema def validate_sqs_message(message): """Validate input message with library. Also, if RESTRICT_API_ACCESS enabled in config (it's enabled by default for all envs, except prod), it checks whether provided ISRC exists in test ISRCs list. If it's not there - validation fails. If validation failed, sending log error message. Args: message (object): SQS message object. Returns: bool: validation result. """ try: payload = message.get_body() validictory.validate(payload, schema) if (config.RESTRICT_API_ACCESS and payload['isrc'] not in config.TEST_ISRCS): raise ValueError( 'RESTRICT_API_ACCESS configuration parameter enabled, and ' 'provided ISRC: {0} does not exist in test ISRCs list.'. format(payload['isrc'])) if not message.correlation_id: raise ValueError( 'Correlation-Id is not present in message attributes') except ValueError as e: message.logger.error(str(e)) return False return True