From 0b60df5cdf1ac6206ef7ece79d74efe19773da9e Mon Sep 17 00:00:00 2001 From: Pallab Pain Date: Tue, 30 Jul 2024 22:50:10 +0530 Subject: [PATCH] feat(devices): filter device list by name The list device API supports filtering the list by a device's name. This commit adds the support in the get_all_devices method. --- rapyuta_io/clients/device_manager.py | 13 +++++++++---- rapyuta_io/rio_client.py | 6 ++++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/rapyuta_io/clients/device_manager.py b/rapyuta_io/clients/device_manager.py index 22e52761..59a69bf7 100644 --- a/rapyuta_io/clients/device_manager.py +++ b/rapyuta_io/clients/device_manager.py @@ -38,12 +38,17 @@ def _add_auth_token_to_devices(self, devices): setattr(device, '_auth_token', self._auth_token) setattr(device, '_project', self._project) - def _get_device(self, device_id=None, retry_limit=0): + def _get_device(self, device_id=None, retry_limit=0, device_name=None): url = self._device_api_host + DEVICE_API_PATH if device_id: url = url + device_id + + query = {} + if device_name is not None: + query = {"name": device_name} + headers = create_auth_header(self._auth_token, self._project) - response = RestClient(url).retry(retry_limit).headers(headers).execute() + response = RestClient(url).retry(retry_limit).headers(headers).query_param(query_param=query).execute() return get_api_response_data(response) @staticmethod @@ -69,14 +74,14 @@ def _device_selection_by_arch(self, arch_list, retry_limit): def set_project(self, project): self._project = project - def device_list(self, online_device=False, arch_list=None, retry_limit=0): + def device_list(self, online_device=False, arch_list=None, retry_limit=0, device_name=None): arch_filtered_uuids = set() if arch_list: for device in self._device_selection_by_arch(arch_list, retry_limit): arch_filtered_uuids.add(device['uuid']) # TODO(shivam): if arch_list is set there's no need for _get_device all - device_list = self._get_device(retry_limit=retry_limit) + device_list = self._get_device(retry_limit=retry_limit, device_name=device_name) devices = [] # todo: add a generic filter like status, name etc for device in device_list: diff --git a/rapyuta_io/rio_client.py b/rapyuta_io/rio_client.py index 7c70d0ff..311fe563 100644 --- a/rapyuta_io/rio_client.py +++ b/rapyuta_io/rio_client.py @@ -389,7 +389,7 @@ def get_user_organizations(self): """ return self._core_api_client.get_user_organizations() - def get_all_devices(self, online_device=False, arch_list=None, retry_limit=0): + def get_all_devices(self, online_device=False, arch_list=None, retry_limit=0, device_name=None): """ Get all the devices @@ -404,6 +404,8 @@ def get_all_devices(self, online_device=False, arch_list=None, retry_limit=0): :param retry_limit: No of retry attempts to be carried out if any failures occurs\ during the API call. :type retry_limit: int + :param device_name: Optional parameter to filter the devices based on the device name. + :type device_name: str :return: List of instances of :py:class:`~Device` class :raises: :py:class:`APIError`: If the API returns an error, a status code of anything other than 200/201 is returned @@ -418,7 +420,7 @@ def get_all_devices(self, online_device=False, arch_list=None, retry_limit=0): >>> DeviceArch.ARM32V7, DeviceArch.ARM64V8, DeviceArch.AMD64]) """ - return self._dmClient.device_list(online_device, arch_list, retry_limit) + return self._dmClient.device_list(online_device, arch_list, retry_limit, device_name=device_name) def get_device(self, device_id, retry_limit=0): """