Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools/c7n_org - exit early on an empty list of accounts or policies #8515

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions tools/c7n_org/c7n_org/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,13 @@ def run(config, use, output_dir, accounts, not_accounts, tags, region,
accounts_config, custodian_config, executor = init(
config, use, debug, verbose, accounts, tags, policy, policy_tags=policy_tags,
not_accounts=not_accounts)
if not (accounts_config["accounts"] and custodian_config["policies"]):
kapilt marked this conversation as resolved.
Show resolved Hide resolved
log.info(
"Targeting accounts: %d, policies: %d. Nothing to do." %
(len(accounts_config["accounts"]), len(custodian_config["policies"]))
)
return

policy_counts = Counter()
success = True

Expand Down
44 changes: 44 additions & 0 deletions tools/c7n_org/tests/test_org.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,47 @@ def test_accounts_iterator(self):
accounts[0]["vars"]["default_tz"] = "Sydney/Australia"
# NOTE allow override at account level
accounts[1]["vars"]["default_tz"] = "UTC"

def test_cli_nothing_to_do(self):
run_dir = self.setup_run_dir()
logger = mock.MagicMock()
run_account = mock.MagicMock()
run_account.return_value = (
{'compute': 24, 'serverless': 12}, True)
self.patch(org, 'logging', logger)
self.patch(org, 'run_account', run_account)
self.change_cwd(run_dir)
log_output = self.capture_logging('c7n_org')
runner = CliRunner()

cli_args = [
'run', '-c', 'accounts.yml', '-u', 'policies.yml',
'--debug', '-s', 'output', '--cache-path', 'cache',
'--metrics-uri', 'aws://',
]

# No policies to run
result = runner.invoke(
org.cli,
cli_args + ['--policytags', 'nonsense'],
catch_exceptions=False
)
self.assertEqual(result.exit_code, 0)
self.assertEqual(
log_output.getvalue().strip(),
"Targeting accounts: 2, policies: 0. Nothing to do.",
)

# No accounts to run against
log_output.truncate(0)
log_output.seek(0)
result = runner.invoke(
org.cli,
cli_args + ['--tags', 'nonsense'],
catch_exceptions=False
)
self.assertEqual(result.exit_code, 0)
self.assertEqual(
log_output.getvalue().strip(),
"Targeting accounts: 0, policies: 2. Nothing to do.",
)