From 61b1b47a96bb81ab6eb216a4a0988f7682a7fa9f Mon Sep 17 00:00:00 2001 From: olofvndrhr Date: Fri, 10 Nov 2023 17:06:52 +0100 Subject: [PATCH] update readme and make absolute optional --- README.md | 36 +++++++++++++++++++++++----- src/octodns_netbox_dns/__init__.py | 38 ++++++++---------------------- 2 files changed, 40 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 932d34a..83174ec 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,45 @@ -# +# netbox-plugin-dns source for octodns + +> Works with https://github.com/peteeckel/netbox-plugin-dns ## config ```yml providers: - netbox: + config: class: octodns_netbox_dns.NetBoxDNSSource # Netbox url + # [mandatory, default=null] url: "https://some-url" # Netbox api token + # [mandatory, default=null] token: env/NETBOX_API_KEY - # Provider 'view' configuration is optional; however, it still can - # be declared as "null" or with an empty value. If you don't want to - # set a view in the query, set the value to "false". + # View of the zone. Can be either a string (the view name) or "null" + # to only query zones without a view. Set to false to ignore views + # [optional, default=false] view: false # When records sourced from multiple providers, allows provider - # to replace entries comming from the previous one. + # to replace entries coming from the previous one. # Implementation matches YamlProvider's 'populate_should_replace' + # [optional, default=false] replace_duplicates: false + # Make CNAME, MX and SRV records absolute if they are missing the trailing "." + # [optional, default=false] + make_absolute: false +``` + +## install + +### via pip + +```bash +pip install octodns-netbox-dns@git+https://github.com/olofvndrhr/octodns-netbox-dns.git@main +``` + +### via pip + `requirements.txt` + +add the following line to your requirements file + +```bash +octodns-netbox-dns@git+https://github.com/olofvndrhr/octodns-netbox-dns.git@main ``` diff --git a/src/octodns_netbox_dns/__init__.py b/src/octodns_netbox_dns/__init__.py index b5e31cd..386af44 100644 --- a/src/octodns_netbox_dns/__init__.py +++ b/src/octodns_netbox_dns/__init__.py @@ -11,32 +11,7 @@ import pynetbox.core.api import pynetbox.core.response as pynb_resp -def make_absolute(value: str) -> str: - if value[-1] == ".": - return value - return value + "." - - class NetBoxDNSSource(octodns.provider.base.BaseProvider): - """ - NetBoxDNS source for OctoDNS. - - config: - class: octodns_netbox_dns.NetBoxDNSSource - # Netbox url - url: "https://some-url" - # Netbox api token - token: env/NETBOX_API_KEY - # Provider 'view' configuration is optional; however, it still can - # be declared as "null" or with an empty value. If you don't want to - # set a view in the query, set the value to "false". - view: false - # When records sourced from multiple providers, allows provider - # to replace entries comming from the previous one. - # Implementation matches YamlProvider's 'populate_should_replace' - replace_duplicates: false - """ - SUPPORTS_GEO: bool = False SUPPORTS_DYNAMIC: bool = False SUPPORTS: set[str] = { @@ -82,6 +57,7 @@ class NetBoxDNSSource(octodns.provider.base.BaseProvider): view: str | None | Literal[False] = False, ttl=3600, replace_duplicates: bool = False, + make_absolute: bool = False, ): """Initialize the NetboxDNSSource.""" self.log = logging.getLogger(f"NetboxDNSSource[{id}]") @@ -93,6 +69,12 @@ class NetBoxDNSSource(octodns.provider.base.BaseProvider): self._nb_view = {} if view is False else self._get_view(view) self._ttl = ttl self.replace_duplicates = replace_duplicates + self.make_absolute = make_absolute + + def _make_absolute(self, value: str) -> str: + if not self.make_absolute or value[-1] == ".": + return value + return value + "." def _get_view(self, view: str | None) -> dict[str, int | str]: if view is None: @@ -150,7 +132,7 @@ class NetBoxDNSSource(octodns.provider.base.BaseProvider): value = rdata.address case "CNAME": - value = make_absolute(rdata.target.to_text()) + value = self._make_absolute(rdata.target.to_text()) case "DNAME" | "NS" | "PTR": value = rdata.target.to_text() @@ -181,7 +163,7 @@ class NetBoxDNSSource(octodns.provider.base.BaseProvider): case "MX": value = { "preference": rdata.preference, - "exchange": make_absolute(rdata.exchange.to_text()), + "exchange": self._make_absolute(rdata.exchange.to_text()), } case "NAPTR": @@ -213,7 +195,7 @@ class NetBoxDNSSource(octodns.provider.base.BaseProvider): "priority": rdata.priority, "weight": rdata.weight, "port": rdata.port, - "target": make_absolute(rdata.target.to_text()), + "target": self._make_absolute(rdata.target.to_text()), } case _: