|
|
|
@ -19,14 +19,14 @@ from .base import BaseProvider |
|
|
|
|
|
|
|
# Only made for A records. will have to adjust for more generic params types |
|
|
|
class _AzureRecord(object): |
|
|
|
def __init__(self, resource_group_name, record, values=None) |
|
|
|
def __init__(self, resource_group_name, record, values=None): |
|
|
|
self.resource_group_name = resource_group_name |
|
|
|
self.zone_name = record.zone.name |
|
|
|
self.relative_record_set_name = record.name |
|
|
|
self.record_type = record._type |
|
|
|
|
|
|
|
type_name = '{}records'.format(self.record_type) |
|
|
|
class_name = '{}'.format(self.record_type).capitalize() + |
|
|
|
class_name = '{}'.format(self.record_type).capitalize() + \ |
|
|
|
'Record'.format(self.record_type) |
|
|
|
_values = [record._process_values] |
|
|
|
self.params = {'ttl':record.ttl or 1800, \ |
|
|
|
@ -75,30 +75,76 @@ class AzureProvider(BaseProvider): |
|
|
|
self._resource_group = resource_group |
|
|
|
|
|
|
|
|
|
|
|
self._azure_zones = None |
|
|
|
self._azure_records = {} # this is populated through populate() |
|
|
|
self._azure_zones = None # will be a dictionary. key: name. val: id. |
|
|
|
self._azure_records = None # will be dict by octodns record, az record |
|
|
|
|
|
|
|
self._supported_types = ['A'] |
|
|
|
|
|
|
|
# TODO: health checks a la route53. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: add support for all types. First skeleton: add A. |
|
|
|
def supports(self, record): |
|
|
|
return record._type == 'A' |
|
|
|
# TODO: possibly refactor |
|
|
|
return record._type in self._supported_types |
|
|
|
|
|
|
|
@property |
|
|
|
def azure_zones(self): |
|
|
|
# TODO: return zones. will be created by populate() |
|
|
|
if self._azure_zones is None: |
|
|
|
self.log.debug('azure_zones: loading') |
|
|
|
zones = {} |
|
|
|
for zone in self._dns_client.zones.list(): |
|
|
|
zones[zone['name']] = zone['id'] |
|
|
|
self._azure_zones = zones |
|
|
|
return self._azure_zones |
|
|
|
|
|
|
|
# Given a zone name, returns the zone id. If DNE, creates it. |
|
|
|
def _get_zone_id(self, name): |
|
|
|
|
|
|
|
def _get_zone_id(self, name, create=False): |
|
|
|
self.log.debug('_get_zone_id: name=%s', name) |
|
|
|
if name in self.azure_zones: |
|
|
|
id = self.azure_zones[name] |
|
|
|
self.log.debug('_get_zone_id: id=%s', id) |
|
|
|
return id |
|
|
|
if create: |
|
|
|
#TODO |
|
|
|
return None |
|
|
|
|
|
|
|
# Create a dictionary of record objects by zone and octodns record names |
|
|
|
# TODO: add geo parsing |
|
|
|
def populate(self, zone, target): |
|
|
|
self._azure_records = {} |
|
|
|
self.log.debug('populate: name=%s', zone.name) |
|
|
|
before = len(zone.records) |
|
|
|
|
|
|
|
for record in zone.records: |
|
|
|
zone_id = self._get_zone_id(zone.name) |
|
|
|
if zone_id: |
|
|
|
records = defaultdict(list) |
|
|
|
for type in self._supported_types: |
|
|
|
for azrecord in self.dns_client.record_sets.list_by_type(self._resource_group, zone.name, type): |
|
|
|
record_name = azrecord.name |
|
|
|
data = getattr(self, '_data_for_{}'.format(type))(type, azrecord) |
|
|
|
record = Record.new(zone, record_name, data, source=self) |
|
|
|
zone.add_record(record) |
|
|
|
self._azure_records[record] = _AzureRecord(self._resource_group, record, record.data) |
|
|
|
|
|
|
|
self.log.info('populate: found %s records', len(zone.records)-before) |
|
|
|
|
|
|
|
# might not need |
|
|
|
def _get_type(azrecord): |
|
|
|
azrecord['type'].split('/')[-1] |
|
|
|
|
|
|
|
def _data_for_A(self, type, azrecord): |
|
|
|
return { |
|
|
|
'type': type |
|
|
|
'ttl': azrecord['ttl'], |
|
|
|
'values': [ar.ipv4_address for ar in azrecord.arecords] |
|
|
|
} |
|
|
|
|
|
|
|
def _get_azure_record(record): |
|
|
|
try: |
|
|
|
return self._azure_records[record] |
|
|
|
except: |
|
|
|
raise |
|
|
|
|
|
|
|
def _apply_Create(self, change): |
|
|
|
new = change.new |
|
|
|
|