diff --git a/cinq_collector_dns/views.py b/cinq_collector_dns/views.py index 8e0d524..cdf9693 100644 --- a/cinq_collector_dns/views.py +++ b/cinq_collector_dns/views.py @@ -3,6 +3,12 @@ from cloud_inquisitor.plugins.views import BaseView from cloud_inquisitor.utils import MenuItem from cloud_inquisitor.wrappers import check_auth, rollback +from cloud_inquisitor.json_utils import InquisitorJSONEncoder + +from flask import Response +from base64 import b64encode +import json + class DNSZoneList(BaseView): @@ -70,3 +76,35 @@ def get(self, zone_id): 'recordCount': len(zone.records), 'records': zone.records[start_offset:end_offset] }) + + +class DNSZonesExport(BaseView): + URLS = ['/api/v1/dns/zonesExport'] + + @rollback + @check_auth(ROLE_USER) + def get(self): + self.reqparse.add_argument('page', type=int, default=1, required=True) + self.reqparse.add_argument('count', type=int, default=99999, required=True) + self.reqparse.add_argument('fileFormat', type=str, default='json', choices=['json', 'xlsx']) + + args = self.reqparse.parse_args() + + total, zones = DNSZone.search(limit=99999, page=args['page']) + output = [{ + 'zones': [x.to_json(with_records=False) for x in zones], + 'zoneCount': total + }] + + response = Response( + response=b64encode( + bytes( + json.dumps(output, indent=4, cls=InquisitorJSONEncoder), + 'utf-8' + ) + ) + ) + response.content_type = 'application/octet-stream' + response.status_code = HTTP.OK + + return response diff --git a/setup.py b/setup.py index d0de2f3..2c2e804 100644 --- a/setup.py +++ b/setup.py @@ -20,6 +20,7 @@ 'view_dns_zone_list = cinq_collector_dns.views:DNSZoneList', 'view_dns_zone_details = cinq_collector_dns.views:DNSZoneDetails', 'view_dns_zone_records = cinq_collector_dns.views:DNSZoneRecords', + 'view_dns_zone_export = cinq_collector_dns.views:DNSZonesExport' ], },