Skip to content

Commit 41d4ef5

Browse files
#999 dns record-list can handle zones with a missing Host record (like SRV records). Improved the zone-list command a bit by adding the records count column
1 parent c1e5501 commit 41d4ef5

File tree

4 files changed

+55
-41
lines changed

4 files changed

+55
-41
lines changed

SoftLayer/CLI/dns/record_list.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,28 @@
1414
@click.argument('zone')
1515
@click.option('--data', help='Record data, such as an IP address')
1616
@click.option('--record', help='Host record, such as www')
17-
@click.option('--ttl',
18-
type=click.INT,
17+
@click.option('--ttl', type=click.INT,
1918
help='TTL value in seconds, such as 86400')
20-
@click.option('--type', help='Record type, such as A or CNAME')
19+
@click.option('--type', 'record_type', help='Record type, such as A or CNAME')
2120
@environment.pass_env
22-
def cli(env, zone, data, record, ttl, type):
21+
def cli(env, zone, data, record, ttl, record_type):
2322
"""List all records in a zone."""
2423

2524
manager = SoftLayer.DNSManager(env.client)
2625
table = formatting.Table(['id', 'record', 'type', 'ttl', 'data'])
27-
28-
table.align['ttl'] = 'l'
29-
table.align['record'] = 'r'
30-
table.align['data'] = 'l'
26+
table.align = 'l'
3127

3228
zone_id = helpers.resolve_id(manager.resolve_ids, zone, name='zone')
3329

34-
records = manager.get_records(zone_id,
35-
record_type=type,
36-
host=record,
37-
ttl=ttl,
38-
data=data)
30+
records = manager.get_records(zone_id, record_type=record_type, host=record, ttl=ttl, data=data)
3931

4032
for the_record in records:
4133
table.add_row([
42-
the_record['id'],
43-
the_record['host'],
44-
the_record['type'].upper(),
45-
the_record['ttl'],
46-
the_record['data']
34+
the_record.get('id'),
35+
the_record.get('host', ''),
36+
the_record.get('type').upper(),
37+
the_record.get('ttl'),
38+
the_record.get('data')
4739
])
4840

4941
env.fout(table)

SoftLayer/CLI/dns/zone_list.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import SoftLayer
77
from SoftLayer.CLI import environment
88
from SoftLayer.CLI import formatting
9+
from SoftLayer.utils import clean_time
910

1011

1112
@click.command()
@@ -14,17 +15,22 @@ def cli(env):
1415
"""List all zones."""
1516

1617
manager = SoftLayer.DNSManager(env.client)
17-
zones = manager.list_zones()
18-
table = formatting.Table(['id', 'zone', 'serial', 'updated'])
19-
table.align['serial'] = 'c'
20-
table.align['updated'] = 'c'
21-
18+
objectMask = "mask[id,name,serial,updateDate,resourceRecordCount]"
19+
zones = manager.list_zones(mask=objectMask)
20+
table = formatting.Table(['id', 'zone', 'serial', 'updated', 'records'])
21+
table.align = 'l'
2222
for zone in zones:
23+
zone_serial = str(zone.get('serial'))
24+
zone_date = zone.get('updateDate', None)
25+
if zone_date is None:
26+
# The serial is just YYYYMMDD##, and since there is no createDate, just format it like it was one.
27+
zone_date = "{}-{}-{}T00:00:00-06:00".format(zone_serial[0:4], zone_serial[4:6], zone_serial[6:8])
2328
table.add_row([
2429
zone['id'],
2530
zone['name'],
26-
zone['serial'],
27-
zone['updateDate'],
31+
zone_serial,
32+
clean_time(zone_date),
33+
zone.get('resourceRecordCount', 0)
2834
])
2935

3036
env.fout(table)

SoftLayer/managers/dns.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ def list_zones(self, **kwargs):
3939
:returns: A list of dictionaries representing the matching zones.
4040
4141
"""
42-
return self.client['Account'].getDomains(**kwargs)
42+
if kwargs.get('iter') is None:
43+
kwargs['iter'] = True
44+
return self.client.call('SoftLayer_Account', 'getDomains', **kwargs)
45+
# return self.client['Account'].getDomains(**kwargs)
4346

4447
def get_zone(self, zone_id, records=True):
4548
"""Get a zone and its records.
@@ -181,8 +184,7 @@ def get_record(self, record_id):
181184
"""
182185
return self.record.getObject(id=record_id)
183186

184-
def get_records(self, zone_id, ttl=None, data=None, host=None,
185-
record_type=None):
187+
def get_records(self, zone_id, ttl=None, data=None, host=None, record_type=None):
186188
"""List, and optionally filter, records within a zone.
187189
188190
:param zone: the zone name in which to search.
@@ -191,8 +193,7 @@ def get_records(self, zone_id, ttl=None, data=None, host=None,
191193
:param str host: record's host
192194
:param str record_type: the type of record
193195
194-
:returns: A list of dictionaries representing the matching records
195-
within the specified zone.
196+
:returns: A list of dictionaries representing the matching records within the specified zone.
196197
"""
197198
_filter = utils.NestedDict()
198199

@@ -208,12 +209,9 @@ def get_records(self, zone_id, ttl=None, data=None, host=None,
208209
if record_type:
209210
_filter['resourceRecords']['type'] = utils.query_filter(record_type.lower())
210211

211-
results = self.service.getResourceRecords(
212-
id=zone_id,
213-
mask='id,expire,domainId,host,minimum,refresh,retry,mxPriority,ttl,type,data,responsiblePerson',
214-
filter=_filter.to_dict(),
215-
)
216-
212+
objectMask = 'id,expire,domainId,host,minimum,refresh,retry,mxPriority,ttl,type,data,responsiblePerson'
213+
results = self.client.call('SoftLayer_Dns_Domain', 'getResourceRecords', id=zone_id,
214+
mask=objectMask, filter=_filter.to_dict(), iter=True)
217215
return results
218216

219217
def edit_record(self, record):

tests/CLI/modules/dns_tests.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,8 @@ def test_list_zones(self):
5454
result = self.run_command(['dns', 'zone-list'])
5555

5656
self.assert_no_fail(result)
57-
self.assertEqual(json.loads(result.output),
58-
[{'serial': 2014030728,
59-
'updated': '2014-03-07T13:52:31-06:00',
60-
'id': 12345,
61-
'zone': 'example.com'}])
57+
actual_output = json.loads(result.output)
58+
self.assertEqual(actual_output[0]['zone'], 'example.com')
6259

6360
def test_list_records(self):
6461
result = self.run_command(['dns', 'record-list', '1234'])
@@ -261,3 +258,24 @@ def test_import_zone(self):
261258
self.assertEqual(call.args[0], expected_call)
262259

263260
self.assertIn("Finished", result.output)
261+
262+
def test_issues_999(self):
263+
"""Makes sure certain zones with a None host record are pritable"""
264+
265+
# SRV records can have a None `host` record, or just a plain missing one.
266+
fake_records = [
267+
{
268+
'data': '1.2.3.4',
269+
'id': 137416416,
270+
'ttl': 900,
271+
'type': 'srv'
272+
}
273+
]
274+
record_mock = self.set_mock('SoftLayer_Dns_Domain', 'getResourceRecords')
275+
record_mock.return_value = fake_records
276+
result = self.run_command(['dns', 'record-list', '1234'])
277+
278+
self.assert_no_fail(result)
279+
actual_output = json.loads(result.output)[0]
280+
self.assertEqual(actual_output['id'], 137416416)
281+
self.assertEqual(actual_output['record'], '')

0 commit comments

Comments
 (0)