Skip to content

Commit

Permalink
Merge pull request #367 from zalando-stups/traffic-on-delete
Browse files Browse the repository at this point in the history
Make senza traffic work even when stacks are being deleted
  • Loading branch information
jmcs committed Sep 29, 2016
2 parents ccbd08f + 3ec29f9 commit e7d0b51
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
15 changes: 15 additions & 0 deletions senza/manaus/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from typing import Dict, Optional # noqa: F401

from botocore.exceptions import ClientError

__all__ = ["extract_client_error_code"]


def extract_client_error_code(exception: ClientError) -> Optional[str]:
"""
Extracts the client error code from a boto ClientError exception. Returns
None if it fails.
"""
error = exception.response.get('Error', {}) # type: Dict[str, Optional[str]]
error_code = error.get('Code')
return error_code
9 changes: 8 additions & 1 deletion senza/traffic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
from clickclick import Action, action, ok, print_table, warning

from .aws import StackReference, get_stacks, get_tag
from .manaus import ClientError
from .manaus.boto_proxy import BotoClientProxy
from .manaus.cloudformation import CloudFormationStack, ResourceType
from .manaus.exceptions import ELBNotFound, StackNotFound, StackNotUpdated
from .manaus.route53 import (RecordType, Route53, Route53HostedZone,
convert_cname_records_to_alias)
from .manaus.utils import extract_client_error_code

PERCENT_RESOLUTION = 2
FULL_PERCENTAGE = PERCENT_RESOLUTION * 100
Expand Down Expand Up @@ -254,7 +256,12 @@ def get_stack_versions(stack_name: str, region: str) -> Iterator[StackVersion]:
for res in details.resource_summaries.all():
if res.resource_type == 'AWS::ElasticLoadBalancing::LoadBalancer':
elb = BotoClientProxy('elb', region)
lbs = elb.describe_load_balancers(LoadBalancerNames=[res.physical_resource_id])
try:
lbs = elb.describe_load_balancers(LoadBalancerNames=[res.physical_resource_id])
except ClientError as e:
error_code = extract_client_error_code(e)
if error_code == 'LoadBalancerNotFound':
continue
lb_dns_name.append(lbs['LoadBalancerDescriptions'][0]['DNSName'])
elif res.resource_type == 'AWS::Route53::RecordSet':
if 'version' not in res.logical_id.lower():
Expand Down
20 changes: 18 additions & 2 deletions tests/test_traffic.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from unittest.mock import MagicMock

import botocore.exceptions
from senza.aws import SenzaStackSummary
from senza.traffic import get_stack_versions, StackVersion, get_weights, resolve_to_ip_addresses
from senza.manaus.route53 import RecordType
from senza.traffic import (StackVersion, get_stack_versions, get_weights,
resolve_to_ip_addresses)


def test_get_stack_versions(monkeypatch):
Expand Down Expand Up @@ -38,7 +41,20 @@ def my_boto3(service, *args):
return_value=[SenzaStackSummary(stack), SenzaStackSummary({'StackStatus': 'ROLLBACK_COMPLETE',
'StackName': 'my-stack-1'})]))
stack_version = list(get_stack_versions('my-stack', 'my-region'))
assert stack_version == [StackVersion('my-stack', '1', ['myapp.example.org'], ['elb-dns-name'], ['some-arn'])]
assert stack_version == [StackVersion('my-stack', '1',
['myapp.example.org'],
['elb-dns-name'],
['some-arn'])]

elb.describe_load_balancers.side_effect = botocore.exceptions.ClientError(
{'Error': {'Code': 'LoadBalancerNotFound'}},
'foobar'
)
stack_version = list(get_stack_versions('my-stack', 'my-region'))
assert stack_version == [StackVersion('my-stack', '1',
['myapp.example.org'],
[],
['some-arn'])]


def test_get_weights(monkeypatch):
Expand Down

0 comments on commit e7d0b51

Please sign in to comment.