aristanetworks / cvprac

Other
46 stars 47 forks source link

How can I build a query for "get_inventory"? #169

Closed T0hruSaka1 closed 2 years ago

T0hruSaka1 commented 2 years ago

I want to filter to result from get_inventory() by "hostname". If that's possible, please let me show sample for query.

get_inventory() https://github.com/aristanetworks/cvprac/blob/70231695016c03a2b28f5de2b363b24bb23d1fbb/cvprac/cvp_api.py#L497

mharista commented 2 years ago

Hi @T0hruSaka1

If you would like to get a device by hostname you can use the get_device_by_name() function.

**def get_device_by_name(self, fqdn, search_by_hostname=False):
        Returns the net element device dict for the devices fqdn name.

        Args:
            fqdn (str): Fully qualified domain name or hostname of the
                device.
            search_by_hostname (boolean): if set True will attempt to split
                the fqdn string to match on the hostname portion
                specifically which should be the first component

        Returns:
            device (dict): The net element device dict for the device if
                otherwise returns an empty hash.**

Unfortunately the query parameter's functionality for get_inventory() depends on your CVP version and was primarily used in older version of CVP. Let me know if get_device_by_name() solves your problem.

T0hruSaka1 commented 2 years ago

@mharista Thank you for your response. get_device_by_name() worked well.

I would like to get reponse from multiple devices in one request if possible. I think search_by_hostname arg will help.

I executed get_device_by_name with the following arguments.

However, I got an empty response.

Maybe I'm using a CVP that doesn't support this feature? Is there a way to check?

Thank you for your support.

mharista commented 2 years ago

@T0hruSaka1

Unfortunately get_device_by_hostname() matches specifically on the full hostname portion of the fqdn. It expects the hostname to be the first index of the fqdn after splitting by the '.' character. The empty response you are getting using only a part of the name is as expected.

I think a better solution for the functionality you are looking for would be the use the search_topology() api function directly. search_topology takes a query parameter that will work in the partial matching format you are looking for.

Can you try replacing get_device_by_name(query_param) with search_topology(query_param) and see if that gets the results you are looking for?

T0hruSaka1 commented 2 years ago

@mharista

search_topology(query_param) worked perfectly as hoped.

Thank you for your kind support.