Skip to content

Commit c004a0c

Browse files
committed
improved region validation code. It was regex based, now compares agains actual valid region names.
1 parent 98c6a6b commit c004a0c

File tree

2 files changed

+63
-12
lines changed

2 files changed

+63
-12
lines changed

buildstack.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,25 @@ def checkstackname(region):
8181
raise
8282
except Exception as e:
8383
print("[ERROR]", e)
84-
84+
85+
def validateregion(region):
86+
# validates passed region name.
87+
try:
88+
89+
regionfound = False
90+
91+
for i in regionslist['Regions']:
92+
if (i['RegionName'] == region):
93+
regionfound = True
94+
break
95+
96+
return regionfound
97+
98+
except ClientError as e:
99+
print("[ERROR]",e)
100+
raise
101+
except Exception as e:
102+
print("[ERROR]", e)
85103

86104

87105
def main():
@@ -120,6 +138,10 @@ def main():
120138
# Get the list of regions
121139
regions = args.region_list.split(',')
122140

141+
# Get all possible regions
142+
global regionslist
143+
ec2client = boto3.client('ec2','us-east-1')
144+
regionslist = ec2client.describe_regions()
123145

124146
# validate all passed region names for correctness
125147
if not regions:
@@ -128,11 +150,10 @@ def main():
128150
else:
129151
for region in regions:
130152

131-
regionregex = re.compile(r"^us-[a-z]*-[0-9]{1}")
132-
regionmatch = re.search(regionregex, region)
133-
134-
if not regionmatch:
135-
print ("Please provide a valid region name in region list. For example: us-east-1. Incorrect value", region)
153+
# regionregex = re.compile(r"^us-[a-z]*-[0-9]{1}")
154+
# regionmatch = re.search(regionregex, region)
155+
if not validateregion(region):
156+
print ("Please provide a valid region name in region list. For example: us-east-1. Incorrect region name", region, "was provided.")
136157
sys.exit(1)
137158
elif checkstackname(region):
138159
print ("Stack Name", stackname, "already exists in region", region,". Quitting due to stack name conflict. Please choose another stack name.")

create_managed_endpoint.py

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
from logging import FATAL
23
import string
34
import uuid
45
import argparse
@@ -243,7 +244,24 @@ def get_writer_endpoint(cluname):
243244
print(e)
244245
raise
245246

247+
def validateregion(region):
248+
# validates passed region name.
249+
try:
250+
251+
regionfound = False
252+
253+
for i in regionslist['Regions']:
254+
if (i['RegionName'] == region):
255+
regionfound = True
256+
break
257+
258+
return regionfound
246259

260+
except ClientError as e:
261+
print("[ERROR]",e)
262+
raise
263+
except Exception as e:
264+
print("[ERROR]", e)
247265

248266
def main():
249267
# Main routine
@@ -269,20 +287,32 @@ def main():
269287

270288
# Get the list of regions
271289
regions = args.region_list.split(',')
272-
290+
291+
# Get all possible regions
292+
global regionslist
293+
ec2client = boto3.client('ec2','us-east-1')
294+
regionslist = ec2client.describe_regions()
295+
296+
273297
# validate all passed region names for correctness
274298
if not regions:
275299
print ("Please provide list of regions to build the stack.")
276300
sys.exit(1)
277301
else:
278302
for region in regions:
303+
if not validateregion(region):
304+
print ("Please provide a valid region name in region list. For example: us-east-1. Incorrect region name", region, "was provided.")
305+
sys.exit(1)
306+
307+
308+
309+
# for region in regions:
279310

280-
regionregex = re.compile(r"^us-[a-z]*-[0-9]{1}")
281-
regionmatch = re.search(regionregex, region)
311+
# regionregex = re.compile(r"^us-[a-z]*-[0-9]{1}")
312+
# regionmatch = re.search(regionregex, region)
282313

283-
if not regionmatch:
284-
print ("Please provide a valid region name in region list. For example: us-east-1. Incorrect value", region)
285-
sys.exit(1)
314+
# if not regionmatch:
315+
286316

287317
# If the user didn't pass hosted zone in the expected format, fix it by adding a '.' at the end
288318
strlen = len(hostedzonename)

0 commit comments

Comments
 (0)