Skip to content

Commit fd252bc

Browse files
committed
feat: use LongRunningOperation for aks command invoke
1 parent d68ba44 commit fd252bc

File tree

2 files changed

+56
-2
lines changed
  • src

2 files changed

+56
-2
lines changed

src/azure-cli-core/azure/cli/core/commands/progress.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,12 @@ def update_progress(self):
195195

196196
def end(self):
197197
self.hook.end()
198+
199+
class PollerProgressBar(IndeterminateProgressBar):
200+
def __init__(self, cli_ctx, poller, message="Running"):
201+
super().__init__(cli_ctx, message)
202+
self.poller = poller
203+
204+
def update_progress(self):
205+
self.message = self.poller.status()
206+
super().update_progress()

src/azure-cli/azure/cli/command_modules/acs/custom.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import stat
2020
import subprocess
2121
import sys
22+
from typing import Optional
2223
import tempfile
2324
import threading
2425
import time
@@ -94,7 +95,10 @@
9495
)
9596
from azure.cli.core.commands import LongRunningOperation
9697
from azure.cli.core.commands.client_factory import get_subscription_id
98+
from azure.cli.core.commands.progress import PollerProgressBar
99+
from azure.core.polling.base_polling import LocationPolling, _is_empty, BadResponse, _as_json
97100
from azure.cli.core.profiles import ResourceType
101+
from azure.mgmt.core.polling.arm_polling import ARMPolling
98102
from azure.cli.core.util import in_cloud_console, sdk_no_wait
99103
from azure.core.exceptions import ResourceNotFoundError as ResourceNotFoundErrorAzCore
100104
from azure.mgmt.containerservice.models import KubernetesSupportPlan
@@ -2027,13 +2031,46 @@ def aks_get_versions(cmd, client, location):
20272031
return client.list_kubernetes_versions(location)
20282032

20292033

2034+
class RunCommandLocationPolling(LocationPolling):
2035+
"""Extends LocationPolling but uses the body content instead of the status code for the status"""
2036+
2037+
@staticmethod
2038+
def _get_provisioning_state(response: Optional[str]):
2039+
"""Attempt to get provisioning state from resource.
2040+
2041+
:param azure.core.pipeline.transport.HttpResponse response: latest REST call response.
2042+
:returns: Status if found, else 'None'.
2043+
"""
2044+
if _is_empty(response):
2045+
return None
2046+
body = _as_json(response)
2047+
return body.get("properties", {}).get("provisioningState")
2048+
2049+
def get_status(self, pipeline_response):
2050+
"""Process the latest status update retrieved from the same URL as
2051+
the previous request.
2052+
2053+
:param azure.core.pipeline.PipelineResponse response: latest REST call response.
2054+
:raises: BadResponse if status not 200 or 204.
2055+
"""
2056+
response = pipeline_response.http_response
2057+
if _is_empty(response):
2058+
raise BadResponse(
2059+
"The response from long running operation does not contain a body."
2060+
)
2061+
2062+
status = self._get_provisioning_state(response)
2063+
return status or "Succeeded"
2064+
2065+
20302066
def aks_runcommand(cmd, client, resource_group_name, name, command_string="", command_files=None, no_wait=False):
20312067
colorama.init()
20322068

20332069
mc = client.get(resource_group_name, name)
20342070

20352071
if not command_string:
20362072
raise ValidationError('Command cannot be empty.')
2073+
20372074
RunCommandRequest = cmd.get_models('RunCommandRequest', resource_type=ResourceType.MGMT_CONTAINERSERVICE,
20382075
operation_group='managed_clusters')
20392076
request_payload = RunCommandRequest(command=command_string)
@@ -2046,8 +2083,15 @@ def aks_runcommand(cmd, client, resource_group_name, name, command_string="", co
20462083
request_payload.cluster_token = _get_dataplane_aad_token(
20472084
cmd.cli_ctx, "6dae42f8-4368-4678-94ff-3960e28e3630")
20482085

2086+
polling_interval = 5
2087+
retry_total = 0
2088+
20492089
command_result_poller = sdk_no_wait(
2050-
no_wait, client.begin_run_command, resource_group_name, name, request_payload, polling_interval=5, retry_total=0
2090+
no_wait, client.begin_run_command, resource_group_name, name, request_payload,
2091+
# NOTE: Note sure if retry_total is used in ARMPolling
2092+
polling=ARMPolling(polling_interval, lro_options={"final-state-via": "location"}, lro_algorithms=[RunCommandLocationPolling()], retry_total=retry_total),
2093+
polling_interval=polling_interval,
2094+
retry_total=retry_total
20512095
)
20522096
if no_wait:
20532097
# pylint: disable=protected-access
@@ -2058,7 +2102,8 @@ def aks_runcommand(cmd, client, resource_group_name, name, command_string="", co
20582102
command_id = command_id_regex.findall(command_result_polling_url)[0]
20592103
_aks_command_result_in_progess_helper(client, resource_group_name, name, command_id)
20602104
return
2061-
return _print_command_result(cmd.cli_ctx, command_result_poller.result(300))
2105+
2106+
return LongRunningOperation(cmd.cli_ctx, progress_bar=PollerProgressBar(cmd.cli_ctx, command_result_poller))(command_result_poller)
20622107

20632108

20642109
def aks_command_result(cmd, client, resource_group_name, name, command_id=""):

0 commit comments

Comments
 (0)