Edits history of script submission #23937 for ' Update Interface (netbox)'

  • python3
    One script reply has been approved by the moderators
    Ap­pro­ved
    # 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 so a PATCH never blanks a value.
        return {k: v for k, v in fields.items() if v not in (None, "", [], {})}
    
    
    def main(
        auth: netbox,
        interface_id: int,
        enabled: Optional[Literal["keep", "true", "false"]] = None,
        description: Optional[str] = None,
        label: Optional[str] = None,
        mtu: Optional[int] = None,
        mode: Optional[Literal["access", "tagged", "tagged-all", "q-in-q"]] = None,
        untagged_vlan_id: Optional[int] = None,
        tagged_vlan_ids: Optional[List[int]] = None,
        custom_fields: Optional[dict] = None,
        clear_mode: bool = False,
        clear_untagged_vlan: bool = False,
    ):
        """
        Update Interface
        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.
        """
        if clear_mode and (mode or untagged_vlan_id is not None or tagged_vlan_ids):
            raise ValueError(
                "clear_mode unsets mode and VLANs: don't also set mode, untagged_vlan_id or tagged_vlan_ids"
            )
        if clear_untagged_vlan and untagged_vlan_id is not None:
            raise ValueError("Set untagged_vlan_id or clear_untagged_vlan, not both")
        body = keep(
            enabled=None if enabled in (None, "", "keep") else enabled == "true",
            description=description,
            label=label,
            mtu=mtu,
            mode=mode,
            untagged_vlan=untagged_vlan_id,
            tagged_vlans=tagged_vlan_ids,
            custom_fields=custom_fields,
        )
        # keep() drops empty values, so clearing a field is explicit: these send null.
        if clear_mode:
            # NetBox refuses a null mode while VLANs are still set.
            body |= {
                "mode": None,
                "untagged_vlan": None,
                "tagged_vlans": [],
                "qinq_svlan": None,
            }
        if clear_untagged_vlan:
            body["untagged_vlan"] = None
        # A list body on the collection endpoint is NetBox's PATCH of only these fields.
        return dict(api(auth).dcim.interfaces.update([{"id": interface_id, **body}])[0])
    

    Submitted by hugo989 20 days ago

  • python3
    # 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 so a PATCH never blanks a value.
        return {k: v for k, v in fields.items() if v not in (None, "", [], {})}
    
    
    def main(
        auth: netbox,
        interface_id: int,
        enabled: Optional[Literal["keep", "true", "false"]] = None,
        description: Optional[str] = None,
        label: Optional[str] = None,
        mtu: Optional[int] = None,
        mode: Optional[Literal["access", "tagged", "tagged-all", "q-in-q"]] = None,
        untagged_vlan_id: Optional[int] = None,
        tagged_vlan_ids: Optional[List[int]] = None,
        custom_fields: Optional[dict] = None,
    ):
        """
        Update Interface
        Patch an interface: enable/disable it, change its description, MTU or 802.1Q mode and VLANs. Only the fields you set change.
        """
        body = keep(
            enabled=None if enabled in (None, "", "keep") else enabled == "true",
            description=description,
            label=label,
            mtu=mtu,
            mode=mode,
            untagged_vlan=untagged_vlan_id,
            tagged_vlans=tagged_vlan_ids,
            custom_fields=custom_fields,
        )
        # A list body on the collection endpoint is NetBox's PATCH of only these fields.
        return dict(api(auth).dcim.interfaces.update([{"id": interface_id, **body}])[0])
    

    Submitted by hugo989 21 days ago