# py312
# requirements:
# pynetbox==7.9.0
from typing import List, Literal, Optional, TypedDict
import pynetbox
class netbox(TypedDict):
url: str
token: str
def api(auth: netbox):
# pynetbox adds /api itself; the resource URL may already end with it.
return pynetbox.api(
auth["url"].rstrip("/").removesuffix("/api"), token=auth["token"]
)
def keep(**fields):
# The run form sends "" / [] / {} for untouched fields: drop them.
return {k: v for k, v in fields.items() if v not in (None, "", [], {})}
def main(
auth: netbox,
address: str,
status: Optional[
Literal["active", "reserved", "deprecated", "dhcp", "slaac"]
] = None,
vrf_id: Optional[int] = None,
dns_name: Optional[str] = None,
description: Optional[str] = None,
interface_id: Optional[int] = None,
tags: Optional[List[str]] = None,
custom_fields: Optional[dict] = None,
):
"""
Create IP Address
Create an IP address (CIDR, e.g. 10.0.0.5/24), optionally assigned to a device interface.
"""
body = keep(
address=address,
status=status,
vrf=vrf_id,
dns_name=dns_name,
description=description,
assigned_object_type="dcim.interface" if interface_id else None,
assigned_object_id=interface_id,
tags=[{"slug": s} for s in tags or []],
custom_fields=custom_fields,
)
return dict(api(auth).ipam.ip_addresses.create(body))
Submitted by hugo989 3 hours ago