@@ -42,7 +42,14 @@ def _master_account_type(val: str, pattern: str = r'[0-9]{12}'):
42
42
raise argparse .ArgumentTypeError
43
43
return val
44
44
45
- # Setup command line arguments
45
+ class ParseCommaSeparatedKeyValuePairsAction (argparse .Action ):
46
+ def __call__ (self , parser , namespace , values , option_string = None ):
47
+ setattr (namespace , self .dest , dict ())
48
+ for kv_pairs in values .split ("," ):
49
+ key , value = kv_pairs .split ('=' , 1 )
50
+ getattr (namespace , self .dest )[key ] = value
51
+
52
+ # Setup command line arguments
46
53
parser = argparse .ArgumentParser (description = ('Link AWS Accounts to central '
47
54
'Detective Account.' ))
48
55
parser .add_argument ('--master_account' , type = _master_account_type ,
@@ -57,6 +64,9 @@ def _master_account_type(val: str, pattern: str = r'[0-9]{12}'):
57
64
parser .add_argument ('--enabled_regions' , type = str ,
58
65
help = ('Regions to enable Detective. If not specified, '
59
66
'all available regions enabled.' ))
67
+ parser .add_argument ('--tags' ,
68
+ action = ParseCommaSeparatedKeyValuePairsAction ,
69
+ help = "Tags to be added to any newly enabled Detective graphs." )
60
70
return parser .parse_args (args )
61
71
62
72
@@ -286,15 +296,15 @@ def accept_invitations(role: str, accounts: typing.Set[str], graph: str, region:
286
296
except Exception as e :
287
297
logging .exception (f'error accepting invitation { e .args } ' )
288
298
289
- def enable_detective (d_client : botocore .client .BaseClient , region : str ):
299
+ def enable_detective (d_client : botocore .client .BaseClient , region : str , tags : dict = None ):
290
300
graphs = get_graphs (d_client )
291
301
292
302
if not graphs :
293
303
confirm = input ('Should Amazon Detective be enabled in {}? Enter [Y/N]: ' .format (region ))
294
304
295
305
if confirm == 'Y' or confirm == 'y' :
296
- logging .info (f'Enabling Amazon Detective in { region } ' )
297
- graphs = [d_client .create_graph ()['GraphArn' ]]
306
+ logging .info (f'Enabling Amazon Detective in { region } ' + ( f'with tags { tags } ' if tags else '' ) )
307
+ graphs = [d_client .create_graph (Tags = tags )['GraphArn' ]]
298
308
else :
299
309
logging .info (f'Skipping { region } ' )
300
310
return None
@@ -326,7 +336,7 @@ def enable_detective(d_client: botocore.client.BaseClient, region: str):
326
336
for region in detective_regions :
327
337
try :
328
338
d_client = master_session .client ('detective' , region_name = region )
329
- graphs = enable_detective (d_client , region )
339
+ graphs = enable_detective (d_client , region , args . tags )
330
340
331
341
if graphs is None :
332
342
continue
0 commit comments