Skip to content

Commit 1c0a66a

Browse files
Merge pull request #1335 from allmightyspiff/issues999
Some improvements to the dns commands
2 parents 042b99e + 78ce548 commit 1c0a66a

File tree

6 files changed

+102
-89
lines changed

6 files changed

+102
-89
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+
object_mask = "mask[id,name,serial,updateDate,resourceRecordCount]"
19+
zones = manager.list_zones(mask=object_mask)
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)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
createObject = {'name': 'example.com'}
22
deleteObject = True
33
editObject = True
4+
getObject = {'id': 12345, 'ttl': 7200, 'data': 'd', 'host': 'a', 'type': 'cname'}

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+
object_mask = '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=object_mask, filter=_filter.to_dict(), iter=True)
217215
return results
218216

219217
def edit_record(self, record):

tests/CLI/modules/dns_tests.py

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77
import json
88
import os.path
9+
import sys
910

1011
import mock
1112

@@ -54,11 +55,8 @@ def test_list_zones(self):
5455
result = self.run_command(['dns', 'zone-list'])
5556

5657
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'}])
58+
actual_output = json.loads(result.output)
59+
self.assertEqual(actual_output[0]['zone'], 'example.com')
6260

6361
def test_list_records(self):
6462
result = self.run_command(['dns', 'record-list', '1234'])
@@ -261,3 +259,44 @@ def test_import_zone(self):
261259
self.assertEqual(call.args[0], expected_call)
262260

263261
self.assertIn("Finished", result.output)
262+
263+
def test_issues_999(self):
264+
"""Makes sure certain zones with a None host record are pritable"""
265+
266+
# SRV records can have a None `host` record, or just a plain missing one.
267+
fake_records = [
268+
{
269+
'data': '1.2.3.4',
270+
'id': 137416416,
271+
'ttl': 900,
272+
'type': 'srv'
273+
}
274+
]
275+
record_mock = self.set_mock('SoftLayer_Dns_Domain', 'getResourceRecords')
276+
record_mock.return_value = fake_records
277+
result = self.run_command(['dns', 'record-list', '1234'])
278+
279+
self.assert_no_fail(result)
280+
actual_output = json.loads(result.output)[0]
281+
self.assertEqual(actual_output['id'], 137416416)
282+
self.assertEqual(actual_output['record'], '')
283+
284+
def test_list_zones_no_update(self):
285+
pyversion = sys.version_info
286+
fake_zones = [
287+
{
288+
'name': 'example.com',
289+
'id': 12345,
290+
'serial': 2014030728,
291+
'updateDate': None}
292+
]
293+
domains_mock = self.set_mock('SoftLayer_Account', 'getDomains')
294+
domains_mock.return_value = fake_zones
295+
result = self.run_command(['dns', 'zone-list'])
296+
297+
self.assert_no_fail(result)
298+
actual_output = json.loads(result.output)
299+
if pyversion.major >= 3 and pyversion.minor >= 7:
300+
self.assertEqual(actual_output[0]['updated'], '2014-03-07 00:00')
301+
else:
302+
self.assertEqual(actual_output[0]['updated'], '2014-03-07T00:00:00-06:00')

tests/managers/dns_tests.py

Lines changed: 25 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,19 @@ def test_get_zone(self):
2525
res = self.dns_client.get_zone(12345)
2626

2727
self.assertEqual(res, fixtures.SoftLayer_Dns_Domain.getObject)
28-
self.assert_called_with('SoftLayer_Dns_Domain', 'getObject',
29-
identifier=12345,
30-
mask='mask[resourceRecords]')
28+
self.assert_called_with('SoftLayer_Dns_Domain', 'getObject', identifier=12345, mask='mask[resourceRecords]')
3129

3230
def test_get_zone_without_records(self):
3331
self.dns_client.get_zone(12345, records=False)
3432

35-
self.assert_called_with('SoftLayer_Dns_Domain', 'getObject',
36-
identifier=12345,
37-
mask=None)
33+
self.assert_called_with('SoftLayer_Dns_Domain', 'getObject', identifier=12345, mask=None)
3834

3935
def test_resolve_zone_name(self):
4036
res = self.dns_client._get_zone_id_from_name('example.com')
4137

4238
self.assertEqual([12345], res)
4339
_filter = {"domains": {"name": {"operation": "_= example.com"}}}
44-
self.assert_called_with('SoftLayer_Account', 'getDomains',
45-
filter=_filter)
40+
self.assert_called_with('SoftLayer_Account', 'getDomains', filter=_filter)
4641

4742
def test_resolve_zone_name_no_matches(self):
4843
mock = self.set_mock('SoftLayer_Account', 'getDomains')
@@ -52,36 +47,30 @@ def test_resolve_zone_name_no_matches(self):
5247

5348
self.assertEqual([], res)
5449
_filter = {"domains": {"name": {"operation": "_= example.com"}}}
55-
self.assert_called_with('SoftLayer_Account', 'getDomains',
56-
filter=_filter)
50+
self.assert_called_with('SoftLayer_Account', 'getDomains', filter=_filter)
5751

5852
def test_create_zone(self):
5953
res = self.dns_client.create_zone('example.com', serial='2014110201')
6054

6155
args = ({'serial': '2014110201',
6256
'name': 'example.com',
6357
'resourceRecords': {}},)
64-
self.assert_called_with('SoftLayer_Dns_Domain', 'createObject',
65-
args=args)
58+
self.assert_called_with('SoftLayer_Dns_Domain', 'createObject', args=args)
6659
self.assertEqual(res, {'name': 'example.com'})
6760

