Skip to content

Netbox Rest Inventory Task¤

Rest Task Sample Usage¤

NORFAB Netbox GrapQL Command Shell Reference¤

NorFab shell supports these command options for Netbox rest task:

Python API Reference¤

Method to query Netbox REST API.

Parameters:

Name Type Description Default
instance str

Netbox instance name

None
method str

requests method name e.g. get, post, put etc.

'get'
api str

api url to query e.g. "extras" or "dcim/interfaces" etc.

''
kwargs

any additional requests method's arguments

{}

Returns:

Type Description
Union[dict, list]

REST API Query result

Raises:

Type Description
Exception

If REST API query fails

Source code in norfab\workers\netbox_worker.py
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
def rest(
    self, instance: str = None, method: str = "get", api: str = "", **kwargs
) -> Union[dict, list]:
    """
    Method to query Netbox REST API.

    :param instance: Netbox instance name
    :param method: requests method name e.g. get, post, put etc.
    :param api: api url to query e.g. "extras" or "dcim/interfaces" etc.
    :param kwargs: any additional requests method's arguments
    :return: REST API Query result
    :raises Exception: If REST API query fails
    """
    params = self._get_instance_params(instance)

    # send request to Netbox REST API
    response = getattr(requests, method)(
        url=f"{params['url']}/api/{api}/",
        headers={
            "Content-Type": "application/json",
            "Accept": "application/json",
            "Authorization": f"Token {params['token']}",
        },
        verify=params.get("ssl_verify", True),
        **kwargs,
    )

    response.raise_for_status()

    return response.json()