Edits history of script submission #23924 for ' List Available Prefixes (netbox)'

  • python3
    One script reply has been approved by the moderators
    Ap­pro­ved
    # py312
    # requirements:
    # pynetbox==7.9.0
    
    
    from typing import TypedDict
    
    import pynetbox
    
    
    class netbox(TypedDict):
        url: str
        token: str
    
    
    DynSelect_prefix_id = 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"]
        )
    
    
    # Dropdown of the instance's prefixes, labelled with their VRF and scope since the same prefix can exist in several.
    # First 1000 only (NetBox MAX_PAGE_SIZE); set the value by expression beyond that.
    def prefix_id(auth: netbox):
        options = []
        for p in api(auth).ipam.prefixes.filter(
            fields="id,prefix,vrf,scope,site", limit=1000, offset=0
        ):
            # Raw dict: a missing attribute on a pynetbox Record triggers a fetch.
            p = dict(p)
            # Scope (4.2+) or site (older) tells apart the same prefix at two sites.
            where = p.get("scope") or p.get("site")
            label = f"{p['prefix']} ({p['vrf']['name'] if p.get('vrf') else 'global'})"
            if where:
                label += f" · {where['display']}"
            options.append({"value": str(p["id"]), "label": label})
        return options
    
    
    def main(
        auth: netbox,
        prefix_id: DynSelect_prefix_id,
    ):
        """
        List Available Prefixes
        List the free child prefixes in a prefix.
        """
        prefix = api(auth).ipam.prefixes.get(int(prefix_id))
        if prefix is None:
            raise ValueError(f"No prefix with id {prefix_id}")
        return [dict(p) for p in prefix.available_prefixes.list()]
    

    Submitted by hugo989 20 days ago

  • python3
    # py312
    # requirements:
    # pynetbox==7.9.0
    
    
    from typing import TypedDict
    
    import pynetbox
    
    
    class netbox(TypedDict):
        url: str
        token: str
    
    
    DynSelect_prefix_id = 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 options(endpoint, key="id", **filters):
        return [
            {"value": str(getattr(r, key)), "label": r.display}
            for r in endpoint.filter(brief=1, limit=1000, offset=0, **filters)
        ]
    
    
    # Dropdown of the instance's prefixes.
    def prefix_id(auth: netbox):
        return options(api(auth).ipam.prefixes)
    
    
    def main(
        auth: netbox,
        prefix_id: DynSelect_prefix_id,
    ):
        """
        List Available Prefixes
        List the free child prefixes in a prefix.
        """
        prefix = api(auth).ipam.prefixes.get(int(prefix_id))
        if prefix is None:
            raise ValueError(f"No prefix with id {prefix_id}")
        return [dict(p) for p in prefix.available_prefixes.list()]
    

    Submitted by hugo989 21 days ago