Skip to content

Commit 07ffaa8

Browse files
error handling
1 parent f8849c8 commit 07ffaa8

File tree

1 file changed

+38
-36
lines changed

1 file changed

+38
-36
lines changed

src/azure-cli/azure/cli/command_modules/appconfig/_kv_import_helpers.py

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from azure.cli.core.util import user_confirmation
2323
from azure.cli.core.azclierror import (
2424
AzureInternalError,
25+
AzureResponseError,
2526
FileOperationError,
2627
InvalidArgumentValueError,
2728
RequiredArgumentMissingError,
@@ -767,47 +768,48 @@ def __read_kv_from_kubernetes_configmap(
767768
Returns:
768769
list: List of KeyValue objects
769770
"""
770-
key_values = []
771-
from azure.cli.command_modules.acs.custom import aks_runcommand
772-
from azure.cli.command_modules.acs._client_factory import cf_managed_clusters
773-
774-
# Get the AKS client from the factory
775-
cmd.cli_ctx.data['subscription_id'] = aks_cluster["subscription"]
776-
cmd.cli_ctx.data['safe_params'] = None
777-
aks_client = cf_managed_clusters(cmd.cli_ctx)
778-
779-
# Command to get the ConfigMap and output it as JSON
780-
command = f"kubectl get configmap {configmap_name} -n {namespace} -o json"
781-
782-
# Execute the command on the cluster
783-
result = aks_runcommand(cmd, aks_client, aks_cluster["resource_group"], aks_cluster["name"], command_string=command)
771+
try:
772+
key_values = []
773+
from azure.cli.command_modules.acs.custom import aks_runcommand
774+
from azure.cli.command_modules.acs._client_factory import cf_managed_clusters
775+
776+
# Get the AKS client from the factory
777+
cmd.cli_ctx.data['subscription_id'] = aks_cluster["subscription"]
778+
cmd.cli_ctx.data['safe_params'] = None
779+
aks_client = cf_managed_clusters(cmd.cli_ctx)
784780

785-
if hasattr(result, 'logs') and result.logs:
786-
if not hasattr(result, 'exit_code') or result.exit_code != 0:
787-
raise CLIError(
788-
f"Failed to get ConfigMap {configmap_name} in namespace {namespace}. {result.logs.strip()}"
789-
)
781+
# Command to get the ConfigMap and output it as JSON
782+
command = f"kubectl get configmap {configmap_name} -n {namespace} -o json"
783+
784+
# Execute the command on the cluster
785+
result = aks_runcommand(cmd, aks_client, aks_cluster["resource_group"], aks_cluster["name"], command_string=command)
786+
787+
if hasattr(result, 'logs') and result.logs:
788+
if not hasattr(result, 'exit_code') or result.exit_code != 0:
789+
raise AzureResponseError(f"{result.logs.strip()}")
790+
791+
try:
792+
import json
793+
configmap_data = json.loads(result.logs)
794+
795+
# Extract the data section which contains the key-value pairs
796+
kvs = __extract_kv_from_configmap_data(
797+
configmap_data, content_type, prefix_to_add, format_, depth, separator)
798+
799+
key_values.extend(kvs)
800+
except json.JSONDecodeError:
801+
raise ValidationError(
802+
f"The result from ConfigMap {configmap_name} could not be parsed as valid JSON."
803+
)
804+
else:
805+
raise AzureResponseError("Unable to get the ConfigMap.")
790806

791-
try:
792-
import json
793-
configmap_data = json.loads(result.logs)
794-
795-
# Extract the data section which contains the key-value pairs
796-
kvs = __extract_kv_from_configmap_data(
797-
configmap_data, content_type, prefix_to_add, format_, depth, separator)
798-
799-
key_values.extend(kvs)
800-
except json.JSONDecodeError:
801-
raise ValidationError(
802-
f"The result from ConfigMap {configmap_name} could not be parsed as valid JSON."
803-
)
804-
else:
807+
return key_values
808+
except Exception as exception:
805809
raise CLIError(
806-
f"Failed to get ConfigMap {configmap_name} in namespace {namespace}."
810+
f"Failed to read key-values from ConfigMap '{configmap_name}' in namespace '{namespace}'.\n{str(exception)}"
807811
)
808812

809-
return key_values
810-
811813
def __extract_kv_from_configmap_data(configmap_data, content_type, prefix_to_add="", format_=None, depth=None, separator=None):
812814
"""
813815
Helper function to extract key-value pairs from ConfigMap data.

0 commit comments

Comments
 (0)