Skip to content

Commit 52e6fb7

Browse files
committed
Issue #5: refactoring: Use dry_run in root.
1 parent 145427a commit 52e6fb7

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

gcp_block_country.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import itertools
55
import subprocess
66
import sys
7-
from functools import partial
7+
from collections.abc import Iterator
88
from math import ceil
99
from pathlib import Path
1010

@@ -31,7 +31,13 @@ def main():
3131
print(f'Total rules being created: {ceil(total_count / CHUNK_SIZE)}')
3232

3333
prefix = COUNTRIES[country_code]
34-
create_rules(prefix, addresses, dry_run=dry_run, gcp_project=gcp_project)
34+
args_list = create_rules(prefix, addresses, gcp_project=gcp_project)
35+
36+
for args in args_list:
37+
if dry_run:
38+
print(f'Run: {" ".join(args)}')
39+
else:
40+
run(args)
3541

3642

3743
def get_args():
@@ -44,23 +50,23 @@ def get_args():
4450
return parser.parse_args()
4551

4652

47-
def create_rules(prefix: str, addresses: list[str], *, dry_run: bool, gcp_project: str):
53+
def create_rules(
54+
prefix: str, addresses: list[str], *, gcp_project: str
55+
) -> Iterator[list[str]]:
4856
"""ファイヤウォールルールを複数件まとめて作成する"""
49-
create_rule_ = partial(create_rule, dry_run=dry_run, gcp_project=gcp_project)
50-
5157
for n in itertools.count():
5258
start = n * CHUNK_SIZE
5359
stop = start + CHUNK_SIZE
5460
chunk_addresses = addresses[start:stop]
5561
if not chunk_addresses:
5662
break
5763

58-
create_rule_(f'{prefix}{n}', chunk_addresses)
64+
yield create_rule(f'{prefix}{n}', chunk_addresses, gcp_project=gcp_project)
5965

6066

61-
def create_rule(name: str, addresses: list[str], *, dry_run: bool, gcp_project: str):
67+
def create_rule(name: str, addresses: list[str], *, gcp_project: str) -> list[str]:
6268
"""ファイヤウォールルールを 1 件作成する"""
63-
args = [
69+
return [
6470
'gcloud',
6571
'compute',
6672
'firewall-rules',
@@ -74,11 +80,6 @@ def create_rule(name: str, addresses: list[str], *, dry_run: bool, gcp_project:
7480
'--no-enable-logging',
7581
f'--source-ranges={",".join(addresses)}',
7682
]
77-
if dry_run:
78-
print('Run:', ' '.join(args))
79-
return
80-
81-
return run(args)
8283

8384

8485
def run(args: list[str]):

0 commit comments

Comments
 (0)