Skip to content

Commit 4e51ecd

Browse files
authored
Merge pull request #598 from roel4ez/bugfix/597-use-json-output
fix: force json output for results that get parsed
2 parents 31683b4 + 37b78f1 commit 4e51ecd

1 file changed

Lines changed: 33 additions & 18 deletions

File tree

iotedgedev/azurecli.py

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,17 @@ def decode(self, val):
3535
def is_posix(self):
3636
return self.envvars.is_posix()
3737

38-
def prepare_az_cli_args(self, args, suppress_output=False):
38+
def prepare_az_cli_args(self, args, suppress_output=False, ensure_json_output=False):
3939
if suppress_output:
4040
args.extend(["--query", "\"[?n]|[0]\""])
4141

42+
if ensure_json_output:
43+
args.extend(["--output", "json"])
44+
4245
az_args = ["az"] + args
4346
return az_args
4447

45-
def invoke_az_cli_outproc(self, args, error_message=None, stdout_io=None, stderr_io=None, suppress_output=False, timeout=None):
48+
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):
4649
try:
4750
if timeout:
4851
timeout = int(timeout)
@@ -54,19 +57,19 @@ def invoke_az_cli_outproc(self, args, error_message=None, stdout_io=None, stderr
5457

5558
# Consider using functools
5659
if monitor_events:
57-
process = subprocess.Popen(self.prepare_az_cli_args(args, suppress_output),
60+
process = subprocess.Popen(self.prepare_az_cli_args(args, suppress_output, ensure_json_output),
5861
shell=not self.is_posix(),
5962
stdout=subprocess.PIPE,
6063
stderr=subprocess.PIPE,
6164
preexec_fn=os.setsid if self.is_posix() else None,
6265
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP if not self.is_posix() else 0)
6366
elif stdout_io or stderr_io:
64-
process = subprocess.Popen(self.prepare_az_cli_args(args, suppress_output),
67+
process = subprocess.Popen(self.prepare_az_cli_args(args, suppress_output, ensure_json_output),
6568
stdout=subprocess.PIPE,
6669
stderr=subprocess.PIPE,
6770
shell=not self.is_posix())
6871
else:
69-
process = subprocess.Popen(self.prepare_az_cli_args(args, suppress_output),
72+
process = subprocess.Popen(self.prepare_az_cli_args(args, suppress_output, ensure_json_output),
7073
shell=not self.is_posix())
7174

7275
self.process = process
@@ -227,8 +230,9 @@ def user_has_logged_in(self):
227230
self.output.status(f("Retrieving Azure CLI credentials from cache..."))
228231

229232
with output_io_cls() as io:
230-
result = self.invoke_az_cli_outproc(
231-
["account", "show"], stdout_io=io)
233+
result = self.invoke_az_cli_outproc(["account", "show"],
234+
stdout_io=io,
235+
ensure_json_output=True)
232236

233237
if result:
234238
try:
@@ -270,7 +274,9 @@ def get_default_subscription(self):
270274

271275
with output_io_cls() as io:
272276
result = self.invoke_az_cli_outproc(["account", "show"],
273-
"Error while trying to get the default Azure subscription id.", io)
277+
"Error while trying to get the default Azure subscription id.",
278+
io,
279+
ensure_json_output=True)
274280
if result:
275281
out_string = io.getvalue()
276282
data = json.loads(out_string)
@@ -281,12 +287,13 @@ def get_subscription_id_starts_with(self, token):
281287
with output_io_cls() as io:
282288
query = get_query_argument_for_id_and_name(token)
283289
result = self.invoke_az_cli_outproc(["account", "list", "--query", query],
284-
"Could not find a subscription for which the id starts with or name contains '{0}'".format(token), io)
290+
"Could not find a subscription for which the id starts with or name contains '{0}'".format(token),
291+
io,
292+
ensure_json_output=True)
285293

286294
if result:
287295
out_string = io.getvalue()
288296
if out_string:
289-
290297
data = json.loads(out_string)
291298
if len(data) == 1:
292299
return data[0]["id"]
@@ -418,7 +425,10 @@ def monitor_events(self, device_id, connection_string, hub_name, timeout=300):
418425
def get_free_iothub(self):
419426
with output_io_cls() as io:
420427

421-
result = self.invoke_az_cli_outproc(["iot", "hub", "list"], f("Could not list IoT Hubs in subscription."), stdout_io=io)
428+
result = self.invoke_az_cli_outproc(["iot", "hub", "list"],
429+
f("Could not list IoT Hubs in subscription."),
430+
stdout_io=io,
431+
ensure_json_output=True)
422432
if result:
423433
out_string = io.getvalue()
424434
data = json.loads(out_string)
@@ -431,7 +441,10 @@ def get_first_iothub(self, resource_group):
431441

432442
with output_io_cls() as io:
433443
result = self.invoke_az_cli_outproc(
434-
["iot", "hub", "list", "--resource-group", resource_group, "--query", "[0]"], f("Could not get first IoT Hub."), io)
444+
["iot", "hub", "list", "--resource-group", resource_group, "--query", "[0]"],
445+
f("Could not get first IoT Hub."),
446+
io,
447+
ensure_json_output=True)
435448

436449
if result:
437450
out_string = io.getvalue()
@@ -492,9 +505,10 @@ def get_iothub_connection_string(self, value, resource_group):
492505
f("Retrieving '{value}' connection string..."))
493506

494507
with output_io_cls() as io:
495-
result = self.invoke_az_cli_outproc(["iot", "hub", "connection-string", "show", "--hub-name", value,
496-
"--resource-group", resource_group],
497-
f("Could not create the IoT Hub {value} in {resource_group}."), stdout_io=io)
508+
result = self.invoke_az_cli_outproc(["iot", "hub", "connection-string", "show", "--hub-name", value, "--resource-group", resource_group],
509+
f("Could not create the IoT Hub {value} in {resource_group}."),
510+
stdout_io=io,
511+
ensure_json_output=True)
498512
if result:
499513
out_string = io.getvalue()
500514
data = json.loads(out_string)
@@ -539,9 +553,10 @@ def get_device_connection_string(self, value, iothub, resource_group):
539553
f("Retrieving '{value}' connection string..."))
540554

541555
with output_io_cls() as io:
542-
result = self.invoke_az_cli_outproc(["iot", "hub", "device-identity", "connection-string", "show", "--device-id", value, "--hub-name", iothub,
543-
"--resource-group", resource_group],
544-
f("Could not locate the {value} device in {iothub} IoT Hub in {resource_group}."), stdout_io=io)
556+
result = self.invoke_az_cli_outproc(["iot", "hub", "device-identity", "connection-string", "show", "--device-id", value, "--hub-name", iothub, "--resource-group", resource_group],
557+
f("Could not locate the {value} device in {iothub} IoT Hub in {resource_group}."),
558+
stdout_io=io,
559+
ensure_json_output=True)
545560
if result:
546561
out_string = io.getvalue()
547562
data = json.loads(out_string)

0 commit comments

Comments
 (0)