From 69ff1e88429d067115773cbedabc9e035683d896 Mon Sep 17 00:00:00 2001 From: Lars Kellogg-Stedman Date: Thu, 23 Jan 2025 21:04:49 -0500 Subject: [PATCH] Make "esi mdc baremetal node list" work OpenStackConfig.get_all() will fail with a keystone exception if there are any OS_* variables set in the environment [1]. This is easy to trigger if, for example, you have set OS_BAREMETAL_API_VERSION in your environment to work around [2]. We can avoid the problem by removing the call to get_all(), and instead initializing the list of clouds using get_cloud_names(). This commit makes a number of additional changes to esiclient/v1/mdc/mdc_node_baremetal.py: - Several attribute names appear to have changed since this code was written (node.uuid is now node.id; node.instance_uuid is now node.instance_id). - Replace single-letter variable names with more meaningful names. - Add the '--ignore-invalid' command, which allows the command to continue in the event that it cannot successfully connect to one or more clouds. [1]: https://bugs.launchpad.net/openstacksdk/+bug/2096621 [2]: https://github.com/CCI-MOC/esi/issues/669 --- esiclient/v1/mdc/mdc_node_baremetal.py | 47 +++++++++++++------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/esiclient/v1/mdc/mdc_node_baremetal.py b/esiclient/v1/mdc/mdc_node_baremetal.py index c991cca..ed3ecdb 100644 --- a/esiclient/v1/mdc/mdc_node_baremetal.py +++ b/esiclient/v1/mdc/mdc_node_baremetal.py @@ -24,13 +24,14 @@ class MDCBaremetalNodeList(command.Lister): auth_required = False def get_parser(self, prog_name): - parser = super(MDCBaremetalNodeList, self).get_parser(prog_name) + parser = super().get_parser(prog_name) + parser.add_argument("--ignore-invalid", "-i", action="store_true") parser.add_argument( - "--clouds", - dest="clouds", + "clouds", metavar="", - nargs="+", + nargs="*", help=_("Specify the cloud to use from clouds.yaml."), + default=openstack.config.OpenStackConfig().get_cloud_names(), ) return parser @@ -38,7 +39,6 @@ def get_parser(self, prog_name): def take_action(self, parsed_args): columns = [ "Cloud", - "Region", "UUID", "Name", "Instance UUID", @@ -48,27 +48,26 @@ def take_action(self, parsed_args): ] data = [] - cloud_regions = openstack.config.loader.OpenStackConfig().get_all() - if parsed_args.clouds: - cloud_regions = filter( - lambda c: c.name in parsed_args.clouds, cloud_regions - ) - for c in cloud_regions: - nodes = openstack.connect( - cloud=c.name, region=c.config["region_name"] - ).list_machines() - for n in nodes: - data.append( + for cloud in parsed_args.clouds: + try: + data.extend( [ - c.name, - c.config["region_name"], - n.id, - n.name, - n.instance_id, - n.power_state, - n.provision_state, - n.is_maintenance, + cloud, + node.id, + node.name, + node.instance_id, + node.power_state, + node.provision_state, + node.is_maintenance, ] + for node in openstack.connect(cloud=cloud).list_machines() ) + except Exception as err: + if parsed_args.ignore_invalid: + self.log.error( + "failed to retrieve information for cloud %s: %s", cloud, err + ) + continue + raise return columns, data