Skip to content
Open
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
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ To create A/AAAA records for octoDNS, you need to manage the mapping between IP

Starting with [Netbox v2.6.0](https://github.com/netbox-community/netbox/issues/166), Netbox now has a `dns_name` field in IP address records. But we **do not** use this field by default because this `dns_name` field can only store **single** FQDN. To use a `dns_name` field, set `field_name: dns_name` in [the configuration](#examples).

### Custom fields

You can also use NetBox custom fields to store DNS names. This is useful when you want to keep the `dns_name` field for the primary hostname and use a custom field for additional aliases.

To use a custom field, prefix the field name with `cf_`. For example, if you have a custom field named `additional_dns` on IP addresses, configure it as:

```yaml
providers:
netbox-aliases:
class: octodns_netbox.NetboxSource
url: https://ipam.example.com
token: env/NETBOX_TOKEN
field_name: cf_additional_dns
```

The custom field should contain comma-separated FQDNs, just like the `description` field.

### PTR records

`octodns-netbox` also supports PTR records. By default, only the first FQDN in the field is used to generate the PTR record, but you can enable multiple PTR records for a single IP by setting the `multivalue_ptr` parameter to `true` in [the configuration](#examples).
Expand Down Expand Up @@ -92,6 +109,10 @@ providers:
# The `description` does not have any limitations so by default
# we use the `description` field to store multiple FQDNs, separated by commas.
# Other tested values are `dns_name`.
#
# Custom fields are also supported by using the `cf_` prefix.
# For example, if you have a custom field named `additional_dns`,
# set `field_name: cf_additional_dns`.
field_name: description

# Tag Name (Optional)
Expand Down
31 changes: 29 additions & 2 deletions octodns_netbox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def _build_ptr_records(
)
# Potentially multiple FQDNs in the designated field
fqdns = self._parse_fqdns_list(
ipam_record[self.field_name],
self._get_field_value(ipam_record),
len_limit=None if self.multivalue_ptr else 1,
)
for fqdn in fqdns:
Expand Down Expand Up @@ -385,7 +385,7 @@ def _build_records_for_ipam_record(
record_type: Literal["A", "AAAA"] = "A" if ip_address.version == 4 else "AAAA"

# Parse out any FQDNs listed in the desired NetBox field
fqdns = self._parse_fqdns_list(ipam_record[self.field_name])
fqdns = self._parse_fqdns_list(self._get_field_value(ipam_record))

# For each FQDN, determine if it belongs to this zone and create records
for fqdn in fqdns:
Expand Down Expand Up @@ -465,3 +465,30 @@ def _parse_fqdns_list(
if fqdn.strip()
]
return fqdns[:len_limit] if len_limit else fqdns

def _get_field_value(self, ipam_record: typing.Any) -> str:
"""
Retrieves the value of the configured field from an IPAM record.

Supports both standard fields (e.g., 'dns_name', 'description') and
custom fields (prefixed with 'cf_'). Custom fields are accessed via
the 'custom_fields' dictionary on the IPAM record.

Args:
ipam_record (Any): A single IPAM record from NetBox.

Returns:
str: The field value, or an empty string if the field is not set.

Examples:
- field_name='dns_name' -> ipam_record['dns_name']
- field_name='cf_additional_dns' -> ipam_record.custom_fields['additional_dns']
"""
if self.field_name.startswith("cf_"):
# Custom field: strip 'cf_' prefix and access via custom_fields
custom_field_name = self.field_name[3:]
custom_fields = getattr(ipam_record, "custom_fields", {}) or {}
return custom_fields.get(custom_field_name, "") or ""
else:
# Standard field: access directly
return ipam_record[self.field_name] or ""