Skip to content

Commit 6769d8c

Browse files
issues192 added ability to sync aaaa records
1 parent 50c60bd commit 6769d8c

2 files changed

Lines changed: 85 additions & 4 deletions

File tree

SoftLayer/CLI/virt/dns.py

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,43 @@
1818
@click.option('--a-record', '-a',
1919
is_flag=True,
2020
help="Sync the A record for the host")
21+
@click.option('--aaaa-record',
22+
is_flag=True,
23+
help="Sync the AAAA record for the host")
2124
@click.option('--ptr', is_flag=True, help="Sync the PTR record for the host")
2225
@click.option('--ttl',
2326
default=7200,
2427
show_default=True,
2528
type=click.INT,
2629
help="Sets the TTL for the A and/or PTR records")
2730
@environment.pass_env
28-
def cli(env, identifier, a_record, ptr, ttl):
31+
def cli(env, identifier, a_record, aaaa_record, ptr, ttl):
2932
"""Sync DNS records."""
3033

34+
items = ['id',
35+
'globalIdentifier',
36+
'fullyQualifiedDomainName',
37+
'hostname',
38+
'domain',
39+
'primaryBackendIpAddress',
40+
'primaryIpAddress',
41+
'''primaryNetworkComponent[
42+
id, primaryIpAddress,
43+
primaryVersion6IpAddressRecord[ipAddress]
44+
]''']
45+
mask = "mask[%s]" % ','.join(items)
3146
dns = SoftLayer.DNSManager(env.client)
3247
vsi = SoftLayer.VSManager(env.client)
3348

3449
vs_id = helpers.resolve_id(vsi.resolve_ids, identifier, 'VS')
35-
instance = vsi.get_instance(vs_id)
50+
instance = vsi.get_instance(vs_id, mask=mask)
3651
zone_id = helpers.resolve_id(dns.resolve_ids,
3752
instance['domain'],
3853
name='zone')
3954

4055
def sync_a_record():
4156
"""Sync A record."""
4257
records = dns.get_records(zone_id, host=instance['hostname'])
43-
4458
if not records:
4559
# don't have a record, lets add one to the base zone
4660
dns.create_record(zone['id'],
@@ -58,6 +72,38 @@ def sync_a_record():
5872
rec['ttl'] = ttl
5973
dns.edit_record(rec)
6074

75+
def sync_aaaa_record():
76+
"""Sync AAAA record."""
77+
records = dns.get_records(zone_id,
78+
host=instance['hostname'],
79+
record_type='aaaa')
80+
81+
try:
82+
# done this way to stay within 80 character lines
83+
component = instance['primaryNetworkComponent']
84+
record = component['primaryVersion6IpAddressRecord']
85+
ip_address = record['ipAddress']
86+
except KeyError:
87+
raise exceptions.CLIAbort("%s does not have an ipv6 address"
88+
% instance['fullyQualifiedDomainName'])
89+
90+
if not records:
91+
# don't have a record, lets add one to the base zone
92+
dns.create_record(zone['id'],
93+
instance['hostname'],
94+
'aaaa',
95+
ip_address,
96+
ttl=ttl)
97+
else:
98+
recs = [x for x in records if x['type'].lower() == 'aaaa']
99+
if len(recs) != 1:
100+
raise exceptions.CLIAbort("Aborting A record sync, found "
101+
"%d A record exists!" % len(recs))
102+
rec = recs[0]
103+
rec['data'] = ip_address
104+
rec['ttl'] = ttl
105+
dns.edit_record(rec)
106+
61107
def sync_ptr_record():
62108
"""Sync PTR record."""
63109
host_rec = instance['primaryIpAddress'].split('.')[-1]
@@ -102,3 +148,6 @@ def sync_ptr_record():
102148

103149
if both or ptr:
104150
sync_ptr_record()
151+
152+
if aaaa_record:
153+
sync_aaaa_record()

tests/CLI/modules/vs_tests.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
:license: MIT, see LICENSE for more details.
66
"""
77
import mock
8-
8+
from pprint import pprint as pp
99
from SoftLayer import testing
1010

1111
import json
@@ -132,3 +132,35 @@ def test_create(self, confirm_mock):
132132
'networkComponents': [{'maxSpeed': '100'}]},)
133133
self.assert_called_with('SoftLayer_Virtual_Guest', 'createObject',
134134
args=args)
135+
136+
@mock.patch('SoftLayer.CLI.formatting.confirm')
137+
def test_dns_sync(self, confirm_mock):
138+
confirm_mock.return_value = True
139+
getReverseDomainRecords = self.set_mock('SoftLayer_Virtual_Guest',
140+
'getReverseDomainRecords')
141+
getReverseDomainRecords.return_value = [{
142+
'networkAddress': '172.16.240.2',
143+
'name': '2.240.16.172.in-addr.arpa',
144+
'resourceRecords': [{'data': 'test.softlayer.com.',
145+
'id': 100,
146+
'host': '2'}],
147+
'updateDate': '2013-09-11T14:36:57-07:00',
148+
'serial': 1234665663,
149+
'id': 123456,
150+
}]
151+
getResourceRecords = self.set_mock('SoftLayer_Dns_Domain',
152+
'getResourceRecords')
153+
getResourceRecords.return_value = [
154+
{'id': 1,
155+
'ttl': 7200,
156+
'data': '172.16.240.2',
157+
'host': 'test',
158+
'type': 'a'}
159+
]
160+
result = self.run_command(['vs', 'dns-sync', '100'])
161+
self.assertEqual(result.exit_code, 0)
162+
result = self.run_command(['vs', 'dns-sync', '--aaaa-record', '100'])
163+
self.assertEqual(result.exit_code, 2)
164+
165+
166+

0 commit comments

Comments
 (0)