-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfoEC2VolumesReportUsage.py
283 lines (241 loc) · 9.21 KB
/
infoEC2VolumesReportUsage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import boto3
import xlsxwriter
import datetime
from botocore.exceptions import ClientError
# Time 0 days ago. I mean NOW.
today = datetime.datetime.now()
"""
Function: infoEC2VolumesReportUsage()
Input:
AWS_PROFILE=sso-nvsit-pu python3 infoEC2VolumesReportUsage.py
Output:
Excel in S3 with all report.
Descr: Obtain a Excel file that contains info about available Volumes to be deleted.
"""
def create_excel(excelFileName):
# Name of file XLSX Excel that contains all info.
print("Excel File going to be created --> "+ excelFileName)
# Create workbook.
workbook = xlsxwriter.Workbook(f'{excelFileName}')
# Add MAIN worksheet to the workbook.
main_worksheet = workbook.add_worksheet('INFORMATION')
# Set auto filter on for the cells
main_worksheet.autofilter('C3:I4')
# Hide unused rows.
main_worksheet.set_default_row(hide_unused_rows=True)
# Define wihite backgroup format.
whiteBackGroundFromat = workbook.add_format(
{
'bg_color': "#FFFFFF",
'align': 'center',
'valign': 'center',
'font_name': 'calibri',
'font_size': 11,
'text_wrap': True,
'locked': True,
'border': 0
}
)
# Merge Format for KP worksheet.
mergedFormat = workbook.add_format(
{
'bg_color': "#000000",
'font_color': "#FFFFFF",
'align': 'center',
'valign': 'center',
'font_name': 'Segoe UI',
'font_size': 15,
'bold': 'True',
'locked': 'True',
'border': 0
}
)
# Cells under Merged form.
subMergedFormat = workbook.add_format(
{
'bg_color': "03C0FF",
'font_color': "#FFFFFF",
'align': 'vcenter',
'valign': 'center',
'font_name': 'Calibri',
'font_size': 13,
'bold': 'True',
'locked': 'True',
'border': 5,
'top': 5,
'bottom': 5,
'left': 5,
'right': 5,
'border_color': "#FFFFFF"
}
)
grayFormatDark = workbook.add_format(
{
'bg_color': "#CDD1DE",
'font_color': "#000000",
'align': 'center',
'valign': 'center',
'text_wrap': True,
'font_name': 'Calibri',
'font_size': 10,
'locked': 'True',
'border': 4,
'top': 4,
'bottom': 4,
'left': 4,
'right': 4,
'border_color': "#FFFFFF"
}
)
grayFormatDarkNum = workbook.add_format(
{
'bg_color': "#CDD1DE",
'font_color': "#000000",
'align': 'center',
'valign': 'center',
'text_wrap': True,
'font_name': 'Calibri',
'font_size': 10,
'locked': 'True',
'border': 4,
'top': 4,
'bottom': 4,
'left': 4,
'right': 4,
'border_color': "#FFFFFF",
'num_format': 'dd.mm.yyyy'
}
)
grayFormatLight = workbook.add_format(
{
'bg_color': "#E8E9EF",
'font_color': "#000000",
'align': 'center',
'valign': 'center',
'text_wrap': True,
'font_name': 'Calibri',
'font_size': 10,
'locked': 'True',
'border': 4,
'top': 4,
'bottom': 4,
'left': 4,
'right': 4,
'border_color': "#FFFFFF"
}
)
grayFormatLightNum = workbook.add_format(
{
'bg_color': "#E8E9EF",
'font_color': "#000000",
'align': 'center',
'valign': 'center',
'text_wrap': True,
'font_name': 'Calibri',
'font_size': 10,
'locked': 'True',
'border': 4,
'top': 4,
'bottom': 4,
'left': 4,
'right': 4,
'border_color': "#FFFFFF",
'num_format': 'dd.mm.yyyy'
}
)
# Make the sheet white with no boarder.
for whiteBackGroundCells in range(100): # integer odd-even alternation.
main_worksheet.set_row(whiteBackGroundCells, cell_format=(whiteBackGroundFromat))
# Set column width across the worksheet.
main_worksheet.set_column("B:I", 21.57)
# First all SSP merge cells.
main_worksheet.merge_range('C2:I2', 'AWS', mergedFormat)
# All headers and name of platform.
main_worksheet.merge_range('B2:B4', 'VOLUME ID', subMergedFormat)
main_worksheet.merge_range('C3:C4', 'REGION', subMergedFormat)
main_worksheet.merge_range('D3:D4', 'ENCRYPTED', subMergedFormat)
main_worksheet.merge_range('E3:E4', 'SIZE', subMergedFormat)
main_worksheet.merge_range('F3:F4', 'VOLUMETYPE', subMergedFormat)
main_worksheet.merge_range('G3:G4', 'SNAPSHOT', subMergedFormat)
main_worksheet.merge_range('H3:H4', 'AVAILABILITYZONE', subMergedFormat)
main_worksheet.merge_range('I3:I4', 'MULTIATTACHENABLED', subMergedFormat)
# Create a tupla to use later.
return_tupla_to_later_reuse_temporal = workbook, grayFormatDark, grayFormatDarkNum, grayFormatLight, grayFormatLightNum
# Return tupla
return return_tupla_to_later_reuse_temporal
def write_in_excel_Worksheet(workbook, grayFormatDark, grayFormatDarkNum, grayFormatLight, grayFormatLightNum, data_information_complete):
# We are going to LOAD previous Worksheet.
existingWorksheet = workbook.get_worksheet_by_name('INFORMATION')
row = 4
col = 1
# Loop to print to all rows and columns.
for VolumeId, Region, Encrypted, Size, VolumeType, Snapshot, AvailabilityZone, MultiAttachEnabled in data_information_complete:
# Condition to format alternate rows
if row%2 == 1:
cellFormatFix = grayFormatDark
cellFormatNumFix = grayFormatDarkNum
else:
cellFormatFix = grayFormatLight
cellFormatNumFix = grayFormatLightNum
existingWorksheet.write_string(row, col, VolumeId, cellFormatFix )
existingWorksheet.write_string(row, col + 1 , Region, cellFormatFix )
existingWorksheet.write_string(row, col + 2 , Encrypted, cellFormatFix )
existingWorksheet.write_string(row, col + 3 , Size, cellFormatFix )
existingWorksheet.write_string(row, col + 4 , VolumeType, cellFormatFix )
existingWorksheet.write_string(row, col + 5 , Snapshot, cellFormatFix )
existingWorksheet.write_string(row, col + 6 , AvailabilityZone, cellFormatFix )
existingWorksheet.write_string(row, col + 7 , MultiAttachEnabled, cellFormatFix )
row += 1
workbook.close()
print("Workbook closed")
def obtain_all_results():
# Inicial configuration EC2.
client = boto3.client('ec2')
global volume_list
volume_list = []
# Check all regions.
for region in client.describe_regions()['Regions']:
# Save the region to get later all volumes.
regions_to_check=region['RegionName']
# Initiate boto3 in each region.
client = boto3.client('ec2', region_name=regions_to_check)
# Describe volumes that are in an available state.
volumes = client.describe_volumes( Filters=[{'Name': 'status', 'Values': ['available']}])
if volumes['Volumes']:
# If there are volumes in an available state show print region.
print ("\nRegion: " + regions_to_check)
else:
# If there are no volumes in an available state show region.
print ("\nRegion: "+ regions_to_check + "\tNo volumes in available state")
#Loop through volumes only in an available state.
for volume in volumes['Volumes']:
#Add volume and its region to volume_list.
lsst = (f"{volume['VolumeId']}|{regions_to_check}|{volume['Encrypted']}|{volume['Size']}|{volume['VolumeType']}|{volume['SnapshotId']}|{volume['AvailabilityZone']}|{volume['MultiAttachEnabled']}")
volume_list.append(lsst.split('|'))
# Information ID of every volume.
print ("Volume: " + volume['VolumeId'])
if volume_list:
# Get the list of volumes in state available.
print ("\nYou have "+str(len(volume_list)) +" volumes in an available state")
return volume_list
else:
#Exit if not volumes.
print("\nYou have no volumes in an available state")
print("\nExiting")
exit()
##########################################
###### infoEC2VolumesReportUsage.py #####
##########################################
# Excel Name.
excelFileName = ('infoEC2VolumesReportUsage' + '-' + today.strftime("%d""-""%b") + '.xlsx')
# First execution
try:
data_information_complete = obtain_all_results()
# Create Excel and return tupla with all neccesary to later write with data.
return_tupla_to_later_reuse = create_excel(excelFileName)
# Write in an excel.
write_in_excel_Worksheet(return_tupla_to_later_reuse[0], return_tupla_to_later_reuse[1], return_tupla_to_later_reuse[2], return_tupla_to_later_reuse[3], return_tupla_to_later_reuse[4], data_information_complete)
except:
ClientError