Skip to content

Commit

Permalink
Merge pull request #598 from roel4ez/bugfix/597-use-json-output
Browse files Browse the repository at this point in the history
fix: force json output for results that get parsed
  • Loading branch information
EliiseS authored Nov 9, 2022
2 parents 31683b4 + 37b78f1 commit 4e51ecd
Showing 1 changed file with 33 additions and 18 deletions.
51 changes: 33 additions & 18 deletions iotedgedev/azurecli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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"]
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 4e51ecd

Please sign in to comment.