6861
def test_delete_zone(self):
6962
self.dns_client.delete_zone(1)
70-
self.assert_called_with('SoftLayer_Dns_Domain', 'deleteObject',
71-
identifier=1)
63+
self.assert_called_with('SoftLayer_Dns_Domain', 'deleteObject', identifier=1)
7264

7365
def test_edit_zone(self):
7466
self.dns_client.edit_zone('example.com')
7567

76-
self.assert_called_with('SoftLayer_Dns_Domain', 'editObject',
77-
args=('example.com',))
68+
self.assert_called_with('SoftLayer_Dns_Domain', 'editObject', args=('example.com',))
7869

7970
def test_create_record(self):
80-
res = self.dns_client.create_record(1, 'test', 'TXT', 'testing',
81-
ttl=1200)
71+
res = self.dns_client.create_record(1, 'test', 'TXT', 'testing', ttl=1200)
8272

83-
self.assert_called_with('SoftLayer_Dns_Domain_ResourceRecord',
84-
'createObject',
73+
self.assert_called_with('SoftLayer_Dns_Domain_ResourceRecord', 'createObject',
8574
args=({
8675
'domainId': 1,
8776
'ttl': 1200,
@@ -94,8 +83,7 @@ def test_create_record(self):
9483
def test_create_record_mx(self):
9584
res = self.dns_client.create_record_mx(1, 'test', 'testing', ttl=1200, priority=21)
9685

97-
self.assert_called_with('SoftLayer_Dns_Domain_ResourceRecord',
98-
'createObject',
86+
self.assert_called_with('SoftLayer_Dns_Domain_ResourceRecord', 'createObject',
9987
args=({
10088
'domainId': 1,
10189
'ttl': 1200,
@@ -110,8 +98,7 @@ def test_create_record_srv(self):
11098
res = self.dns_client.create_record_srv(1, 'record', 'test_data', 'SLS', 8080, 'foobar',
11199
ttl=1200, priority=21, weight=15)
112100

113-
self.assert_called_with('SoftLayer_Dns_Domain_ResourceRecord',
114-
'createObject',
101+
self.assert_called_with('SoftLayer_Dns_Domain_ResourceRecord', 'createObject',
115102
args=({
116103
'complexType': 'SoftLayer_Dns_Domain_ResourceRecord_SrvType',
117104
'domainId': 1,
@@ -130,14 +117,8 @@ def test_create_record_srv(self):
130117
def test_create_record_ptr(self):
131118
res = self.dns_client.create_record_ptr('test', 'testing', ttl=1200)
132119

133-
self.assert_called_with('SoftLayer_Dns_Domain_ResourceRecord',
134-
'createObject',
135-
args=({
136-
'ttl': 1200,
137-
'host': 'test',
138-
'type': 'PTR',
139-
'data': 'testing'
140-
},))
120+
self.assert_called_with('SoftLayer_Dns_Domain_ResourceRecord', 'createObject',
121+
args=({'ttl': 1200, 'host': 'test', 'type': 'PTR', 'data': 'testing'},))
141122
self.assertEqual(res, {'name': 'example.com'})
142123

143124
def test_generate_create_dict(self):
@@ -161,27 +142,21 @@ def test_generate_create_dict(self):
161142
def test_delete_record(self):
162143
self.dns_client.delete_record(1)
163144

164-
self.assert_called_with('SoftLayer_Dns_Domain_ResourceRecord',
165-
'deleteObject',
166-
identifier=1)
145+
self.assert_called_with('SoftLayer_Dns_Domain_ResourceRecord', 'deleteObject', identifier=1)
167146

168147
def test_edit_record(self):
169148
self.dns_client.edit_record({'id': 1, 'name': 'test', 'ttl': '1800'})
170149

171-
self.assert_called_with('SoftLayer_Dns_Domain_ResourceRecord',
172-
'editObject',
173-
args=({'id': 1,
174-
'name': 'test',
175-
'ttl': '1800'},),
150+
self.assert_called_with('SoftLayer_Dns_Domain_ResourceRecord', 'editObject',
151+
args=({'id': 1, 'name': 'test', 'ttl': '1800'},),
176152
identifier=1)
177153

178154
def test_dump_zone(self):
179155
self.dns_client.dump_zone(1)
180156

181-
self.assert_called_with('SoftLayer_Dns_Domain', 'getZoneFileContents',
182-
identifier=1)
157+
self.assert_called_with('SoftLayer_Dns_Domain', 'getZoneFileContents', identifier=1)
183158

184-
def test_get_record(self):
159+
def test_get_records(self):
185160
# maybe valid domain, but no records matching
186161
mock = self.set_mock('SoftLayer_Dns_Domain', 'getResourceRecords')
187162
mock.return_value = []
@@ -190,11 +165,7 @@ def test_get_record(self):
190165
mock.reset_mock()
191166
records = fixtures.SoftLayer_Dns_Domain.getResourceRecords
192167
mock.return_value = [records[0]]
193-
self.dns_client.get_records(12345,
194-
record_type='a',
195-
host='hostname',
196-
data='a',
197-
ttl='86400')
168+
self.dns_client.get_records(12345, record_type='a', host='hostname', data='a', ttl='86400')
198169

199170
_filter = {'resourceRecords': {'type': {'operation': '_= a'},
200171
'host': {'operation': '_= hostname'},
@@ -203,3 +174,9 @@ def test_get_record(self):
203174
self.assert_called_with('SoftLayer_Dns_Domain', 'getResourceRecords',
204175
identifier=12345,
205176
filter=_filter)
177+
178+
def test_get_record(self):
179+
record_id = 1234
180+
record = self.dns_client.get_record(record_id)
181+
self.assertEqual(record['id'], 12345)
182+
self.assert_called_with('SoftLayer_Dns_Domain_ResourceRecord', 'getObject', identifier=record_id)

0 commit comments

Comments
 (0)