Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 32 additions & 11 deletions src/azure-cli/azure/cli/command_modules/acs/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -3343,22 +3343,46 @@ def aks_command_result(cmd, client, resource_group_name, name, command_id=""):
commandResult = client.get_command_result(
resource_group_name, name, command_id)
if commandResult is None:
_aks_command_result_in_progess_helper(
client, resource_group_name, name, command_id)
return
return _aks_command_result_in_progess_helper(
client, resource_group_name, name, command_id,
return_object=_output_format_specified(cmd.cli_ctx))
return _print_command_result(cmd.cli_ctx, commandResult)


def _aks_command_result_in_progess_helper(client, resource_group_name, name, command_id):
def _output_format_specified(cli_ctx):
# cli_ctx.data['safe_params'] contains list of parameter name user typed in, without value.
# cli core also use this calculate ParameterSetName header for all http request from cli.
safe_params = cli_ctx.data['safe_params']
return safe_params is None or "-o" in safe_params or "--output" in safe_params


def _aks_command_result_in_progess_helper(client, resource_group_name, name, command_id, return_object=False):
# return_object: return the result instead of printing it, for a caller whose return value is rendered
# with the output format the user specified. "invoke --no-wait" cannot use it, as the return value of
# a command is discarded when --no-wait is set.
# pylint: disable=unused-argument
def command_result_direct_response_handler(pipeline_response, *args, **kwargs):
def command_result_direct_response_handler(pipeline_response, deserialized, *args, **kwargs):
if return_object and deserialized is not None:
# the command finished since the caller's request: return the complete result
return deserialized
deserialized_data = pipeline_response.context.get(
"deserialized_data", {})
if deserialized_data:
provisioning_state = deserialized_data.get(
"properties", {}).get("provisioningState", None)
started_at = deserialized_data.get(
"properties", {}).get("startedAt", None)
if return_object:
# same keys as a finished command result, the ones that are not known yet are None
return {
"id": command_id,
"provisioningState": provisioning_state,
"startedAt": started_at,
"finishedAt": None,
"exitCode": None,
"logs": None,
"reason": None,
}
print(
f"command id: {command_id}, started at: {started_at}, status: {provisioning_state}")
print(
Expand All @@ -3368,16 +3392,13 @@ def command_result_direct_response_handler(pipeline_response, *args, **kwargs):
else:
print(
f"failed to fetch command result for command id: {command_id}")
client.get_command_result(
return None
return client.get_command_result(
resource_group_name, name, command_id, cls=command_result_direct_response_handler)


def _print_command_result(cli_ctx, commandResult):
# cli_ctx.data['safe_params'] contains list of parameter name user typed in, without value.
# cli core also use this calculate ParameterSetName header for all http request from cli.
if (cli_ctx.data['safe_params'] is None or
"-o" in cli_ctx.data['safe_params'] or
"--output" in cli_ctx.data['safe_params']):
if _output_format_specified(cli_ctx):
# user specified output format, honor their choice, return object to render pipeline
return commandResult

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3026,6 +3026,39 @@ def test_aks_agentpool_auto_scale_delete_success(self):


class TestRunCommand(unittest.TestCase):
@staticmethod
def _in_progress_client(finished_result=None):
# the service answers 202 with no result object while the command is still running.
# finished_result: what the second request gets, when the command finished in the meantime
def get_command_result(resource_group_name, name, command_id, cls=None):
if cls is None:
return None
response = mock.Mock(context={"deserialized_data": {
"properties": {"provisioningState": "Running", "startedAt": "start"}}})
return cls(response, finished_result, {})
return mock.Mock(get_command_result=get_command_result)

def test_command_result_in_progress_returns_object_for_explicit_output(self):
cmd = mock.Mock(cli_ctx=mock.Mock(data={"safe_params": ["--command-id", "--output"]}))
result = acs_custom.aks_command_result(cmd, self._in_progress_client(), "rg", "name", "id")
self.assertEqual(result["id"], "id")
self.assertEqual(result["provisioningState"], "Running")
self.assertEqual(result["startedAt"], "start")
self.assertIsNone(result["exitCode"])

def test_command_result_returns_result_finished_between_requests(self):
cmd = mock.Mock(cli_ctx=mock.Mock(data={"safe_params": ["--command-id", "--output"]}))
finished = mock.Mock(provisioning_state="Succeeded", exit_code=3)
result = acs_custom.aks_command_result(cmd, self._in_progress_client(finished), "rg", "name", "id")
self.assertIs(result, finished)

def test_command_result_in_progress_prints_without_explicit_output(self):
cmd = mock.Mock(cli_ctx=mock.Mock(data={"safe_params": ["--command-id"]}))
with mock.patch("builtins.print") as mock_print:
result = acs_custom.aks_command_result(cmd, self._in_progress_client(), "rg", "name", "id")
self.assertIsNone(result)
self.assertIn("status: Running", mock_print.call_args_list[0][0][0])

def test_get_command_context_invalid_file(self):
with self.assertRaises(CLIError) as cm:
_get_command_context([get_test_data_file_path("notexistingfile")])
Expand Down