From 810fe15f5a5dd82c4addce94ecd8d06e4c2fd8a9 Mon Sep 17 00:00:00 2001 From: Andreas Maier Date: Fri, 17 Nov 2023 17:25:52 +0100 Subject: [PATCH] Check in partition/lpar list that specified CPC exists Details: * Fail partition/lpar list commands if the specified CPC does not exist. (issue #514) Signed-off-by: Andreas Maier --- docs/changes.rst | 3 +++ zhmccli/_cmd_lpar.py | 33 ++++++++++++--------------------- zhmccli/_cmd_partition.py | 30 +++++++++++------------------- 3 files changed, 26 insertions(+), 40 deletions(-) diff --git a/docs/changes.rst b/docs/changes.rst index b75ce56b..4887a463 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -139,6 +139,9 @@ Released: not yet * Added support for zeroizing crypto domains with a new command 'zhmc partition zeroize-crypto'. (issue #502) +* Fail partition/lpar list commands if the specified CPC does not exist. + (issue #514) + **Cleanup:** * Fixed copyright statements (issue #542) diff --git a/zhmccli/_cmd_lpar.py b/zhmccli/_cmd_lpar.py index f4cc14af..db9c1868 100644 --- a/zhmccli/_cmd_lpar.py +++ b/zhmccli/_cmd_lpar.py @@ -828,30 +828,21 @@ def cmd_lpar_list(cmd_ctx, cpc_name, options): client = zhmcclient.Client(cmd_ctx.session) - if client.version_info() >= API_VERSION_HMC_2_14_0: - # This approach is faster than going through the CPC. - # In addition, starting with HMC API version 3.6 (an update to - # HMC 2.15.0), this approach supports users that do not have object - # access permission to the CPC. - filter_args = {} - if cpc_name: - filter_args['cpc-name'] = cpc_name - lpars = client.consoles.console.list_permitted_lpars( - filter_args=filter_args) + if cpc_name: + # Make sure a non-existing CPC is raised as error + cpc = client.cpcs.find(name=cpc_name) + lpars = cpc.lpars.list() + elif client.version_info() >= API_VERSION_HMC_2_14_0: + # This approach is faster than looping through the CPCs. + # In addition, this approach supports users that do not have object + # access permission to the parent CPC of the returned LPARs. + lpars = client.consoles.console.list_permitted_lpars() else: - filter_args = {} - if cpc_name: - filter_args['name'] = cpc_name - try: - cpcs = client.cpcs.list(filter_args=filter_args) - except zhmcclient.Error as exc: - raise click_exception(exc, cmd_ctx.error_format) lpars = [] + cpcs = client.cpcs.list() for cpc in cpcs: - try: - lpars.extend(cpc.lpars.list()) - except zhmcclient.Error as exc: - raise click_exception(exc, cmd_ctx.error_format) + lpars.extend(cpc.lpars.list()) + # The default exception handling is sufficient for the above. if options['type']: click.echo("The --type option is deprecated and type information " diff --git a/zhmccli/_cmd_partition.py b/zhmccli/_cmd_partition.py index f8267c88..7ff9c393 100644 --- a/zhmccli/_cmd_partition.py +++ b/zhmccli/_cmd_partition.py @@ -1107,29 +1107,21 @@ def cmd_partition_list(cmd_ctx, cpc_name, options): client = zhmcclient.Client(cmd_ctx.session) - if client.version_info() >= API_VERSION_HMC_2_14_0: - # This approach is faster than going through the CPC. + if cpc_name: + # Make sure a non-existing CPC is raised as error + cpc = client.cpcs.find(name=cpc_name) + partitions = cpc.partitions.list() + elif client.version_info() >= API_VERSION_HMC_2_14_0: + # This approach is faster than looping through the CPCs. # In addition, this approach supports users that do not have object - # access permission to the parent CPC of the LPAR. - filter_args = {} - if cpc_name: - filter_args['cpc-name'] = cpc_name - partitions = client.consoles.console.list_permitted_partitions( - filter_args=filter_args) + # access permission to the parent CPC of the returned partitions. + partitions = client.consoles.console.list_permitted_partitions() else: - filter_args = {} - if cpc_name: - filter_args['name'] = cpc_name - try: - cpcs = client.cpcs.list(filter_args=filter_args) - except zhmcclient.Error as exc: - raise click_exception(exc, cmd_ctx.error_format) partitions = [] + cpcs = client.cpcs.list() for cpc in cpcs: - try: - partitions.extend(cpc.partitions.list()) - except zhmcclient.Error as exc: - raise click_exception(exc, cmd_ctx.error_format) + partitions.extend(cpc.partitions.list()) + # The default exception handling is sufficient for the above. if options['type']: click.echo("The --type option is deprecated and type information "