0

Update Interface

Published 20 days ago

Patch an interface: enable/disable it, change its description, MTU or 802.1Q mode and VLANs. Only the fields you set change. clear_mode sets mode to null and also unsets the untagged, tagged and Q-in-Q service VLANs (NetBox refuses a null mode while any are set); clear_untagged_vlan unsets the untagged VLAN. A clear_* switch together with a value for the same field is refused.

Script netbox Verified

The script

Submitted by hugo989 Python3
Verified 20 days ago
1
# py312
2
# requirements:
3
# pynetbox==7.9.0
4

5

6
from typing import List, Literal, Optional, TypedDict
7

8
import pynetbox
9

10

11
class netbox(TypedDict):
12
    url: str
13
    token: str
14

15

16
def api(auth: netbox):
17
    # pynetbox adds /api itself; the resource URL may already end with it.
18
    return pynetbox.api(
19
        auth["url"].rstrip("/").removesuffix("/api"), token=auth["token"]
20
    )
21

22

23
def keep(**fields):
24
    # The run form sends "" / [] / {} for untouched fields: drop them so a PATCH never blanks a value.
25
    return {k: v for k, v in fields.items() if v not in (None, "", [], {})}
26

27

28
def main(
29
    auth: netbox,
30
    interface_id: int,
31
    enabled: Optional[Literal["keep", "true", "false"]] = None,
32
    description: Optional[str] = None,
33
    label: Optional[str] = None,
34
    mtu: Optional[int] = None,
35
    mode: Optional[Literal["access", "tagged", "tagged-all", "q-in-q"]] = None,
36
    untagged_vlan_id: Optional[int] = None,
37
    tagged_vlan_ids: Optional[List[int]] = None,
38
    custom_fields: Optional[dict] = None,
39
    clear_mode: bool = False,
40
    clear_untagged_vlan: bool = False,
41
):
42
    """
43
    Update Interface
44
    Patch an interface: enable/disable it, change its description, MTU or 802.1Q mode and VLANs. Only the fields you set change. clear_mode sets mode to null and also unsets the untagged, tagged and Q-in-Q service VLANs (NetBox refuses a null mode while any are set); clear_untagged_vlan unsets the untagged VLAN. A clear_* switch together with a value for the same field is refused.
45
    """
46
    if clear_mode and (mode or untagged_vlan_id is not None or tagged_vlan_ids):
47
        raise ValueError(
48
            "clear_mode unsets mode and VLANs: don't also set mode, untagged_vlan_id or tagged_vlan_ids"
49
        )
50
    if clear_untagged_vlan and untagged_vlan_id is not None:
51
        raise ValueError("Set untagged_vlan_id or clear_untagged_vlan, not both")
52
    body = keep(
53
        enabled=None if enabled in (None, "", "keep") else enabled == "true",
54
        description=description,
55
        label=label,
56
        mtu=mtu,
57
        mode=mode,
58
        untagged_vlan=untagged_vlan_id,
59
        tagged_vlans=tagged_vlan_ids,
60
        custom_fields=custom_fields,
61
    )
62
    # keep() drops empty values, so clearing a field is explicit: these send null.
63
    if clear_mode:
64
        # NetBox refuses a null mode while VLANs are still set.
65
        body |= {
66
            "mode": None,
67
            "untagged_vlan": None,
68
            "tagged_vlans": [],
69
            "qinq_svlan": None,
70
        }
71
    if clear_untagged_vlan:
72
        body["untagged_vlan"] = None
73
    # A list body on the collection endpoint is NetBox's PATCH of only these fields.
74
    return dict(api(auth).dcim.interfaces.update([{"id": interface_id, **body}])[0])
75