Skip to content

Commit

Permalink
[Keyvault] Fix #6372: table output for secrets isn't correct (#18308)
Browse files Browse the repository at this point in the history
* table transformer

* import

* move build_table_output to core
  • Loading branch information
evelyn-ys authored Jul 29, 2021
1 parent 807c92e commit 9e13343
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 27 deletions.
25 changes: 25 additions & 0 deletions src/azure-cli-core/azure/cli/core/commands/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 17 additions & 0 deletions src/azure-cli/azure/cli/command_modules/keyvault/_format.py
Original file line number Diff line number Diff line change
@@ -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')
])
5 changes: 4 additions & 1 deletion src/azure-cli/azure/cli/command_modules/keyvault/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
27 changes: 1 addition & 26 deletions src/azure-cli/azure/cli/command_modules/storage/_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down

0 comments on commit 9e13343

Please sign in to comment.