|
1 | 1 | """Sync DNS records.""" |
2 | 2 | # :license: MIT, see LICENSE for more details. |
| 3 | +# pylint: disable=duplicate-code |
3 | 4 |
|
4 | 5 | import click |
5 | 6 |
|
|
15 | 16 | want to update both records, you may use the -a or --ptr arguments to limit |
16 | 17 | the records updated.""") |
17 | 18 | @click.argument('identifier') |
18 | | -@click.option('--a-record', '-a', |
19 | | - is_flag=True, |
20 | | - 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") |
| 19 | +@click.option('--a-record', '-a', is_flag=True, help="Sync the A record for the host") |
| 20 | +@click.option('--aaaa-record', is_flag=True, help="Sync the AAAA record for the host") |
24 | 21 | @click.option('--ptr', is_flag=True, help="Sync the PTR record for the host") |
25 | | -@click.option('--ttl', |
26 | | - default=7200, |
27 | | - show_default=True, |
28 | | - type=click.INT, |
| 22 | +@click.option('--ttl', default=7200, show_default=True, type=click.INT, |
29 | 23 | help="Sets the TTL for the A and/or PTR records") |
30 | 24 | @environment.pass_env |
31 | 25 | def cli(env, identifier, a_record, aaaa_record, ptr, ttl): |
32 | 26 | """Sync DNS records.""" |
33 | 27 |
|
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) |
| 28 | + mask = """mask[id, globalIdentifier, fullyQualifiedDomainName, hostname, domain, |
| 29 | + primaryBackendIpAddress,primaryIpAddress, |
| 30 | + primaryNetworkComponent[id,primaryIpAddress,primaryVersion6IpAddressRecord[ipAddress]]]""" |
46 | 31 | dns = SoftLayer.DNSManager(env.client) |
47 | | - vsi = SoftLayer.VSManager(env.client) |
| 32 | + server = SoftLayer.VSManager(env.client) |
48 | 33 |
|
49 | | - vs_id = helpers.resolve_id(vsi.resolve_ids, identifier, 'VS') |
50 | | - instance = vsi.get_instance(vs_id, mask=mask) |
51 | | - zone_id = helpers.resolve_id(dns.resolve_ids, |
52 | | - instance['domain'], |
53 | | - name='zone') |
54 | | - |
55 | | - def sync_a_record(): |
56 | | - """Sync A record.""" |
57 | | - records = dns.get_records(zone_id, |
58 | | - host=instance['hostname'], |
59 | | - record_type='a') |
60 | | - if not records: |
61 | | - # don't have a record, lets add one to the base zone |
62 | | - dns.create_record(zone['id'], |
63 | | - instance['hostname'], |
64 | | - 'a', |
65 | | - instance['primaryIpAddress'], |
66 | | - ttl=ttl) |
67 | | - else: |
68 | | - if len(records) != 1: |
69 | | - raise exceptions.CLIAbort("Aborting A record sync, found " |
70 | | - "%d A record exists!" % len(records)) |
71 | | - rec = records[0] |
72 | | - rec['data'] = instance['primaryIpAddress'] |
73 | | - rec['ttl'] = ttl |
74 | | - dns.edit_record(rec) |
75 | | - |
76 | | - def sync_aaaa_record(): |
77 | | - """Sync AAAA record.""" |
78 | | - records = dns.get_records(zone_id, |
79 | | - host=instance['hostname'], |
80 | | - record_type='aaaa') |
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 | | - if len(records) != 1: |
99 | | - raise exceptions.CLIAbort("Aborting A record sync, found " |
100 | | - "%d A record exists!" % len(records)) |
101 | | - rec = records[0] |
102 | | - rec['data'] = ip_address |
103 | | - rec['ttl'] = ttl |
104 | | - dns.edit_record(rec) |
105 | | - |
106 | | - def sync_ptr_record(): |
107 | | - """Sync PTR record.""" |
108 | | - host_rec = instance['primaryIpAddress'].split('.')[-1] |
109 | | - ptr_domains = (env.client['Virtual_Guest'] |
110 | | - .getReverseDomainRecords(id=instance['id'])[0]) |
111 | | - edit_ptr = None |
112 | | - for ptr in ptr_domains['resourceRecords']: |
113 | | - if ptr['host'] == host_rec: |
114 | | - ptr['ttl'] = ttl |
115 | | - edit_ptr = ptr |
116 | | - break |
117 | | - |
118 | | - if edit_ptr: |
119 | | - edit_ptr['data'] = instance['fullyQualifiedDomainName'] |
120 | | - dns.edit_record(edit_ptr) |
121 | | - else: |
122 | | - dns.create_record(ptr_domains['id'], |
123 | | - host_rec, |
124 | | - 'ptr', |
125 | | - instance['fullyQualifiedDomainName'], |
126 | | - ttl=ttl) |
| 34 | + server_id = helpers.resolve_id(server.resolve_ids, identifier, 'VS') |
| 35 | + instance = server.get_instance(server_id, mask=mask) |
| 36 | + zone_id = helpers.resolve_id(dns.resolve_ids, instance['domain'], name='zone') |
127 | 37 |
|
128 | 38 | if not instance['primaryIpAddress']: |
129 | | - raise exceptions.CLIAbort('No primary IP address associated with ' |
130 | | - 'this VS') |
131 | | - |
132 | | - zone = dns.get_zone(zone_id) |
| 39 | + raise exceptions.CLIAbort('No primary IP address associated with this VS') |
133 | 40 |
|
134 | 41 | go_for_it = env.skip_confirmations or formatting.confirm( |
135 | | - "Attempt to update DNS records for %s" |
136 | | - % instance['fullyQualifiedDomainName']) |
| 42 | + "Attempt to update DNS records for %s" % instance['fullyQualifiedDomainName']) |
137 | 43 |
|
138 | 44 | if not go_for_it: |
139 | 45 | raise exceptions.CLIAbort("Aborting DNS sync") |
140 | 46 |
|
141 | | - both = False |
142 | | - if not ptr and not a_record and not aaaa_record: |
143 | | - both = True |
| 47 | + # both will be true only if no options are passed in, basically. |
| 48 | + both = (not ptr) and (not a_record) and (not aaaa_record) |
144 | 49 |
|
145 | 50 | if both or a_record: |
146 | | - sync_a_record() |
| 51 | + dns.sync_host_record(zone_id, instance['hostname'], instance['primaryIpAddress'], 'a', ttl) |
147 | 52 |
|
148 | 53 | if both or ptr: |
149 | | - sync_ptr_record() |
| 54 | + # getReverseDomainRecords returns a list of 1 element, so just get the top. |
| 55 | + ptr_domains = env.client['Virtual_Guest'].getReverseDomainRecords(id=instance['id']).pop() |
| 56 | + dns.sync_ptr_record(ptr_domains, instance['primaryIpAddress'], instance['fullyQualifiedDomainName'], ttl) |
150 | 57 |
|
151 | 58 | if aaaa_record: |
152 | | - sync_aaaa_record() |
| 59 | + try: |
| 60 | + # done this way to stay within 80 character lines |
| 61 | + ipv6 = instance['primaryNetworkComponent']['primaryVersion6IpAddressRecord']['ipAddress'] |
| 62 | + dns.sync_host_record(zone_id, instance['hostname'], ipv6, 'aaaa', ttl) |
| 63 | + except KeyError: |
| 64 | + raise exceptions.CLIAbort("%s does not have an ipv6 address" % instance['fullyQualifiedDomainName']) |
0 commit comments