From 9e133430a0d59ac10dd1d4436f0c4dc0a6189ff2 Mon Sep 17 00:00:00 2001 From: Yishi Wang Date: Thu, 29 Jul 2021 10:30:11 +0800 Subject: [PATCH] [Keyvault] Fix #6372: table output for secrets isn't correct (#18308) * table transformer * import * move build_table_output to core --- .../azure/cli/core/commands/transform.py | 25 +++++++++++++++++ .../cli/command_modules/keyvault/_format.py | 17 ++++++++++++ .../cli/command_modules/keyvault/commands.py | 5 +++- .../cli/command_modules/storage/_format.py | 27 +------------------ 4 files changed, 47 insertions(+), 27 deletions(-) create mode 100644 src/azure-cli/azure/cli/command_modules/keyvault/_format.py diff --git a/src/azure-cli-core/azure/cli/core/commands/transform.py b/src/azure-cli-core/azure/cli/core/commands/transform.py index 2a5f3692290..329d3c39607 100644 --- a/src/azure-cli-core/azure/cli/core/commands/transform.py +++ b/src/azure-cli-core/azure/cli/core/commands/transform.py @@ -73,3 +73,28 @@ def _dict_to_list_transform(result): return result return _dict_to_list_transform + + +def build_table_output(result, projection): + + if not isinstance(result, list): + result = [result] + + final_list = [] + + for item in result: + def _value_from_path(each_item, path): + obj = each_item + try: + for part in path.split('.'): + obj = obj.get(part, None) + except AttributeError: + obj = None + return obj or ' ' + + item_dict = {} + for element in projection: + item_dict[element[0]] = _value_from_path(item, element[1]) + final_list.append(item_dict) + + return final_list diff --git a/src/azure-cli/azure/cli/command_modules/keyvault/_format.py b/src/azure-cli/azure/cli/command_modules/keyvault/_format.py new file mode 100644 index 00000000000..50aa9414f5a --- /dev/null +++ b/src/azure-cli/azure/cli/command_modules/keyvault/_format.py @@ -0,0 +1,17 @@ +# -------------------------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for license information. +# -------------------------------------------------------------------------------------------- +"""Table transformer for keyvault commands""" + +from azure.cli.core.commands.transform import build_table_output + + +def transform_secret_list(result): + return build_table_output(result, [ + ('Name', 'name'), + ('Id', 'id'), + ('ContentType', 'contentType'), + ('Enabled', 'attributes.enabled'), + ('Expires', 'attributes.expires') + ]) diff --git a/src/azure-cli/azure/cli/command_modules/keyvault/commands.py b/src/azure-cli/azure/cli/command_modules/keyvault/commands.py index 3033e4728b2..b33467c3bb3 100644 --- a/src/azure-cli/azure/cli/command_modules/keyvault/commands.py +++ b/src/azure-cli/azure/cli/command_modules/keyvault/commands.py @@ -15,6 +15,8 @@ extract_subresource_name, filter_out_managed_resources, multi_transformers, transform_key_decryption_output, keep_max_results) +from azure.cli.command_modules.keyvault._format import transform_secret_list + from azure.cli.command_modules.keyvault._validators import ( process_secret_set_namespace, process_certificate_cancel_namespace, validate_private_endpoint_connection_id, validate_role_assignment_args) @@ -178,7 +180,8 @@ def load_command_table(self, _): transform=multi_transformers( filter_out_managed_resources, keep_max_results, - extract_subresource_name())) + extract_subresource_name()), + table_transformer=transform_secret_list) g.keyvault_command('list-versions', 'get_secret_versions', transform=multi_transformers( keep_max_results, diff --git a/src/azure-cli/azure/cli/command_modules/storage/_format.py b/src/azure-cli/azure/cli/command_modules/storage/_format.py index a449adfd67c..bdd9a76a958 100644 --- a/src/azure-cli/azure/cli/command_modules/storage/_format.py +++ b/src/azure-cli/azure/cli/command_modules/storage/_format.py @@ -4,38 +4,13 @@ # -------------------------------------------------------------------------------------------- """Table transformer for storage commands""" +from azure.cli.core.commands.transform import build_table_output from azure.cli.core.profiles import get_sdk, ResourceType from knack.log import get_logger logger = get_logger(__name__) -def build_table_output(result, projection): - - if not isinstance(result, list): - result = [result] - - final_list = [] - - from collections import OrderedDict - for item in result: - def _value_from_path(each_item, path): - obj = each_item - try: - for part in path.split('.'): - obj = obj.get(part, None) - except AttributeError: - obj = None - return obj or ' ' - - item_dict = OrderedDict() - for element in projection: - item_dict[element[0]] = _value_from_path(item, element[1]) - final_list.append(item_dict) - - return final_list - - def transform_container_list(result): return build_table_output(result, [ ('Name', 'name'),