4
4
import itertools
5
5
import subprocess
6
6
import sys
7
- from functools import partial
7
+ from collections . abc import Iterator
8
8
from math import ceil
9
9
from pathlib import Path
10
10
@@ -31,7 +31,13 @@ def main():
31
31
print (f'Total rules being created: { ceil (total_count / CHUNK_SIZE )} ' )
32
32
33
33
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 )
35
41
36
42
37
43
def get_args ():
@@ -44,23 +50,23 @@ def get_args():
44
50
return parser .parse_args ()
45
51
46
52
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 ]]:
48
56
"""ファイヤウォールルールを複数件まとめて作成する"""
49
- create_rule_ = partial (create_rule , dry_run = dry_run , gcp_project = gcp_project )
50
-
51
57
for n in itertools .count ():
52
58
start = n * CHUNK_SIZE
53
59
stop = start + CHUNK_SIZE
54
60
chunk_addresses = addresses [start :stop ]
55
61
if not chunk_addresses :
56
62
break
57
63
58
- create_rule_ (f'{ prefix } { n } ' , chunk_addresses )
64
+ yield create_rule (f'{ prefix } { n } ' , chunk_addresses , gcp_project = gcp_project )
59
65
60
66
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 ] :
62
68
"""ファイヤウォールルールを 1 件作成する"""
63
- args = [
69
+ return [
64
70
'gcloud' ,
65
71
'compute' ,
66
72
'firewall-rules' ,
@@ -74,11 +80,6 @@ def create_rule(name: str, addresses: list[str], *, dry_run: bool, gcp_project:
74
80
'--no-enable-logging' ,
75
81
f'--source-ranges={ "," .join (addresses )} ' ,
76
82
]
77
- if dry_run :
78
- print ('Run:' , ' ' .join (args ))
79
- return
80
-
81
- return run (args )
82
83
83
84
84
85
def run (args : list [str ]):
0 commit comments