Skip to content

Commit

Permalink
chore: format code
Browse files Browse the repository at this point in the history
  • Loading branch information
neubi4 committed Nov 21, 2024
1 parent 22da63e commit a5e44b6
Showing 1 changed file with 48 additions and 44 deletions.
92 changes: 48 additions & 44 deletions octodns_autodns/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ def zone_create(self, origin, soa, nameservers):
data = {'origin': origin, 'soa': soa, 'nameservers': nameservers}
return self._do_json('POST', '/zone', data=data)['zone']

def zone_update_records(self, zone_name: str, records_add: list[dict], records_remove: list[dict]):
data = {
'adds': records_add,
'rems': records_remove,
}
def zone_update_records(
self,
zone_name: str,
records_add: list[dict],
records_remove: list[dict],
):
data = {'adds': records_add, 'rems': records_remove}
return self._do_json('POST', f'/zone/{zone_name}/_stream', data=data)


Expand All @@ -88,17 +90,24 @@ class AutoDNSProvider(BaseProvider):
)

def __init__(
self,
id,
username,
password,
context,
system_name_servers=["a.ns14.net","b.ns14.net","c.ns14.net","d.ns14.net"],
*args,
**kwargs
self,
id,
username,
password,
context,
system_name_servers=[
"a.ns14.net",
"b.ns14.net",
"c.ns14.net",
"d.ns14.net",
],
*args,
**kwargs,
):
self.log = getLogger(f'AutoDNSProvider[{id}]')
self.log.debug(f"__init__: username={username}, password={password}, context={context}, system_name_servers={system_name_servers}")
self.log.debug(
f"__init__: username={username}, password={password}, context={context}, system_name_servers={system_name_servers}"
)

super().__init__(id, *args, **kwargs)

Expand All @@ -115,12 +124,7 @@ def _data_for_MX(self, _type, records, default_ttl):
for record in records:
preference = record.get('pref')
value = record.get('value')
values.append(
{
'preference': int(preference),
'value': str(value),
}
)
values.append({'preference': int(preference), 'value': str(value)})
return {
'ttl': record.get("ttl", default_ttl),
'type': _type,
Expand All @@ -134,15 +138,15 @@ def _data_for_MULTI(self, _type, records, default_ttl):
return {
'ttl': record.get("ttl", default_ttl),
'type': _type,
'values': values
'values': values,
}

def _data_for_CNAME(self, _type, records, default_ttl):
record = records[0]
return {
'ttl': record.get("ttl", default_ttl),
'type': _type,
'value': record.get('value')
'value': record.get('value'),
}

def _data_for_SRV(self, _type, records, default_ttl):
Expand All @@ -157,7 +161,7 @@ def _data_for_SRV(self, _type, records, default_ttl):
'priority': priority,
'weight': weight,
'port': port,
'target': target
'target': target,
}
)
return {
Expand Down Expand Up @@ -188,27 +192,28 @@ def populate(self, zone: Zone, target=False, lenient=False):

match _type:
case 'MX':
record_data = self._data_for_MX(_type, records, default_ttl)
record_data = self._data_for_MX(
_type, records, default_ttl
)
case 'SRV':
record_data = self._data_for_SRV(_type, records, default_ttl)
record_data = self._data_for_SRV(
_type, records, default_ttl
)
case 'CNAME':
record_data = self._data_for_CNAME(_type, records, default_ttl)
record_data = self._data_for_CNAME(
_type, records, default_ttl
)
case _:
record_data = self._data_for_MULTI(_type, records, default_ttl)
record_data = self._data_for_MULTI(
_type, records, default_ttl
)

record = Record.new(
zone,
name,
record_data,
source=self,
lenient=lenient,
zone, name, record_data, source=self, lenient=lenient
)
zone.add_record(record, lenient=lenient)

self.log.info(
'populate: found %s records', len(zone.records)
)

self.log.info('populate: found %s records', len(zone.records))

def _params_for_multiple(self, record):
for value in record.values:
Expand Down Expand Up @@ -256,9 +261,7 @@ def _params_for_MX(self, record):

def _params_for_SRV(self, record):
for value in record.values:
data = (
f'{value.weight} {value.port} {value.target}'
)
data = f'{value.weight} {value.port} {value.target}'
yield {
'value': data,
'name': record.name,
Expand All @@ -274,24 +277,25 @@ def _apply_Create(self, zone_name, change):
params_for = getattr(self, f'_params_for_{new._type}')

for params in params_for(new):
self.client.zone_update_records(zone_name, records_remove=[], records_add=[params])

self.client.zone_update_records(
zone_name, records_remove=[], records_add=[params]
)

def _apply_Update(self, zone_name, change):
# It's way simpler to delete-then-recreate than to update
self._apply_Delete(zone_name, change)
self._apply_Create(zone_name, change)


def _apply_Delete(self, zone_name, change):
existing = change.existing
zone = existing.zone

params_for = getattr(self, f'_params_for_{existing._type}')

for params in params_for(existing):
self.client.zone_update_records(zone_name, records_add=[], records_remove=[params])

self.client.zone_update_records(
zone_name, records_add=[], records_remove=[params]
)

def _apply(self, plan):
desired = plan.desired
Expand Down

0 comments on commit a5e44b6

Please sign in to comment.