Skip to content

Commit

Permalink
refactor: move RunCommandLocationPolling class to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
CustardTart32 committed Nov 8, 2024
1 parent 67b9c42 commit 7bbfe83
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 35 deletions.
35 changes: 35 additions & 0 deletions src/azure-cli/azure/cli/command_modules/acs/_polling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from typing import Optional

from azure.core.polling.base_polling import LocationPolling, _is_empty, BadResponse, _as_json


class RunCommandLocationPolling(LocationPolling):
"""Extends LocationPolling but uses the body content instead of the status code for the status"""

@staticmethod
def _get_provisioning_state(response: Optional[str]):
"""Attempt to get provisioning state from resource.
:param azure.core.pipeline.transport.HttpResponse response: latest REST call response.
:returns: Status if found, else 'None'.
"""
if _is_empty(response):
return None
body = _as_json(response)
return body.get("properties", {}).get("provisioningState")

def get_status(self, pipeline_response):
"""Process the latest status update retrieved from the same URL as
the previous request.
:param azure.core.pipeline.PipelineResponse response: latest REST call response.
:raises: BadResponse if status not 200 or 204.
"""
response = pipeline_response.http_response
if _is_empty(response):
raise BadResponse(
"The response from long running operation does not contain a body."
)

status = self._get_provisioning_state(response)
return status or "Succeeded"
36 changes: 1 addition & 35 deletions src/azure-cli/azure/cli/command_modules/acs/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import stat
import subprocess
import sys
from typing import Optional
import tempfile
import threading
import time
Expand Down Expand Up @@ -69,7 +68,7 @@
CONST_AZURE_SERVICE_MESH_MODE_ISTIO,
CONST_MANAGED_CLUSTER_SKU_TIER_PREMIUM,
)

from azure.cli.command_modules.acs._polling import RunCommandLocationPolling
from azure.cli.command_modules.acs._helpers import get_snapshot_by_snapshot_id, check_is_private_link_cluster
from azure.cli.command_modules.acs._resourcegroup import get_rg_location
from azure.cli.command_modules.acs._validators import extract_comma_separated_string
Expand All @@ -96,7 +95,6 @@
from azure.cli.core.commands import LongRunningOperation
from azure.cli.core.commands.client_factory import get_subscription_id
from azure.cli.core.commands.progress import PollerProgressBar
from azure.core.polling.base_polling import LocationPolling, _is_empty, BadResponse, _as_json
from azure.cli.core.profiles import ResourceType
from azure.mgmt.core.polling.arm_polling import ARMPolling
from azure.cli.core.util import in_cloud_console, sdk_no_wait
Expand Down Expand Up @@ -2031,38 +2029,6 @@ def aks_get_versions(cmd, client, location):
return client.list_kubernetes_versions(location)


class RunCommandLocationPolling(LocationPolling):
"""Extends LocationPolling but uses the body content instead of the status code for the status"""

@staticmethod
def _get_provisioning_state(response: Optional[str]):
"""Attempt to get provisioning state from resource.
:param azure.core.pipeline.transport.HttpResponse response: latest REST call response.
:returns: Status if found, else 'None'.
"""
if _is_empty(response):
return None
body = _as_json(response)
return body.get("properties", {}).get("provisioningState")

def get_status(self, pipeline_response):
"""Process the latest status update retrieved from the same URL as
the previous request.
:param azure.core.pipeline.PipelineResponse response: latest REST call response.
:raises: BadResponse if status not 200 or 204.
"""
response = pipeline_response.http_response
if _is_empty(response):
raise BadResponse(
"The response from long running operation does not contain a body."
)

status = self._get_provisioning_state(response)
return status or "Succeeded"


def aks_runcommand(cmd, client, resource_group_name, name, command_string="", command_files=None, no_wait=False):
colorama.init()

Expand Down

0 comments on commit 7bbfe83

Please sign in to comment.