From 61a20630f79d34a717b1f993bae010e1f807cf79 Mon Sep 17 00:00:00 2001 From: RomilShah Date: Mon, 7 Aug 2023 18:16:43 +0530 Subject: [PATCH] feat(device): updates device delete command to delete multiple devices This commit updates device delete command that enables users to conveniently delete existing devices by providing device name or regex that can delete multiple devices Usage: python -m rio device delete [OPTIONS] [DEVICE_NAME_OR_REGEX] Deletes one more devices Options: -f, --force, --silent Skip confirmation -a, --delete-all Deletes all devices -w, --workers INTEGER number of parallel workers while running delete device command. defaults to 10. --help Show this message and exit. --- riocli/device/delete.py | 152 +++++++++++++++++++++++++++++++++++----- 1 file changed, 136 insertions(+), 16 deletions(-) diff --git a/riocli/device/delete.py b/riocli/device/delete.py index 90e27a54..2816dafe 100644 --- a/riocli/device/delete.py +++ b/riocli/device/delete.py @@ -11,12 +11,21 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +from concurrent.futures import ThreadPoolExecutor +import functools +import re +from queue import Queue +from typing import List + import click from click_help_colors import HelpColorsCommand +from rapyuta_io.clients.device import Device +from yaspin.api import Yaspin +from rapyuta_io import Client from riocli.config import new_client -from riocli.constants import Colors, Symbols -from riocli.device.util import name_to_guid +from riocli.constants import Symbols, Colors +from riocli.utils import tabulate_data from riocli.utils.spinner import with_spinner @@ -26,26 +35,137 @@ help_headers_color=Colors.YELLOW, help_options_color=Colors.GREEN, ) -@click.option('--force', '-f', 'force', is_flag=True, help='Skip confirmation') -@click.argument('device-name', type=str) -@name_to_guid +@click.option('--force', '-f', '--silent', is_flag=True, default=False, + help='Skip confirmation') +@click.option('--delete-all', '-a', is_flag=True, default=False, + help='deletes all devices') +@click.option('--workers', '-w', + help="number of parallel workers while running delete devices " + "command. defaults to 10.", type=int, default=10) +@click.argument('device-name-or-regex', type=str, default="") @with_spinner(text='Deleting device...') -def delete_device(device_name: str, device_guid: str, force: bool, spinner=None): +def delete_device( + force: bool, + delete_all: bool, + workers: int, + device_name_or_regex: str, + spinner: Yaspin = None, +) -> None: """ - Deletes a device + Deletes one more devices """ + client = new_client() + if not (device_name_or_regex or delete_all): + spinner.text = 'Nothing to delete' + spinner.green.ok(Symbols.SUCCESS) + return + + try: + devices = fetch_devices( + client, device_name_or_regex, delete_all) + except Exception as e: + spinner.text = click.style( + 'Failed to delete device(s): {}'.format(e), Colors.RED) + spinner.red.fail(Symbols.ERROR) + raise SystemExit(1) from e + + if not devices: + spinner.text = "No devices to delete" + spinner.ok(Symbols.SUCCESS) + return + + headers = ['Name', 'Device ID', 'Status'] + data = [[d.name, d.uuid, d.status] for d in devices] + with spinner.hidden(): - if not force: - click.confirm( - 'Deleting device {} ({})'.format( - device_name, device_guid), abort=True) + tabulate_data(data, headers) + + spinner.write('') + + if not force: + with spinner.hidden(): + click.confirm('Do you want to delete above device(s)?', + default=True, abort=True) + spinner.write('') try: - client = new_client(with_project=True) - client.delete_device(device_id=device_guid) - spinner.text = click.style('Device deleted successfully', fg=Colors.GREEN) - spinner.green.ok(Symbols.SUCCESS) + result = Queue() + func = functools.partial(_delete_deivce, client, result) + with ThreadPoolExecutor(max_workers=workers) as executor: + executor.map(func, devices) + + result = sorted(list(result.queue), key=lambda x: x[0]) + + data, fg, statuses = [], Colors.GREEN, [] + success_count, failed_count = 0, 0 + + for name, response in result: + if response and response.status_code < 400: + fg = Colors.GREEN + icon = Symbols.SUCCESS + success_count += 1 + else: + fg = Colors.RED + icon = Symbols.ERROR + failed_count +=1 + if response: + r = response.json() + error = r.get('response', {}).get('error') + + if 'deployments' in error: + msg = 'Device {0} has running deployments. Please de-provision them before deleting the device.'.format(name) + spinner.text = click.style(msg, Colors.YELLOW) + spinner.ok(click.style(Symbols.WARNING, Colors.YELLOW)) + + data.append([ + click.style(name, fg), + click.style(icon, fg) + ]) + + with spinner.hidden(): + tabulate_data(data, headers=['Name', 'Status']) + + spinner.write('') + if success_count: + spinner.text = click.style( + '{0} Devices(s) deleted successfully.'.format(success_count), Colors.GREEN) + spinner.ok(click.style(Symbols.SUCCESS, Colors.GREEN)) + if failed_count: + spinner.text = click.style( + '{0} Devices(s) deletion failed.'.format(failed_count), Colors.YELLOW) + spinner.ok(click.style(Symbols.WARNING, Colors.YELLOW)) except Exception as e: - spinner.text = click.style('Failed to delete device: {}'.format(e), fg=Colors.RED) + spinner.text = click.style( + 'Failed to delete device(s): {}'.format(e), Colors.RED) spinner.red.fail(Symbols.ERROR) raise SystemExit(1) from e + + +def fetch_devices( + client: Client, + device_name_or_regex: str, + delete_all: bool, +) -> List[Device]: + devices = client.get_all_devices() + result = [] + for device in devices: + if (delete_all or device.name == device_name_or_regex or + (device_name_or_regex not in device.name and + re.search(device_name_or_regex, device.name)) or + device_name_or_regex == device.uuid): + result.append(device) + + return result + + +def _delete_deivce( + client: Client, + result: Queue, + device: Device = None, +) -> None: + response = None + try: + response = client.delete_device(device_id=device.uuid) + result.put((device["name"], response)) + except Exception: + result.put((device["name"], response))