Skip to content

Commit

Permalink
Merge branch 'master' into pylint-friday-2017-01
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcs committed Jan 6, 2017
2 parents bb3a05a + 9a44e88 commit c7d2fe0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
5 changes: 3 additions & 2 deletions senza/aws.py
Expand Up @@ -9,6 +9,7 @@
from botocore.exceptions import ClientError
from click import FileError

from .exceptions import SecurityGroupNotFound
from .manaus.boto_proxy import BotoClientProxy
from .manaus.utils import extract_client_error_code
from .stack_references import check_file_exceptions
Expand Down Expand Up @@ -108,14 +109,14 @@ def resolve_security_group(security_group, region: str):
if isinstance(security_group, dict):
sg = resolve_referenced_resource(security_group, region)
if not sg:
raise ValueError('Referenced Security Group "{}" does not exist'.format(security_group))
raise SecurityGroupNotFound(security_group)
return sg
elif security_group.startswith('sg-'):
return security_group
else:
sg = get_security_group(region, security_group)
if not sg:
raise ValueError('Security Group "{}" does not exist'.format(security_group))
raise SecurityGroupNotFound(security_group)
return sg.id


Expand Down
8 changes: 6 additions & 2 deletions senza/error_handling.py
Expand Up @@ -15,7 +15,7 @@
from raven import Client

from .configuration import configuration
from .exceptions import InvalidDefinition, PiuNotFound
from .exceptions import InvalidDefinition, PiuNotFound, SecurityGroupNotFound
from .manaus.exceptions import (ELBNotFound, HostedZoneNotFound, InvalidState,
RecordNotFound)
from .manaus.utils import extract_client_error_code
Expand Down Expand Up @@ -112,7 +112,7 @@ def __call__(self, *args, **kwargs):
sys.stdout.flush()
if is_credentials_expired_error(client_error):
die_fatal_error('AWS credentials have expired.\n'
'Use the "mai" command line tool to get a new '
'Use the "zaws" command line tool to get a new '
'temporary access key.')
elif is_access_denied_error(client_error):
die_fatal_error(
Expand All @@ -136,6 +136,10 @@ def __call__(self, *args, **kwargs):
except (ELBNotFound, HostedZoneNotFound, RecordNotFound,
InvalidDefinition, InvalidState) as error:
die_fatal_error(error)
except SecurityGroupNotFound as error:
message = ("{}\nRun `senza init` to (re-)create "
"the security group.").format(error)
die_fatal_error(message)
except Exception as unknown_exception: # pylint: disable=locally-disabled, broad-except
# Catch All
self.die_unknown_error(unknown_exception)
Expand Down
12 changes: 12 additions & 0 deletions senza/exceptions.py
Expand Up @@ -41,3 +41,15 @@ def __init__(self, path: str, reason: str):
def __str__(self):
return ("{path} is not a valid senza definition: "
"{reason}".format_map(vars(self)))


class SecurityGroupNotFound(SenzaException):
"""
Exception raised when a Security Group is not found
"""

def __init__(self, security_group: str):
self.security_group = security_group

def __str__(self):
return 'Security Group "{}" does not exist.'.format(self.security_group)
14 changes: 13 additions & 1 deletion tests/test_error_handling.py
Expand Up @@ -7,7 +7,7 @@
import senza.error_handling
import yaml
from pytest import fixture, raises
from senza.exceptions import PiuNotFound
from senza.exceptions import PiuNotFound, SecurityGroupNotFound
from senza.manaus.exceptions import ELBNotFound, InvalidState


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


def test_sg_not_found(capsys):
func = MagicMock(side_effect=SecurityGroupNotFound('my-app'))

with raises(SystemExit):
senza.error_handling.HandleExceptions(func)()

out, err = capsys.readouterr()

assert err == ('Security Group "my-app" does not exist.\n'
'Run `senza init` to (re-)create the security group.\n')


def test_unknown_error(capsys, mock_tempfile, mock_raven):
senza.error_handling.sentry = senza.error_handling.setup_sentry(None)
func = MagicMock(side_effect=Exception("something"))
Expand Down

0 comments on commit c7d2fe0

Please sign in to comment.