Example #1
0
    def render(self, data, accepted_media_type=None, renderer_context=None):
        """Render all reports as gzip."""
        insights_dict = data
        if not insights_dict:
            return None

        return create_tar_buffer(insights_dict)
Example #2
0
    def render(self, reports_dict, media_type=None, renderer_context=None):
        """Render all reports as gzip."""
        # pylint: disable=arguments-differ,unused-argument,too-many-locals
        # pylint: disable=too-many-branches,too-many-statements
        if not bool(reports_dict):
            return None

        files_data = dict()

        report_id = reports_dict.get('report_id')
        # Collect Json Data
        details_json = reports_dict.get('details_json')
        deployments_json = reports_dict.get('deployments_json')
        if any(value is None
               for value in [report_id, details_json, deployments_json]):
            return None
        details_name = create_filename('details', 'json', report_id)
        files_data[details_name] = details_json
        deployments_name = create_filename('deployments', 'json', report_id)
        files_data[deployments_name] = deployments_json
        # Collect CSV Data
        details_csv = create_details_csv(details_json)
        deployments_csv = create_deployments_csv(deployments_json)
        if any(value is None for value in [details_csv, deployments_json]):
            return None
        details_csv_name = create_filename('details', 'csv', report_id)
        files_data[details_csv_name] = details_csv
        deployments_csv_name = create_filename('deployments', 'csv', report_id)
        files_data[deployments_csv_name] = deployments_csv
        tar_buffer = create_tar_buffer(files_data)
        if tar_buffer is None:
            logger.error(messages.REPORTS_TAR_ERROR)
            return None
        return tar_buffer
 def test_bad_param_type_create_tar_buffer(self):
     """Test passing in a non-list into create_tar_buffer."""
     json_list = [
         "{'id': 1, 'report': [{'key': 'value'}]}",
         "{'id': 2, 'report': [{'key': 'value'}]}"
     ]
     tar_result = create_tar_buffer(json_list)
     self.assertEqual(tar_result, None)
    def render(self,
               data,
               accepted_media_type=None,
               renderer_context=None):
        """Render all reports as gzip."""
        # pylint: disable=too-many-locals
        reports_dict = data
        if not bool(reports_dict):
            return None
        report_id = reports_dict.get('report_id')
        # Collect Json Data
        details_json = reports_dict.get('details_json')
        deployments_json = reports_dict.get('deployments_json')
        if any(value is None for value in [report_id,
                                           details_json,
                                           deployments_json]):
            return None

        # Collect CSV Data
        details_csv = create_details_csv(details_json)
        deployments_csv = create_deployments_csv(deployments_json)
        if any(value is None for value in [details_csv, deployments_csv]):
            return None

        # grab hashes
        details_json_hash = create_hash(details_json, 'json')
        deployments_json_hash = create_hash(deployments_json, 'json')
        details_csv_hash = create_hash(details_csv, 'csv')
        deployments_csv_hash = create_hash(deployments_csv, 'csv')

        # create the file names
        details_json_name = create_filename('details', 'json', report_id)
        deployments_json_name = create_filename(
            'deployments', 'json', report_id)
        details_csv_name = create_filename('details', 'csv', report_id)
        deployments_csv_name = create_filename('deployments', 'csv', report_id)
        sha256sum_name = create_filename('SHA256SUM', None, report_id)

        # map the file names to the file data
        files_data = {
            details_json_name: details_json,
            deployments_json_name: deployments_json,
            details_csv_name: details_csv,
            deployments_csv_name: deployments_csv,
            sha256sum_name:
                details_json_hash + '  details.json\n' +
                deployments_json_hash + '  deployments.json\n' +
                details_csv_hash + '  details.csv\n' +
                deployments_csv_hash + '  deployments.csv'
        }

        tar_buffer = create_tar_buffer(files_data)
        if tar_buffer is None:
            logger.error(messages.REPORTS_TAR_ERROR)
            return None
        return tar_buffer
 def test_create_tar_buffer(self):
     """Test create_tar_buffer method."""
     files_data = dict()
     files_data['test0.json'] = {'id': 1, 'report': [{'key': 'value'}]}
     files_data['test1.json'] = {'id': 2, 'report': [{'key': 'value'}]}
     tar_buffer = create_tar_buffer(files_data)
     self.assertIsInstance(tar_buffer, (io.BytesIO))
     self.assertIn('getvalue', dir(tar_buffer))
     tar = tarfile.open(fileobj=tar_buffer)
     files = tar.getmembers()
     self.assertNotEqual(files, [])
     self.assertEqual(2, len(files))
     for file_obj in files:
         file = tar.extractfile(file_obj)
         extracted_content = json.loads(file.read().decode())
         self.assertIn(extracted_content, files_data.values())
    def render(self, data, accepted_media_type=None, renderer_context=None):
        """Render report as json gzip."""
        report_dict = data
        if not bool(report_dict):
            return None

        report_id = report_dict.get('report_id')
        report_type = report_dict.get('report_type')
        if report_id is None:
            return None
        if report_type is None:
            file_name = '%s.json' % time.strftime('%Y%m%d%H%M%S')
        else:
            file_name = create_filename(report_type, 'json', report_id)
        file_data = {file_name: report_dict}
        tar_buffer = create_tar_buffer(file_data)
        return tar_buffer
 def test_extract_tar_gz_content_good(self):
     """Test extracting files by passing a BytesIO object."""
     files_data = dict()
     files_data['test0.json'] = {'id': 1, 'report': [{'key': 'value'}]}
     files_data['test1.json'] = {'id': 2, 'report': [{'key': 'value'}]}
     tar_buffer = create_tar_buffer(files_data)
     self.assertIsInstance(tar_buffer, (io.BytesIO))
     # bytesIO
     file_contents = extract_tar_gz(tar_buffer)
     for data in file_contents:
         self.assertIn(data, files_data.values())
     self.assertEqual(len(file_contents), len(files_data.values()))
     # hexstring
     file_contents = extract_tar_gz(tar_buffer.getvalue())
     for data in file_contents:
         self.assertIn(data, files_data.values())
     self.assertEqual(len(file_contents), len(files_data.values()))
    def render(self, report_dict, media_type=None, renderer_context=None):
        """Render report as json gzip."""
        # pylint: disable=arguments-differ,unused-argument,too-many-locals
        # pylint: disable=too-many-branches,too-many-statements

        if not bool(report_dict):
            return None

        report_id = report_dict.get('report_id')
        report_type = report_dict.get('report_type')
        if report_id is None:
            return None
        if report_type is None:
            file_name = '%s.json' % time.strftime('%Y%m%d%H%M%S')
        else:
            file_name = create_filename(report_type, 'json', report_id)
        file_data = {file_name: report_dict}
        tar_buffer = create_tar_buffer(file_data)
        return tar_buffer
 def test_bad_dict_contents_type_create_tar_buffer(self):
     """Test passing in a list of json into create_tar_buffer."""
     json0 = {'id.csv': 1, 'report': [{'key.csv': 'value'}]}
     tar_result = create_tar_buffer(json0)
     self.assertEqual(tar_result, None)