From 4ac63da6ab7d2da169e3ef172f383eceaf9a7bd7 Mon Sep 17 00:00:00 2001 From: Roel Fauconnier Date: Mon, 7 Nov 2022 20:19:35 +0000 Subject: [PATCH] fix: #597 force json for results that get parsed --- iotedgedev/azurecli.py | 51 +++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/iotedgedev/azurecli.py b/iotedgedev/azurecli.py index 664a8c4f..ea29cd8b 100644 --- a/iotedgedev/azurecli.py +++ b/iotedgedev/azurecli.py @@ -35,14 +35,17 @@ def decode(self, val): def is_posix(self): return self.envvars.is_posix() - def prepare_az_cli_args(self, args, suppress_output=False): + def prepare_az_cli_args(self, args, suppress_output=False, ensure_json_output=False): if suppress_output: args.extend(["--query", "\"[?n]|[0]\""]) + if ensure_json_output: + args.extend(["--output", "json"]) + az_args = ["az"] + args return az_args - def invoke_az_cli_outproc(self, args, error_message=None, stdout_io=None, stderr_io=None, suppress_output=False, timeout=None): + def invoke_az_cli_outproc(self, args, error_message=None, stdout_io=None, stderr_io=None, suppress_output=False, timeout=None, ensure_json_output=False): try: if timeout: timeout = int(timeout) @@ -54,19 +57,19 @@ def invoke_az_cli_outproc(self, args, error_message=None, stdout_io=None, stderr # Consider using functools if monitor_events: - process = subprocess.Popen(self.prepare_az_cli_args(args, suppress_output), + process = subprocess.Popen(self.prepare_az_cli_args(args, suppress_output, ensure_json_output), shell=not self.is_posix(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=os.setsid if self.is_posix() else None, creationflags=subprocess.CREATE_NEW_PROCESS_GROUP if not self.is_posix() else 0) elif stdout_io or stderr_io: - process = subprocess.Popen(self.prepare_az_cli_args(args, suppress_output), + process = subprocess.Popen(self.prepare_az_cli_args(args, suppress_output, ensure_json_output), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=not self.is_posix()) else: - process = subprocess.Popen(self.prepare_az_cli_args(args, suppress_output), + process = subprocess.Popen(self.prepare_az_cli_args(args, suppress_output, ensure_json_output), shell=not self.is_posix()) self.process = process @@ -227,8 +230,9 @@ def user_has_logged_in(self): self.output.status(f("Retrieving Azure CLI credentials from cache...")) with output_io_cls() as io: - result = self.invoke_az_cli_outproc( - ["account", "show"], stdout_io=io) + result = self.invoke_az_cli_outproc(["account", "show"], + stdout_io=io, + ensure_json_output=True) if result: try: @@ -270,7 +274,9 @@ def get_default_subscription(self): with output_io_cls() as io: result = self.invoke_az_cli_outproc(["account", "show"], - "Error while trying to get the default Azure subscription id.", io) + "Error while trying to get the default Azure subscription id.", + io, + ensure_json_output=True) if result: out_string = io.getvalue() data = json.loads(out_string) @@ -281,12 +287,13 @@ def get_subscription_id_starts_with(self, token): with output_io_cls() as io: query = get_query_argument_for_id_and_name(token) result = self.invoke_az_cli_outproc(["account", "list", "--query", query], - "Could not find a subscription for which the id starts with or name contains '{0}'".format(token), io) + "Could not find a subscription for which the id starts with or name contains '{0}'".format(token), + io, + ensure_json_output=True) if result: out_string = io.getvalue() if out_string: - data = json.loads(out_string) if len(data) == 1: return data[0]["id"] @@ -418,7 +425,10 @@ def monitor_events(self, device_id, connection_string, hub_name, timeout=300): def get_free_iothub(self): with output_io_cls() as io: - result = self.invoke_az_cli_outproc(["iot", "hub", "list"], f("Could not list IoT Hubs in subscription."), stdout_io=io) + result = self.invoke_az_cli_outproc(["iot", "hub", "list"], + f("Could not list IoT Hubs in subscription."), + stdout_io=io, + ensure_json_output=True) if result: out_string = io.getvalue() data = json.loads(out_string) @@ -431,7 +441,10 @@ def get_first_iothub(self, resource_group): with output_io_cls() as io: result = self.invoke_az_cli_outproc( - ["iot", "hub", "list", "--resource-group", resource_group, "--query", "[0]"], f("Could not get first IoT Hub."), io) + ["iot", "hub", "list", "--resource-group", resource_group, "--query", "[0]"], + f("Could not get first IoT Hub."), + io, + ensure_json_output=True) if result: out_string = io.getvalue() @@ -492,9 +505,10 @@ def get_iothub_connection_string(self, value, resource_group): f("Retrieving '{value}' connection string...")) with output_io_cls() as io: - result = self.invoke_az_cli_outproc(["iot", "hub", "connection-string", "show", "--hub-name", value, - "--resource-group", resource_group], - f("Could not create the IoT Hub {value} in {resource_group}."), stdout_io=io) + result = self.invoke_az_cli_outproc(["iot", "hub", "connection-string", "show", "--hub-name", value, "--resource-group", resource_group], + f("Could not create the IoT Hub {value} in {resource_group}."), + stdout_io=io, + ensure_json_output=True) if result: out_string = io.getvalue() data = json.loads(out_string) @@ -539,9 +553,10 @@ def get_device_connection_string(self, value, iothub, resource_group): f("Retrieving '{value}' connection string...")) with output_io_cls() as io: - result = self.invoke_az_cli_outproc(["iot", "hub", "device-identity", "connection-string", "show", "--device-id", value, "--hub-name", iothub, - "--resource-group", resource_group], - f("Could not locate the {value} device in {iothub} IoT Hub in {resource_group}."), stdout_io=io) + result = self.invoke_az_cli_outproc(["iot", "hub", "device-identity", "connection-string", "show", "--device-id", value, "--hub-name", iothub, "--resource-group", resource_group], + f("Could not locate the {value} device in {iothub} IoT Hub in {resource_group}."), + stdout_io=io, + ensure_json_output=True) if result: out_string = io.getvalue() data = json.loads(out_string)