-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathquery_configservice_aggregate.py
69 lines (52 loc) · 2.25 KB
/
query_configservice_aggregate.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
"""
Using select_aggregate_resource_config to query resources through Aggregator.
You can optionally provide path to sql files through argument, if not, you will be prompted
to select from a list of predefined SQL files from the default directory.
"""
import glob
import logging
from os.path import basename, dirname, exists, join
from shutil import rmtree
import click
from boto3.session import Session
from helper.selector import prompt_single_selection
logging.getLogger().setLevel(logging.DEBUG)
DEFAULT_SQLFILE_ROOT = join(dirname(__file__).split(".")[0], "sql_files", "aggregate")
def select_sql_file(sql_files_dir=DEFAULT_SQLFILE_ROOT):
files = glob.glob(f"{sql_files_dir}/*")
return prompt_single_selection("SqlFile", options=files)
def process_request(aggregator, sql_file, profile, region):
sql_statement = None
with open(sql_file, "r") as f:
sql_statement = f.read()
output_filename = f'{basename(sql_file).replace(".sql", "")}.txt'
if exists(output_filename):
rmtree(output_filename)
session = Session(profile_name=profile)
client = session.client("config", region_name=region)
params = {
"Expression": sql_statement,
"ConfigurationAggregatorName": aggregator,
}
while True:
resp = client.select_aggregate_resource_config(**params)
for item in resp["Results"]:
dump(item, output_filename)
if resp.get("NextToken") is None:
break
params["NextToken"] = resp.get("NextToken")
def dump(data, output_filename, to_console=True):
if to_console is True:
print(data)
with open(output_filename, "a") as f:
f.write(f'{data}\n')
@click.command()
@click.option("--aggregator", "-a", required=True, help="Name of the configuration aggregator")
@click.option("--sqlfilesdir", "-d", default=DEFAULT_SQLFILE_ROOT, help="Directory of the SQL files")
@click.option("--profile", "-p", default="default", show_default=True, help="AWS profile name")
@click.option("--region", "-r", default="ap-southeast-2", show_default=True, help="AWS Region")
def main(aggregator, sqlfilesdir, profile, region):
sql_file = select_sql_file(sqlfilesdir)
process_request(aggregator, sql_file, profile, region)
if __name__ == "__main__":
main()