Edits history of script submission #23918 for ' Create Device (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
    
    
    DynSelect_site_id = str
    DynSelect_role_id = str
    DynSelect_device_type_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 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 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 sites: the first 1000 (NetBox MAX_PAGE_SIZE); set the value by expression beyond that.
    def site_id(auth: netbox):
        return options(api(auth).dcim.sites)
    
    
    # Dropdown of the instance's device roles.
    def role_id(auth: netbox):
        return options(api(auth).dcim.device_roles)
    
    
    # Dropdown of the instance's device types.
    def device_type_id(auth: netbox):
        return options(api(auth).dcim.device_types)
    
    
    def main(
        auth: netbox,
        name: str,
        site_id: DynSelect_site_id,
        role_id: DynSelect_role_id,
        device_type_id: DynSelect_device_type_id,
        status: Optional[
            Literal[
                "offline",
                "active",
                "planned",
                "staged",
                "failed",
                "inventory",
                "decommissioning",
            ]
        ] = None,
        platform_id: Optional[int] = None,
        rack_id: Optional[int] = None,
        serial: Optional[str] = None,
        description: Optional[str] = None,
        tags: Optional[List[str]] = None,
        custom_fields: Optional[dict] = None,
    ):
        """
        Create Device
        Create a device in a site with a role and device type. Tags are slugs; custom_fields is a {name: value} object.
        """
        body = keep(
            name=name,
            site=int(site_id),
            role=int(role_id),
            device_type=int(device_type_id),
            status=status,
            platform=platform_id,
            rack=rack_id,
            serial=serial,
            description=description,
            tags=[{"slug": s} for s in tags or []],
            custom_fields=custom_fields,
        )
        return dict(api(auth).dcim.devices.create(body))
    

    Submitted by hugo989 3 hours ago