From b16bba9146fc203957d0cc542ac651ca2a230428 Mon Sep 17 00:00:00 2001 From: Mikita Pilinka Date: Wed, 13 Mar 2024 14:10:14 +0100 Subject: [PATCH] fix: proper message when invalid cloud credentials Ticket: ENT-11134 Changelog: None Signed-off-by: Mikita Pilinka --- cf_remote/commands.py | 27 +++++++++++++++------------ cf_remote/spawn.py | 12 +++++++++--- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/cf_remote/commands.py b/cf_remote/commands.py index 2fceb1d..4927fbf 100644 --- a/cf_remote/commands.py +++ b/cf_remote/commands.py @@ -328,7 +328,6 @@ def spawn( public_ip=True, extend_group=False, ): - if os.path.exists(CLOUD_CONFIG_FPATH): creds_data = read_json(CLOUD_CONFIG_FPATH) else: @@ -392,17 +391,21 @@ def spawn( ) print("Spawning VMs...", end="") sys.stdout.flush() - vms = spawn_vms( - requests, - creds, - region, - key_pair, - security_groups=sec_groups, - provider=provider, - network=network, - role=role, - spawned_cb=print_progress_dot, - ) + try: + vms = spawn_vms( + requests, + creds, + region, + key_pair, + security_groups=sec_groups, + provider=provider, + network=network, + role=role, + spawned_cb=print_progress_dot, + ) + except ValueError as e: + print("Failed to spawn VMs with following error:\n" + str(e)) + return 1 print("DONE") if public_ip and (not all(vm.public_ips for vm in vms)): diff --git a/cf_remote/spawn.py b/cf_remote/spawn.py index c785592..246125c 100644 --- a/cf_remote/spawn.py +++ b/cf_remote/spawn.py @@ -4,6 +4,7 @@ from enum import Enum from multiprocessing.dummy import Pool +from libcloud.common.types import InvalidCredsError from libcloud.compute.types import Provider from libcloud.compute.providers import get_driver from libcloud.compute.base import NodeSize, NodeImage @@ -307,9 +308,14 @@ def spawn_vm_in_aws( size=None, role=None, ): - driver = get_cloud_driver(Providers.AWS, aws_creds, region) - existing_vms = driver.list_nodes() - + try: + driver = get_cloud_driver(Providers.AWS, aws_creds, region) + existing_vms = driver.list_nodes() + except InvalidCredsError as error: + raise ValueError( + "Invalid credentials, check cloud_config.json file, details: \n" + + str(error) + ) if name is None: name = _get_unused_name( [vm.name for vm in existing_vms], platform, _NAME_RANDOM_PART_LENGTH