in src/handlers/resource_report_handler.py [0:0]
def get_latest(self, event: ResourcesReportGetModel, tenant_name: str):
tenant_item = self._tenant_service.get(tenant_name)
tenant_item = modular_helpers.assert_tenant_valid(
tenant_item, event.customer
)
collection = self._report_service.tenant_latest_collection(tenant_item)
if event.region:
_LOG.debug(
'Region is provided. Fetching only shard with ' 'this region'
)
collection.fetch(region=event.region)
else:
_LOG.debug('Region is not provided. Fetching all shards')
collection.fetch_all()
_LOG.debug('Fetching meta')
collection.fetch_meta()
metadata = self._ls.get_customer_metadata(event.customer_id)
dictionary_url = None
dictionary = {} # todo maybe refactor somehow
matched = MatchedResourcesIterator(
collection=collection,
resource_type=event.resource_type,
region=event.region,
exact_match=event.exact_match,
search_by_all=event.search_by_all,
search_by=event.extras,
dictionary_out=dictionary if event.obfuscated else None,
)
content = {}
match event.format:
case ReportFormat.JSON:
content = ResourceReportBuilder(
matched_findings_iterator=matched,
entity=tenant_item,
full=event.full,
metadata=metadata,
).build()
if event.obfuscated:
flip_dict(dictionary)
dictionary_url = self._report_service.one_time_url_json(
dictionary, 'dictionary.json'
)
if event.href:
url = self._report_service.one_time_url_json(
content, f'{tenant_name}-latest.json'
)
content = ReportResponse(
tenant_item, url, dictionary_url, event.format
).dict()
case ReportFormat.XLSX:
buffer = tempfile.TemporaryFile()
with Workbook(buffer, {'strings_to_numbers': True}) as wb:
ResourceReportXlsxWriter(matched, metadata).write(
wb=wb, wsh=wb.add_worksheet(tenant_name)
)
if event.obfuscated:
flip_dict(dictionary)
dictionary_url = self._report_service.one_time_url_json(
dictionary, 'dictionary.json'
)
buffer.seek(0)
url = self._report_service.one_time_url(
buffer, f'{tenant_name}-latest.xlsx'
)
content = ReportResponse(
tenant_item, url, dictionary_url, event.format
).dict()
return build_response(content=content)