Skip to content

Commit 5849d0c

Browse files
author
Mark Oates
committed
Enable chunking in batches of 50 for API compatibility
1 parent 1c90748 commit 5849d0c

File tree

2 files changed

+63
-41
lines changed

2 files changed

+63
-41
lines changed

disableDetective.py

+30-19
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,14 @@ def delete_members(d_client: botocore.client.BaseClient, graph_arn: str,
259259
except e:
260260
logging.error(f'error when deleting member: {e}')
261261

262+
def chunked(it, size):
263+
it = iter(it)
264+
while True:
265+
p = tuple(itertools.islice(it, size))
266+
if not p:
267+
break
268+
yield p
269+
262270
if __name__ == '__main__':
263271
args = setup_command_line()
264272
aws_account_dict = read_accounts_csv(args.input_file)
@@ -286,27 +294,30 @@ def delete_members(d_client: botocore.client.BaseClient, graph_arn: str,
286294
# In this case the traceback adds LOTS of value.
287295
logging.exception(f'error creating session {e.args}')
288296

289-
for region in detective_regions:
290-
try:
291-
d_client = master_session.client('detective', region_name=region)
292-
graphs = get_graphs(d_client)
293-
if not graphs:
294-
logging.info(f'Amazon Detective has already been disabled in {region}')
295-
else:
296-
logging.info(f'Disabling Amazon Detective in region {region}')
297+
#Chunk the list of accounts in the .csv into batches of 50 due to the API limitation of 50 accounts per invokation
298+
for chunk in chunked(aws_account_dict.items(), 50):
297299

300+
for region in detective_regions:
298301
try:
299-
for graph in graphs:
300-
if not args.delete_graph:
301-
delete_members(d_client, graph, aws_account_dict)
302-
else:
303-
d_client.delete_graph(graph)
302+
d_client = master_session.client('detective', region_name=region)
303+
graphs = get_graphs(d_client)
304+
if not graphs:
305+
logging.info(f'Amazon Detective has already been disabled in {region}')
306+
else:
307+
logging.info(f'Disabling Amazon Detective in region {region}')
308+
309+
try:
310+
for graph in graphs:
311+
if not args.delete_graph:
312+
delete_members(d_client, graph, chunk)
313+
else:
314+
d_client.delete_graph(graph)
315+
except NameError as e:
316+
logging.error(f'account is not defined: {e}')
317+
except Exception as e:
318+
logging.exception(f'{e}')
319+
304320
except NameError as e:
305321
logging.error(f'account is not defined: {e}')
306322
except Exception as e:
307-
logging.exception(f'{e}')
308-
309-
except NameError as e:
310-
logging.error(f'account is not defined: {e}')
311-
except Exception as e:
312-
logging.exception(f'error with region {region}: {e}')
323+
logging.exception(f'error with region {region}: {e}')

enableDetective.py

+33-22
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,14 @@ def enable_detective(d_client: botocore.client.BaseClient, region: str, tags: di
318318
logging.info(f'Amazon Detective is enabled in region {region}')
319319

320320
return graphs
321+
322+
def chunked(it, size):
323+
it = iter(it)
324+
while True:
325+
p = tuple(itertools.islice(it, size))
326+
if not p:
327+
break
328+
yield p
321329

322330
if __name__ == '__main__':
323331
args = setup_command_line()
@@ -340,30 +348,33 @@ def enable_detective(d_client: botocore.client.BaseClient, region: str, tags: di
340348
# In this case the traceback adds LOTS of value.
341349
logging.exception(f'error creating session {e.args}')
342350

343-
for region in detective_regions:
344-
try:
345-
d_client = master_session.client('detective', region_name=region)
346-
graphs = enable_detective(d_client, region, args.tags)
347-
348-
if graphs is None:
349-
continue
351+
#Chunk the list of accounts in the .csv into batches of 50 due to the API limitation of 50 accounts per invokation
352+
for chunk in chunked(aws_account_dict.items(), 50):
350353

354+
for region in detective_regions:
351355
try:
352-
all_members, pending = get_members(d_client, graphs)
353-
354-
for graph, members in all_members.items():
355-
new_accounts = create_members(
356-
d_client, graph, args.disable_email, members, aws_account_dict)
357-
print("Sleeping for 5s to allow new members' invitations to propagate.")
358-
time.sleep(5)
359-
accept_invitations(args.assume_role, itertools.chain(
360-
new_accounts, pending[graph]), graph, region)
356+
d_client = master_session.client('detective', region_name=region)
357+
graphs = enable_detective(d_client, region, args.tags)
358+
359+
if graphs is None:
360+
continue
361+
362+
try:
363+
all_members, pending = get_members(d_client, graphs)
364+
365+
for graph, members in all_members.items():
366+
new_accounts = create_members(
367+
d_client, graph, args.disable_email, members, chunk)
368+
print("Sleeping for 5s to allow new members' invitations to propagate.")
369+
time.sleep(5)
370+
accept_invitations(args.assume_role, itertools.chain(
371+
new_accounts, pending[graph]), graph, region)
372+
except NameError as e:
373+
logging.error(f'account is not defined: {e}')
374+
except Exception as e:
375+
logging.exception(f'unable to accept invitiation: {e}')
376+
361377
except NameError as e:
362378
logging.error(f'account is not defined: {e}')
363379
except Exception as e:
364-
logging.exception(f'unable to accept invitiation: {e}')
365-
366-
except NameError as e:
367-
logging.error(f'account is not defined: {e}')
368-
except Exception as e:
369-
logging.exception(f'error with region {region}: {e}')
380+
logging.exception(f'error with region {region}: {e}')

0 commit comments

Comments
 (0)