Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate to poetry #2

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 28 additions & 39 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,46 +1,35 @@
name: octoDNS AutoDNSProvider
on: [pull_request, workflow_dispatch]
---
name: tests

on:
push:
pull_request:

jobs:
config:
runs-on: ubuntu-latest
outputs:
json: ${{ steps.load.outputs.json }}
steps:
- id: load
run: |
{
echo 'json<<EOF'
curl -L https://github.com/octodns/octodns/raw/main/.ci-config.json
echo EOF
} >> $GITHUB_OUTPUT
ci:
needs: config
tests:
name: Run tests (py${{ matrix.python }})
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ${{ fromJson(needs.config.outputs.json).python_versions_active }}
steps:
- uses: actions/checkout@v4
- name: Setup python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
architecture: x64
- name: CI Build
run: |
./script/cibuild
setup-py:
needs: config
runs-on: ubuntu-latest
python:
- "3.10"
- "3.11"
- "3.12"
- "3.13"
steps:
- uses: actions/checkout@v4
- name: Setup python
uses: actions/setup-python@v4
- name: Check out code
uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python }}
uses: actions/setup-python@v5
with:
python-version: ${{ fromJson(needs.config.outputs.json).python_version_current }}
architecture: x64
- name: CI setup.py
run: |
./script/cibuild-setup-py
python-version: ${{ matrix.python }}

- name: Install poetry
run: pip install poetry

- name: Install dependencies
run: poetry install

- name: Run tests
run: poetry run tox
13 changes: 0 additions & 13 deletions .github/workflows/stale.yml

This file was deleted.

107 changes: 56 additions & 51 deletions octodns_autodns/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,21 @@
# TODO: remove __VERSION__ with the next major version release
__version__ = __VERSION__ = '0.0.1'


class AutoDNSClientException(ProviderException):
pass


class AutoDNSClientNotFound(AutoDNSClientException):
def __init__(self):
super().__init__('Not Found')


class AutoDNSClientUnauthorized(AutoDNSClientException):
def __init__(self):
super().__init__('Unauthorized')


class AutoDNSClient(object):
BASE_URL = 'https://api.autodns.com/v1'

Expand Down Expand Up @@ -54,18 +58,20 @@ 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)


class AutoDNSProvider(BaseProvider):
SUPPORTS_GEO = False
#SUPPORTS_DYNAMIC = False
#SUPPORTS_ROOT_NS = True
# SUPPORTS_DYNAMIC = False
# SUPPORTS_ROOT_NS = True
SUPPORTS = set(
(
'A',
Expand All @@ -79,31 +85,36 @@ class AutoDNSProvider(BaseProvider):
'MX',
'NS',
'SRV',
'ALIAS'
'ALIAS',
)
)

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)

self.id = id

sess = Session()
sess.headers.update({
"X-Domainrobot-Context": str(context),
})
sess.headers.update({"X-Domainrobot-Context": str(context)})
sess.auth = HTTPBasicAuth(username, password)

self.client = AutoDNSClient(sess, system_name_servers[0])
Expand All @@ -113,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 @@ -132,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 @@ -155,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 @@ -186,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 @@ -254,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 @@ -272,24 +277,24 @@ 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
Loading