def create(reporting_config): """ Creates the EnterpriseReportSender and all of its dependencies. """ enterprise_customer_name = reporting_config['enterprise_customer'][ 'name'] vertica_client = VerticaClient(os.environ.get('VERTICA_HOST'), os.environ.get('VERTICA_USERNAME'), os.environ.get('VERTICA_PASSWORD')) if reporting_config['delivery_method'] == 'email': LOGGER.debug( '{} is configured to send the report via email to {}'.format( enterprise_customer_name, reporting_config['email'], )) delivery_method = EmailDeliveryMethod( reporting_config['email'], decrypt_string(reporting_config['encrypted_password']), enterprise_customer_name) elif reporting_config['delivery_method'] == 'sftp': LOGGER.debug('{} is configured to send the report via sftp'.format( enterprise_customer_name)) delivery_method = SFTPDeliveryMethod( reporting_config['sftp_hostname'], reporting_config['sftp_port'], reporting_config['sftp_username'], decrypt_string(reporting_config['encrypted_sftp_password']), reporting_config['sftp_file_path'], enterprise_customer_name) else: raise Exception return EnterpriseReportSender(reporting_config, vertica_client, delivery_method)
def create(reporting_config): """Create the EnterpriseReportSender and all of its dependencies.""" enterprise_customer_name = reporting_config['enterprise_customer'][ 'name'] delivery_method_str = reporting_config['delivery_method'] if delivery_method_str == 'email': LOGGER.debug( '{} is configured to send the report via SMTP to {}'.format( enterprise_customer_name, reporting_config['email'], )) delivery_method = SMTPDeliveryMethod( reporting_config, decrypt_string(reporting_config['encrypted_password']), ) elif delivery_method_str == 'sftp': LOGGER.debug( '{} is configured to send the report via SFTP to {}'.format( enterprise_customer_name, reporting_config['sftp_hostname'], )) delivery_method = SFTPDeliveryMethod( reporting_config, decrypt_string(reporting_config['encrypted_sftp_password']), ) else: raise ValueError(f'Invalid delivery method: {delivery_method_str}') return EnterpriseReportSender(reporting_config, delivery_method)
def send(self, files): """Base method for sending files, to perform common sending logic.""" LOGGER.info( f'Encrypting data report for {self.enterprise_customer_name}') zip_password = decrypt_string( self.encrypted_password ) if self.encrypted_password else self.encrypted_password return compress_and_encrypt(files, zip_password, self.pgp_encryption_key)
def send_enterprise_report(self): """ Query the data warehouse (vertica) and export data to a csv file. This file will get encrypted and emailed to the Enterprise Customer. """ enterprise_customer_name = self.reporting_config[ 'enterprise_customer']['name'] LOGGER.info('Starting process to send email report to {}'.format( enterprise_customer_name)) # initialize base csv file and file writer data_report_file_name, data_report_file_writer = self._create_data_report_csv_writer( ) # query vertica and write each row to the file LOGGER.debug('Querying Vertica for data for {}'.format( enterprise_customer_name)) data_report_file_writer.writerows(self._query_vertica()) # create a password encrypted zip file LOGGER.debug( 'Encrypting data report for {}'.format(enterprise_customer_name)) data_report_zip_name = compress_and_encrypt( data_report_file_name, decrypt_string(self.reporting_config['password'], self.reporting_config['initialization_vector'])) # email the file to the email address in the configuration LOGGER.debug( 'Sending encrypted data to {}'.format(enterprise_customer_name)) try: send_email_with_attachment(self.REPORT_EMAIL_SUBJECT, self.REPORT_EMAIL_BODY, self.REPORT_EMAIL_FROM_EMAIL, self.reporting_config['email'], data_report_zip_name) except SMTPException: LOGGER.exception( 'Failed to send email for {}'.format(enterprise_customer_name)) self._cleanup()