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

Commit 6bd1f85

Browse files
authored
Merge pull request #295 from zalando-stups/fix-domains-value
Fix senza domains value column for Alias Records
2 parents 6bb5265 + a8a7214 commit 6bd1f85

File tree

3 files changed

+78
-67
lines changed

3 files changed

+78
-67
lines changed

senza/cli.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
from .components.stups_auto_configuration import find_taupage_image
3737
from .error_handling import HandleExceptions
3838
from .exceptions import VPCError
39-
from .manaus.route53 import Route53
39+
from .manaus.route53 import Route53, Route53Record
4040
from .patch import patch_auto_scaling_group
4141
from .respawn import get_auto_scaling_group, respawn_auto_scaling_group
4242
from .stups.piu import Piu
@@ -1009,10 +1009,13 @@ def domains(stack_ref, region, output, w, watch):
10091009
if res.resource_type == 'AWS::Route53::RecordSet':
10101010
name = res.physical_resource_id
10111011
if name not in records_by_name:
1012-
zone_name = name.split('.', 1)[1]
1012+
hosted_zone = next(Route53.get_hosted_zones(name))
1013+
zone_name = hosted_zone.domain_name
10131014
for rec in get_records(zone_name):
1014-
records_by_name[(rec['Name'].rstrip('.'), rec.get('SetIdentifier'))] = rec
1015-
record = records_by_name.get((name, stack.StackName)) or records_by_name.get((name, None))
1015+
record = Route53Record.from_boto_dict(rec)
1016+
records_by_name[(rec['Name'].rstrip('.'), rec.get('SetIdentifier'))] = record
1017+
record = (records_by_name.get((name, stack.StackName)) or
1018+
records_by_name.get((name, None))) # type: Route53Record
10161019
row = {'stack_name': stack.name,
10171020
'version': stack.version,
10181021
'resource_id': res.logical_id,
@@ -1022,9 +1025,18 @@ def domains(stack_ref, region, output, w, watch):
10221025
'value': None,
10231026
'create_time': calendar.timegm(res.last_updated_timestamp.timetuple())}
10241027
if record:
1025-
row.update({'weight': str(record.get('Weight', '')),
1026-
'type': record.get('Type'),
1027-
'value': ','.join([r['Value'] for r in record.get('ResourceRecords', [])])})
1028+
if record.resource_records:
1029+
value = ','.join([r['Value']
1030+
for r in record.resource_records])
1031+
elif record.alias_target:
1032+
value = record.alias_target['DNSName']
1033+
else:
1034+
value = ''
1035+
row.update({'weight': (str(record.weight)
1036+
if record.weight is not None
1037+
else ''),
1038+
'type': record.type.value,
1039+
'value': value})
10281040
rows.append(row)
10291041

10301042
with OutputFormat(output):

tests/fixtures.py

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,40 @@
9797
@pytest.fixture
9898
def boto_client(monkeypatch):
9999
mocks = defaultdict(lambda: MagicMock())
100+
mocks['route53'] = MagicMock()
101+
mocks['route53'].list_hosted_zones.return_value = {
102+
'HostedZones': [HOSTED_ZONE_ZO_NE],
103+
'IsTruncated': False,
104+
'MaxItems': '100'}
105+
mocks['route53'].list_resource_record_sets.return_value = {
106+
'IsTruncated': False,
107+
'MaxItems': '100',
108+
'ResourceRecordSets': [
109+
{'Name': 'example.org.',
110+
'ResourceRecords': [{'Value': 'ns.awsdns.com.'},
111+
{'Value': 'ns.awsdns.org.'}],
112+
'TTL': 172800,
113+
'Type': 'NS'},
114+
{'Name': 'test-1.example.org.',
115+
'ResourceRecords': [
116+
{'Value': 'test-1-123.myregion.elb.amazonaws.com'}],
117+
'TTL': 20,
118+
'Type': 'CNAME'},
119+
{'Name': 'mydomain.example.org.',
120+
'ResourceRecords': [{'Value': 'test-1.example.org'}],
121+
'SetIdentifier': 'test-1',
122+
'TTL': 20,
123+
'Type': 'CNAME',
124+
'Weight': 20},
125+
{'Name': 'test-2.example.org.',
126+
'AliasTarget': {'DNSName': 'test-2-123.myregion.elb.amazonaws.com'},
127+
'TTL': 20,
128+
'Type': 'A'},
129+
]}
130+
100131
def my_client(rtype, *args, **kwargs):
101132
if rtype == 'route53':
102-
route53 = mocks['route53']
103-
route53.list_hosted_zones.return_value = {
104-
'HostedZones': [HOSTED_ZONE_ZO_NE],
105-
'IsTruncated': False,
106-
'MaxItems': '100'}
107-
return route53
133+
return mocks['route53']
108134
elif rtype == 'acm':
109135
acm = mocks['acm']
110136
summary_list = {'CertificateSummaryList': [
@@ -122,6 +148,9 @@ def my_client(rtype, *args, **kwargs):
122148
'ResourceType': 'AWS::IAM::Role',
123149
'PhysicalResourceId': 'my-referenced-role'}}
124150
cf.describe_stack_resource.return_value = resource
151+
cf.list_stacks.return_value = {
152+
'StackSummaries': [{'StackName': 'test-1',
153+
'CreationTime': '2016-06-14'}]}
125154
return cf
126155
return mocks[rtype]
127156

