Skip to content

Commit 10d2c37

Browse files
add options include-accounts-file and exclude-accounts-file to stack-update and generate-csv commands (#23)
1 parent d1d061c commit 10d2c37

File tree

3 files changed

+64
-8
lines changed

3 files changed

+64
-8
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,5 @@ dmypy.json
130130

131131
# prevents from being accidentally committed
132132
user_config.yml
133+
134+
tmp/

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = conformity-migration-tool
3-
version = 0.11.0
3+
version = 0.12.0
44
description = Migrates your visiblity information in cloudconformity.com to cloudone.trendmicro.com
55
long_description = file: README.md
66
long_description_content_type = text/markdown

src/conformity_migration_tool/aws_cli.py

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
UpdateStackOutputTypeDef,
1515
)
1616

17+
from .cli import include_exclude_accts, read_accts_file
1718
from .di import c1_conformity_api, legacy_conformity_api
1819

1920

@@ -24,16 +25,37 @@ class LegacyConformityAWSAccountInfo:
2425
old_external_id: str
2526

2627

27-
def get_legacy_conformity_aws_accounts_info() -> Iterable[
28-
LegacyConformityAWSAccountInfo
29-
]:
28+
def get_legacy_conformity_aws_accounts_info(
29+
include_accounts_file: str, exclude_accounts_file: str
30+
) -> Iterable[LegacyConformityAWSAccountInfo]:
31+
32+
include_accounts = (
33+
read_accts_file(accounts_file=include_accounts_file)
34+
if include_accounts_file
35+
else None
36+
)
37+
exclude_accounts = (
38+
read_accts_file(accounts_file=exclude_accounts_file)
39+
if exclude_accounts_file
40+
else None
41+
)
42+
3043
legacy_api = legacy_conformity_api()
3144
old_external_id = legacy_api.get_organisation_external_id()
3245
accts = [acct for acct in legacy_api.list_accounts() if acct.cloud_type == "aws"]
46+
accts = include_exclude_accts(
47+
legacy_accts=accts,
48+
include_accts=include_accounts,
49+
exclude_accts=exclude_accounts,
50+
)
3351
for acct in accts:
52+
aws_acct_id = acct.attributes.get("awsaccount-id")
53+
if not aws_acct_id:
54+
print(f"Skipping account {acct.name}. It doesn't have awsaccount-id")
55+
continue
3456
yield LegacyConformityAWSAccountInfo(
3557
account_name=acct.name,
36-
aws_account_number=acct.attributes["awsaccount-id"],
58+
aws_account_number=aws_acct_id,
3759
old_external_id=old_external_id,
3860
)
3961

@@ -51,7 +73,19 @@ def cli(ctx):
5173
help="Creates a csv file containing AWS accounts to be used for 'update-stack --csv-file' command option.",
5274
)
5375
@click.argument("csv-file")
54-
def generate_csv(csv_file: str):
76+
@click.option(
77+
"--include-accounts-file",
78+
required=False,
79+
type=str,
80+
help="CSV file containing accounts that will be the only ones included. Each row should consists of 2 fields: first is the account name and second is the environment as they appear on Conformity Dashboard. An empty file means the tool won't include any account.",
81+
)
82+
@click.option(
83+
"--exclude-accounts-file",
84+
required=False,
85+
type=str,
86+
help="CSV file containing accounts that will be excluded. Each row should consists of 2 fields: first is the account name and second is the environment as they appear on Conformity Dashboard.",
87+
)
88+
def generate_csv(csv_file: str, include_accounts_file: str, exclude_accounts_file: str):
5589
print(f"Generating CSV: {csv_file}")
5690
with open(csv_file, newline="", mode="w") as fh:
5791
csvw = csv.DictWriter(
@@ -72,7 +106,10 @@ def generate_csv(csv_file: str):
72106
)
73107
csvw.writeheader()
74108

75-
accts = get_legacy_conformity_aws_accounts_info()
109+
accts = get_legacy_conformity_aws_accounts_info(
110+
include_accounts_file=include_accounts_file,
111+
exclude_accounts_file=exclude_accounts_file,
112+
)
76113
for acct in accts:
77114
csvw.writerow(
78115
{
@@ -177,6 +214,18 @@ def generate_csv(csv_file: str):
177214
default=None,
178215
help="Cross-Account Role name (e.g. OrganizationAccountAccessRole). The role should at least have the permissions necessary to update the Conformity stack.",
179216
)
217+
@click.option(
218+
"--include-accounts-file",
219+
required=False,
220+
type=str,
221+
help="CSV file containing accounts that will be the only ones included. Each row should consists of 2 fields: first is the account name and second is the environment as they appear on Conformity Dashboard. An empty file means the tool won't include any account.",
222+
)
223+
@click.option(
224+
"--exclude-accounts-file",
225+
required=False,
226+
type=str,
227+
help="CSV file containing accounts that will be excluded. Each row should consists of 2 fields: first is the account name and second is the environment as they appear on Conformity Dashboard.",
228+
)
180229
@click.pass_context
181230
def update_stack(
182231
ctx,
@@ -189,6 +238,8 @@ def update_stack(
189238
secret_key: str,
190239
session_token: str,
191240
cross_account_role_name: str,
241+
include_accounts_file: str,
242+
exclude_accounts_file: str,
192243
):
193244
# region = ctx.obj["region"]
194245
# profile = ctx.obj["profile"]
@@ -224,7 +275,10 @@ def update_stack(
224275
aws_session_token=session_token,
225276
cross_account_role_name=cross_account_role_name,
226277
)
227-
for acct in get_legacy_conformity_aws_accounts_info()
278+
for acct in get_legacy_conformity_aws_accounts_info(
279+
include_accounts_file=include_accounts_file,
280+
exclude_accounts_file=exclude_accounts_file,
281+
)
228282
)
229283

230284
accts = list(accts)

0 commit comments

Comments
 (0)