Skip to content

Commit 7ce1d50

Browse files
style
1 parent a88288e commit 7ce1d50

File tree

3 files changed

+59
-58
lines changed

3 files changed

+59
-58
lines changed

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

Lines changed: 55 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ def flatten_config_data(config_data, format_, content_type, prefix_to_add="", de
615615
depth=depth,
616616
separator=separator,
617617
)
618-
618+
619619
return flattened_data
620620

621621
# App Service <-> List of KeyValue object
@@ -771,47 +771,46 @@ def __read_kv_from_kubernetes_configmap(
771771
Returns:
772772
list: List of KeyValue objects
773773
"""
774-
try:
775-
key_values = []
776-
from azure.cli.command_modules.acs.custom import aks_runcommand
777-
from azure.cli.command_modules.acs._client_factory import cf_managed_clusters
778-
779-
# Get the AKS client from the factory
780-
cmd.cli_ctx.data['subscription_id'] = aks_cluster["subscription"]
781-
cmd.cli_ctx.data['safe_params'] = None
782-
aks_client = cf_managed_clusters(cmd.cli_ctx)
774+
# try:
775+
key_values = []
776+
from azure.cli.command_modules.acs.custom import aks_runcommand
777+
from azure.cli.command_modules.acs._client_factory import cf_managed_clusters
783778

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

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

816815

817816
def __extract_kv_from_configmap_data(configmap_data, content_type, prefix_to_add="", format_=None, depth=None, separator=None):
@@ -830,39 +829,40 @@ def __extract_kv_from_configmap_data(configmap_data, content_type, prefix_to_add
830829
list: List of KeyValue objects
831830
"""
832831
key_values = []
833-
import json
834-
832+
835833
if 'data' not in configmap_data:
836834
logger.warning("ConfigMap exists but has no data")
837835
return key_values
838-
836+
839837
for key, value in configmap_data['data'].items():
840838
if format_ in ("json", "yaml", "properties"):
841839
if format_ == "json":
842840
try:
843841
value = json.loads(value)
844842
except json.JSONDecodeError:
845843
logger.warning(
846-
f'Value "{value}" for key "{key}" is not a well formatted JSON data.'
844+
'Value "%s" for key "%s" is not a well formatted JSON data.',
845+
value, key
847846
)
848847
continue
849848
elif format_ == "yaml":
850849
try:
851850
value = yaml.safe_load(value)
852851
except yaml.YAMLError:
853852
logger.warning(
854-
f'Value "{value}" for key "{key}" is not a well formatted YAML data.'
853+
'Value "%s" for key "%s" is not a well formatted YAML data.',
854+
value, key
855855
)
856856
continue
857857
else:
858858
try:
859-
import io
860859
value = javaproperties.load(io.StringIO(value))
861-
except Exception:
860+
except javaproperties.InvalidUEscapeError:
862861
logger.warning(
863-
f'Value "{value}" for key "{key}" is not a well formatted properties data.'
862+
'Value "%s" for key "%s" is not a well formatted properties data.',
863+
value, key
864864
)
865-
continue
865+
continue
866866

867867
flattened_data = flatten_config_data(
868868
config_data=value,
@@ -878,21 +878,22 @@ def __extract_kv_from_configmap_data(configmap_data, content_type, prefix_to_add
878878
if validate_import_key(key=k):
879879
key_values.append(KeyValue(key=k, value=v))
880880
return key_values
881-
882-
elif validate_import_key(key):
881+
882+
if validate_import_key(key):
883883
# If content_type is JSON, validate the value
884884
if content_type and is_json_content_type(content_type):
885885
try:
886886
json.loads(value)
887887
except json.JSONDecodeError:
888888
logger.warning(
889-
f'Value "{value}" for key "{key}" is not a valid JSON object, which conflicts with the provided content type "{content_type}".'
889+
'Value "{value}" for key "{key}" is not a valid JSON object, which conflicts with the provided content type "{content_type}".',
890+
value, key, content_type
890891
)
891892
continue
892-
893+
893894
kv = KeyValue(key=prefix_to_add + key, value=value)
894895
key_values.append(kv)
895-
896+
896897
return key_values
897898

898899

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
get_default_location_from_resource_group
1717
from ._constants import ImportExportProfiles, ImportMode, FeatureFlagConstants, ARMAuthenticationMode
1818

19-
from ._validators import (validate_appservice_name_or_id, validate_aks_cluster_name_or_id,
19+
from ._validators import (validate_appservice_name_or_id, validate_aks_cluster_name_or_id,
2020
validate_sku, validate_snapshot_query_fields,
2121
validate_connection_string, validate_datetime,
2222
validate_export, validate_import,
@@ -268,7 +268,7 @@ def load_arguments(self, _):
268268
with self.argument_context('appconfig kv import', arg_group='AKS') as c:
269269
c.argument('aks_cluster', validator=validate_aks_cluster_name_or_id, help='ARM ID for AKS OR the name of the AKS, assuming it is in the same subscription and resource group as the App Configuration store. Required for AKS arguments'),
270270
c.argument('configmap_name', help='Name of the ConfigMap. Required for AKS arguments.')
271-
c.argument('configmap_namespace', help='Namespace of the ConfigMap. default to "default" namespace if not specified.')
271+
c.argument('configmap_namespace', help='Namespace of the ConfigMap. default to "default" namespace if not specified.')
272272

273273
with self.argument_context('appconfig kv export') as c:
274274
c.argument('label', help="Only keys and feature flags with this label will be exported. If no label specified, export keys and feature flags with null label by default. When export destination is appconfig, or when export destination is file with `appconfig/kvset` profile, this argument supports asterisk and comma signs for label filtering, for instance, * means all labels, abc* means labels with abc as prefix, and abc,xyz means labels with abc or xyz.")

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def import_config(cmd,
180180
elif source == 'appservice':
181181
src_kvs = __read_kv_from_app_service(
182182
cmd, appservice_account=appservice_account, prefix_to_add=prefix, content_type=content_type)
183-
183+
184184
elif source == 'aks':
185185
if separator:
186186
# If separator is provided, use max depth by default unless depth is specified.
@@ -189,7 +189,7 @@ def import_config(cmd,
189189
if depth and int(depth) != 1:
190190
logger.warning("Cannot flatten hierarchical data without a separator. --depth argument will be ignored.")
191191
depth = 1
192-
192+
193193
src_kvs = __read_kv_from_kubernetes_configmap(cmd,
194194
aks_cluster=aks_cluster,
195195
configmap_name=configmap_name,

0 commit comments

Comments
 (0)