#!/usr/bin/python3

 

import boto3, xlsxwriter

 

cdn_client = boto3.client('cloudfront')

 

list_cdn = cdn_client.list_distributions()

cdn_array = list_cdn['DistributionList']['Items']

 

i = 0

 

with xlsxwriter.Workbook('cdn.xlsx') as xl_report:

        bold_char = xl_report.add_format({'bold': True})

        while i < len(cdn_array):

                work = xl_report.add_worksheet(cdn_array[i]['DomainName'])

                work.write('A1', "Domain Name : %s " % cdn_array[i]['DomainName'])

                work.write('A2', "ID : %s" % cdn_array[i]['Id'])

                work.write('A3', "Status : %s" % cdn_array[i]['Status'])

                work.write('A4', "Origins :", bold_char)

                origin_array = cdn_array[i]['Origins']['Items']

                m = 0

                size = len(origin_array)

                while m < size:

                        n = 5

                        q = n + m

                        row_no = 'B' + str(q)

                        work.write(row_no, "%s with ID : %s . " % (origin_array[m]['DomainName'], origin_array[m]['Id']))

                        m = m + 1

                new_row = size + 5

                work.write('A' + str(new_row), "MaxTTL for Origin %s is %s secs " % (cdn_array[i]['DefaultCacheBehavior']['TargetOriginId'], cdn_array[i]['DefaultCacheBehavior']['MaxTTL']))

                work.write('A' + str(new_row + 1), "DefaultTTL for Origin %s is %s secs " % (cdn_array[i]['DefaultCacheBehavior']['TargetOriginId'], cdn_array[i]['DefaultCacheBehavior']['DefaultTTL']))

                work.write('A' + str(new_row + 2), "MinTTL for Origin %s is %s secs " % (cdn_array[i]['DefaultCacheBehavior']['TargetOriginId'], cdn_array[i]['DefaultCacheBehavior']['MinTTL']))

                i = i + 1

xl_report.close()

print('An Excel file has been generated with CDN information. Please check local folder....')