Example #1
0
    def _upload_to_gcs(self, summary_data):
        """Upload inventory summary to GCS.

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

        LOGGER.debug('Uploading inventory summary data to GCS.')
        gcs_summary_config = (
            self.notifier_config.get('inventory').get('gcs_summary'))

        if not gcs_summary_config.get('gcs_path'):
            LOGGER.error('gcs_path not set for inventory summary notifier.')
            return

        if not gcs_summary_config['gcs_path'].startswith('gs://'):
            LOGGER.error('Invalid GCS path: %s',
                         gcs_summary_config['gcs_path'])
            return

        data_format = gcs_summary_config.get('data_format', 'csv')
        base_notification.BaseNotification.check_data_format(data_format)

        try:
            if data_format == 'csv':
                gcs_upload_path = '{}/{}'.format(
                    gcs_summary_config.get('gcs_path'),
                    self._get_output_filename(
                        string_formats.INVENTORY_SUMMARY_CSV_FMT))
                file_uploader.upload_csv('inv_summary', summary_data,
                                         gcs_upload_path)
            else:
                gcs_upload_path = '{}/{}'.format(
                    gcs_summary_config.get('gcs_path'),
                    self._get_output_filename(
                        string_formats.INVENTORY_SUMMARY_JSON_FMT))
                file_uploader.upload_json(summary_data, gcs_upload_path)
            log_message = f'Inventory Summary file saved to GCS path: ' \
                          f'{gcs_upload_path}'
            self.progress_queue.put(log_message)
            LOGGER.info(log_message)
        except HttpError:
            LOGGER.exception(
                'Unable to upload inventory summary in bucket %s:',
                gcs_upload_path)
    def run(self):
        """Generate the temporary (CSV xor JSON) file and upload to GCS."""
        if not self.notification_config['gcs_path'].startswith('gs://'):
            return

        data_format = self.notification_config.get('data_format', 'csv')
        if data_format not in self.supported_data_formats:
            raise base_notification.InvalidDataFormatError(
                'GCS uploader', data_format)

        if data_format == 'csv':
            gcs_upload_path = '{}/{}'.format(
                self.notification_config['gcs_path'],
                self._get_output_filename(string_formats.VIOLATION_CSV_FMT))
            file_uploader.upload_csv('violations', self.violations,
                                     gcs_upload_path)
        else:
            gcs_upload_path = '{}/{}'.format(
                self.notification_config['gcs_path'],
                self._get_output_filename(string_formats.VIOLATION_JSON_FMT))
            file_uploader.upload_json(self.violations, gcs_upload_path)