Skip to content
This repository was archived by the owner on Oct 23, 2025. It is now read-only.

Commit 373a0a1

Browse files
committed
Merge pull request #209 from zalando-stups/search
Regex based stack matching
2 parents 5db4b24 + 373f145 commit 373a0a1

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

senza/aws.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import base64
2+
import boto3
13
import collections
24
import datetime
35
import functools
6+
import re
47
import time
5-
import boto3
6-
import base64
78
from botocore.exceptions import ClientError
89

910

@@ -240,7 +241,7 @@ def get_stacks(stack_refs: list, region, all=False):
240241

241242

242243
def matches_any(cf_stack_name: str, stack_refs: list):
243-
'''
244+
"""
244245
>>> matches_any(None, [StackReference(name='foobar', version=None)])
245246
False
246247
@@ -255,12 +256,22 @@ def matches_any(cf_stack_name: str, stack_refs: list):
255256
256257
>>> matches_any('foobar-1', [StackReference(name='foobar', version='2')])
257258
False
258-
'''
259+
260+
>>> matches_any('foobar-1', [StackReference(name='foob.r', version='\d')])
261+
True
262+
"""
263+
264+
cf_stack_name = cf_stack_name or '' # ensure cf_stack_name is a str
265+
try:
266+
name, version = cf_stack_name.rsplit('-', 1)
267+
except ValueError:
268+
name = cf_stack_name
269+
version = None
270+
259271
for ref in stack_refs:
260-
if ref.version and cf_stack_name == ref.cf_stack_name():
261-
return True
262-
elif not ref.version and (cf_stack_name or '').rsplit('-', 1)[0] == ref.name:
263-
return True
272+
matches_name = re.match(ref.name + '$', name)
273+
matches_version = not ref.version or re.match(ref.version + '$', version)
274+
return bool(matches_name and matches_version)
264275
return False
265276

266277

senza/cli.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -507,11 +507,9 @@ def get_stack_refs(refs: list):
507507
with open(ref) as fd:
508508
data = yaml.safe_load(fd)
509509
ref = data['SenzaInfo']['StackName']
510-
except Exception as e:
511-
if not STACK_NAME_PATTERN.match(ref):
512-
# we can be sure that ref is a file path,
513-
# as stack names cannot contain dots or slashes
514-
raise click.FileError(ref, str(e))
510+
except (OSError, IOError):
511+
# It's still possible that the ref is a regex
512+
pass
515513

516514
if refs:
517515
version = refs.pop()

0 commit comments

Comments
 (0)