def test_get_date_from_microtimestamp(self):
     # Timestamp = 1514764800 * 1000000 + 123456
     mock_timestamp = 1514764800123456
     # 2018/01/01 00:00:00.123456
     expected_result = datetime(2018, 1, 1, 0, 0, 0, 123456)
     result = date_time.get_date_from_microtimestamp(mock_timestamp)
     self.assertEqual(expected_result, result)
    def _make_content(self):
        """Create the email content.

        Returns:
            str: Email subject.
            unicode: Email template content rendered with
                the provided variables.
        """
        timestamp = date_time.get_date_from_microtimestamp(
            self.inventory_index_id)
        pretty_timestamp = timestamp.strftime(string_formats.TIMESTAMP_READABLE)
        email_content = self.connector.render_from_template(
            'notification_summary.jinja', {
                'scan_date': pretty_timestamp,
                'resource': self.resource,
                'violation_errors': self.violations,
            })

        email_subject = 'Forseti Violations {} - {}'.format(
            pretty_timestamp, self.resource)
        return email_subject, email_content
    def _send_email(self, summary_data, details_data, root_resources):
        """Send the email for inventory summary.

        Args:
            summary_data (list): Summary of inventory data as a list of dicts.
                Example: [{resource_type, count}, {}, {}, ...]

            details_data (list): Details of inventory data as a list of dicts.
                Example: [[{resource_type, count}, {}, {}, ...]

            root_resources (list): List of root resources, either the
                root_resource_id, or the composite_root_resources

        Raises:
            InvalidInputError: Indicating that invalid input was encountered.
        """
        LOGGER.debug('Sending inventory summary by email.')

        email_summary_config = (
            self.notifier_config.get('inventory').get('email_summary'))

        try:
            if self.notifier_config.get('email_connector'):
                email_connector = (EmailFactory(
                    self.notifier_config).get_connector())
            # else block below is added for backward compatibility
            else:
                email_connector = (
                    EmailFactory(email_summary_config).get_connector())
        except:
            LOGGER.exception('Error occurred to instantiate connector.')
            raise InvalidInputError(self.notifier_config)

        email_subject = 'Inventory Summary: {0}'.format(
            self.inventory_index_id)

        inventory_index_datetime = (date_time.get_date_from_microtimestamp(
            self.inventory_index_id))

        timestamp = inventory_index_datetime.strftime(
            string_formats.DEFAULT_FORSETI_TIMESTAMP)

        gsuite_dwd_status = self._get_gsuite_dwd_status(summary_data)

        email_content = email_connector.render_from_template(
            'inventory_summary.jinja', {
                'inventory_index_id': self.inventory_index_id,
                'timestamp': timestamp,
                'gsuite_dwd_status': gsuite_dwd_status,
                'summary_data': summary_data,
                'details_data': details_data,
                'root_resources': root_resources
            })

        if self.notifier_config.get('email_connector'):
            sender = (
                self.notifier_config.get('email_connector').get('sender'))
            recipient = (
                self.notifier_config.get('email_connector').get('recipient'))
        # else block below is added for backward compatibility.
        else:
            sender = email_summary_config.get('sender')
            recipient = email_summary_config.get('recipient')
        try:
            email_connector.send(email_sender=sender,
                                 email_recipient=recipient,
                                 email_subject=email_subject,
                                 email_content=email_content,
                                 content_type='text/html')
            LOGGER.debug('Inventory summary sent successfully by email.')
        except util_errors.EmailSendError:
            LOGGER.exception('Unable to send Inventory summary email')