Ejemplo n.º 1
0
def device_info(ipf: IPFabricClient, hostname: str,
                **api_options) -> Coroutine:
    """
    This function returns a coroutine that is responsible for obtaining device
    information for a given `hostname` and returns a dictionary of data representing
    each aspect of the device:
        hostname: the device hostname
        facts: the device inventory record
        parts: the device parts records
        interfaces: the device interface records
        vlans: the vlan used by this device
        ipaddrs: the IP addresses assigned to the device

    Parameters
    ----------
    ipf: IPFabricClient
    hostname: device hostname
    api_options:

    Returns
    -------
    Coroutine, when awaited will return the dictionary as described.
    """
    filter_hostname = IPFabricClient.parse_filter(f"hostname = {hostname}")

    fut = asyncio.gather(
        ipf.fetch_devices(filters=filter_hostname, **api_options),
        ipf.fetch_device_parts(filters=filter_hostname, **api_options),
        fetch_device_interfaces(ipf, filters=filter_hostname, **api_options),
        fetch_device_vlans(ipf, filters=filter_hostname, **api_options),
        fetch_device_ipaddrs(ipf, filters=filter_hostname, **api_options),
        return_exceptions=True,
    )

    async def gather_result():
        res = await fut
        facts = res[0][0]
        return {
            "hostname": facts["hostname"],
            "facts": facts,
            "parts": res[1],
            "interfaces": res[2],
            "vlans": res[3],
            "ipaddrs": res[4],
        }

    return gather_result()
Ejemplo n.º 2
0
async def run(ipf: IPFabricClient, device_list, callback):
    def _done(_task: asyncio.Task):
        _host = _task.get_name()
        _res = _task.result()
        if not len(_res):
            print(f"IPF device not found: {_host}")
            return

        callback(_host, _res[0])

    tasks = {[
        (task := loop.create_task(
            ipf.fetch_devices(
                filters=ipf.parse_filter(f"hostname ~ '{host}'")),
            name=host,
        )),
        task.add_done_callback(_done),
    ][0]
             for host in device_list}