@@ -132,6 +161,27 @@ def my_client(rtype, *args, **kwargs):
132161
@pytest.fixture
133162
def boto_resource(monkeypatch):
134163
def my_resource(rtype, *args):
164+
if rtype == 'cloudformation':
165+
res = MagicMock()
166+
res.resource_type = 'AWS::Route53::RecordSet'
167+
res.physical_resource_id = 'test-1.example.org'
168+
res.logical_id = 'VersionDomain'
169+
res.last_updated_timestamp = datetime.now()
170+
res2 = MagicMock()
171+
res2.resource_type = 'AWS::Route53::RecordSet'
172+
res2.physical_resource_id = 'mydomain.example.org'
173+
res2.logical_id = 'MainDomain'
174+
res2.last_updated_timestamp = datetime.now()
175+
res3 = MagicMock()
176+
res3.resource_type = 'AWS::Route53::RecordSet'
177+
res3.physical_resource_id = 'test-2.example.org'
178+
res3.logical_id = 'VersionDomain'
179+
res3.last_updated_timestamp = datetime.now()
180+
stack = MagicMock()
181+
stack.resource_summaries.all.return_value = [res, res2, res3]
182+
cf = MagicMock()
183+
cf.Stack.return_value = stack
184+
return cf
135185
if rtype == 'ec2':
136186
ec2 = MagicMock()
137187
ec2.security_groups.filter.return_value = [

tests/test_cli.py

Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -795,63 +795,11 @@ def my_client(rtype, *args):
795795
assert 'Resource Type' in result.output
796796

797797

798-
def test_domains(monkeypatch):
798+
def test_domains(monkeypatch, boto_resource, boto_client):
799799
senza.traffic.DNS_ZONE_CACHE = {}
800800
senza.traffic.DNS_RR_CACHE = {}
801801

802-
def my_resource(rtype, *args):
803-
if rtype == 'cloudformation':
804-
res = MagicMock()
805-
res.resource_type = 'AWS::Route53::RecordSet'
806-
res.physical_resource_id = 'test-1.example.org'
807-
res.logical_id = 'VersionDomain'
808-
res.last_updated_timestamp = datetime.datetime.now()
809-
res2 = MagicMock()
810-
res2.resource_type = 'AWS::Route53::RecordSet'
811-
res2.physical_resource_id = 'mydomain.example.org'
812-
res2.logical_id = 'MainDomain'
813-
res2.last_updated_timestamp = datetime.datetime.now()
814-
stack = MagicMock()
815-
stack.resource_summaries.all.return_value = [res, res2]
816-
cf = MagicMock()
817-
cf.Stack.return_value = stack
818-
return cf
819-
return MagicMock()
820-
821-
def my_client(rtype, *args):
822-
if rtype == 'cloudformation':
823-
cf = MagicMock()
824-
cf.list_stacks.return_value = {'StackSummaries': [{'StackName': 'test-1',
825-
'CreationTime': '2016-06-14'}]}
826-
return cf
827-
elif rtype == 'route53':
828-
route53 = MagicMock()
829-
route53.list_hosted_zones.return_value = {'HostedZones': [HOSTED_ZONE_EXAMPLE_ORG]}
830-
route53.list_resource_record_sets.return_value = {
831-
'IsTruncated': False,
832-
'MaxItems': '100',
833-
'ResourceRecordSets': [
834-
{'Name': 'example.org.',
835-
'ResourceRecords': [{'Value': 'ns.awsdns.com.'},
836-
{'Value': 'ns.awsdns.org.'}],
837-
'TTL': 172800,
838-
'Type': 'NS'},
839-
{'Name': 'test-1.example.org.',
840-
'ResourceRecords': [{'Value': 'test-1-123.myregion.elb.amazonaws.com'}],
841-
'TTL': 20,
842-
'Type': 'CNAME'},
843-
{'Name': 'mydomain.example.org.',
844-
'ResourceRecords': [{'Value': 'test-1.example.org'}],
845-
'SetIdentifier': 'test-1',
846-
'TTL': 20,
847-
'Type': 'CNAME',
848-
'Weight': 20},
849-
]}
850-
return route53
851-
return MagicMock()
852-
853-
monkeypatch.setattr('boto3.resource', my_resource)
854-
monkeypatch.setattr('boto3.client', my_client)
802+
boto_client['route53'].list_hosted_zones.return_value = {'HostedZones': [HOSTED_ZONE_EXAMPLE_ORG]}
855803

856804
runner = CliRunner()
857805

@@ -865,6 +813,7 @@ def my_client(rtype, *args):
865813
catch_exceptions=False)
866814
assert 'mydomain.example.org' in result.output
867815
assert 'VersionDomain test-1.example.org CNAME test-1-123.myregion.elb.amazonaws.com' in result.output
816+
assert 'VersionDomain test-2.example.org A test-2-123.myregion.elb.amazonaws.com' in result.output
868817
assert 'MainDomain mydomain.example.org 20 CNAME test-1.example.org' in result.output
869818

870819

0 commit comments

Comments
 (0)