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

Commit 9a44e88

Browse files
authored
Merge pull request #428 from zalando-stups/sg-error
Suggest using senza init when SG doesn't exist
2 parents 4028d41 + 2d4e558 commit 9a44e88

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

senza/aws.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from botocore.exceptions import ClientError
1010
from click import FileError
1111

12+
from .exceptions import SecurityGroupNotFound
1213
from .manaus.boto_proxy import BotoClientProxy
1314
from .manaus.utils import extract_client_error_code
1415
from .stack_references import check_file_exceptions
@@ -108,14 +109,14 @@ def resolve_security_group(security_group, region: str):
108109
if isinstance(security_group, dict):
109110
sg = resolve_referenced_resource(security_group, region)
110111
if not sg:
111-
raise ValueError('Referenced Security Group "{}" does not exist'.format(security_group))
112+
raise SecurityGroupNotFound(security_group)
112113
return sg
113114
elif security_group.startswith('sg-'):
114115
return security_group
115116
else:
116117
sg = get_security_group(region, security_group)
117118
if not sg:
118-
raise ValueError('Security Group "{}" does not exist'.format(security_group))
119+
raise SecurityGroupNotFound(security_group)
119120
return sg.id
120121

121122

senza/error_handling.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from raven import Client
1616

1717
from .configuration import configuration
18-
from .exceptions import InvalidDefinition, PiuNotFound
18+
from .exceptions import InvalidDefinition, PiuNotFound, SecurityGroupNotFound
1919
from .manaus.exceptions import (ELBNotFound, HostedZoneNotFound, InvalidState,
2020
RecordNotFound)
2121
from .manaus.utils import extract_client_error_code
@@ -136,6 +136,10 @@ def __call__(self, *args, **kwargs):
136136
except (ELBNotFound, HostedZoneNotFound, RecordNotFound,
137137
InvalidDefinition, InvalidState) as error:
138138
die_fatal_error(error)
139+
except SecurityGroupNotFound as error:
140+
message = ("{}\nRun `senza init` to (re-)create "
141+
"the security group.").format(error)
142+
die_fatal_error(message)
139143
except Exception as unknown_exception:
140144
# Catch All
141145
self.die_unknown_error(unknown_exception)

senza/exceptions.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,15 @@ def __init__(self, path: str, reason: str):
4141
def __str__(self):
4242
return ("{path} is not a valid senza definition: "
4343
"{reason}".format_map(vars(self)))
44+
45+
46+
class SecurityGroupNotFound(SenzaException):
47+
"""
48+
Exception raised when a Security Group is not found
49+
"""
50+
51+
def __init__(self, security_group: str):
52+
self.security_group = security_group
53+
54+
def __str__(self):
55+
return 'Security Group "{}" does not exist.'.format(self.security_group)

tests/test_error_handling.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import senza.error_handling
88
import yaml
99
from pytest import fixture, raises
10-
from senza.exceptions import PiuNotFound
10+
from senza.exceptions import PiuNotFound, SecurityGroupNotFound
1111
from senza.manaus.exceptions import ELBNotFound, InvalidState
1212

1313

@@ -225,6 +225,18 @@ def func():
225225
assert 'Please quote all variable values' in err
226226

227227

228+
def test_sg_not_found(capsys):
229+
func = MagicMock(side_effect=SecurityGroupNotFound('my-app'))
230+
231+
with raises(SystemExit):
232+
senza.error_handling.HandleExceptions(func)()
233+
234+
out, err = capsys.readouterr()
235+
236+
assert err == ('Security Group "my-app" does not exist.\n'
237+
'Run `senza init` to (re-)create the security group.\n')
238+
239+
228240
def test_unknown_error(capsys, mock_tempfile, mock_raven):
229241
senza.error_handling.sentry = senza.error_handling.setup_sentry(None)
230242
func = MagicMock(side_effect=Exception("something"))

0 commit comments

Comments
 (0)