Skip to content

Commit 7574ddc

Browse files
committed
Migrate code
1 parent 907df21 commit 7574ddc

2 files changed

Lines changed: 33 additions & 31 deletions

File tree

src/serial-console/azext_serialconsole/_client_factory.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@
66
from azure.cli.core.profiles import ResourceType
77

88

9-
def _compute_client_factory(cli_ctx, **kwargs):
10-
from azure.cli.core.commands.client_factory import get_mgmt_service_client
11-
return get_mgmt_service_client(cli_ctx, ResourceType.MGMT_COMPUTE,
12-
subscription_id=kwargs.get('subscription_id'),
13-
aux_subscriptions=kwargs.get('aux_subscriptions'))
14-
15-
169
def cf_serialconsole(cli_ctx, **kwargs):
1710
from azure.cli.core.commands.client_factory import get_mgmt_service_client
1811
from azext_serialconsole.vendored_sdks.serialconsole import MicrosoftSerialConsoleClient

src/serial-console/azext_serialconsole/custom.py

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from azure.cli.core.azclierror import ForbiddenError
2020
from azure.cli.core._profile import Profile
2121
from azure.core.exceptions import ResourceNotFoundError as ComputeClientResourceNotFoundError
22-
from azext_serialconsole._client_factory import _compute_client_factory
2322
from azext_serialconsole._client_factory import cf_serialconsole
2423
from azext_serialconsole._client_factory import cf_serial_port
2524

@@ -680,21 +679,27 @@ def disable_serialconsole(cmd):
680679

681680

682681
def get_region_from_storage_account(cli_ctx, resource_group_name, vm_vmss_name, vmss_instanceid):
682+
from azure.cli.command_modules.vm.operations.vmss_vms import VMSSVMSShow
683+
from azure.cli.command_modules.vm.operations.vm import VMShow
684+
from azure.cli.command_modules.vm.operations.vmss import VMSSShow
683685
from azext_serialconsole._client_factory import storage_client_factory
684686
from knack.log import get_logger
685687

686688
logger = get_logger(__name__)
687-
result = None
688689
storage_account_region = None
689-
client = _compute_client_factory(cli_ctx)
690690
scf = storage_client_factory(cli_ctx)
691691

692692
if vmss_instanceid:
693-
result_data = client.virtual_machine_scale_set_vms.get_instance_view(
694-
resource_group_name, vm_vmss_name, vmss_instanceid)
693+
command_args = {
694+
'instance_id': vmss_instanceid,
695+
'resource_group': resource_group_name,
696+
'vm_scale_set_name': vm_vmss_name,
697+
'expand': 'instanceView'
698+
}
699+
result_data = VMSSVMSShow(cli_ctx=cli_ctx)(command_args=command_args)
695700
result = result_data
696701

697-
if result_data.boot_diagnostics is None:
702+
if result_data.get('bootDiagnostics') is None:
698703
error_message = "Azure Serial Console requires boot diagnostics to be enabled."
699704
recommendation = ('Use "az vmss update --name MyScaleSet --resource-group MyResourceGroup --set '
700705
'virtualMachineProfile.diagnosticsProfile="{\\"bootDiagnostics\\": {\\"Enabled\\" : '
@@ -704,40 +709,44 @@ def get_region_from_storage_account(cli_ctx, resource_group_name, vm_vmss_name,
704709
'MyScaleSet -g MyResourceGroup --instance-ids *".')
705710
raise AzureConnectionError(
706711
error_message, recommendation=recommendation)
707-
if result.boot_diagnostics is not None:
708-
logger.debug(result.boot_diagnostics)
709-
if result.boot_diagnostics.console_screenshot_blob_uri is not None:
710-
storage_account_url = result.boot_diagnostics.console_screenshot_blob_uri
712+
if result.get('bootDiagnostics') is not None:
713+
logger.debug(result.get('bootDiagnostics'))
714+
if result.get('bootDiagnostics', {}).get('consoleScreenshotBlobUri') is not None:
715+
storage_account_url = result['bootDiagnostics']['consoleScreenshotBlobUri']
711716
storage_account_region = get_storage_account_info(storage_account_url, scf)
712717
else:
713718
try:
714-
result_data = client.virtual_machines.get(
715-
resource_group_name, vm_vmss_name, expand='instanceView')
719+
command_args = {
720+
'resource_group': resource_group_name,
721+
'vm_name': vm_vmss_name,
722+
'expand': 'instanceView'
723+
}
724+
result_data = VMShow(cli_ctx=cli_ctx)(command_args=command_args)
716725
result = result_data
717726
except ComputeClientResourceNotFoundError as e:
718727
try:
719-
client.virtual_machine_scale_sets.get(resource_group_name, vm_vmss_name)
728+
command_args = {
729+
'resource_group': resource_group_name,
730+
'vm_scale_set_name': vm_vmss_name
731+
}
732+
VMSSShow(cli_ctx=cli_ctx)(command_args=command_args)
720733
except ComputeClientResourceNotFoundError:
721734
raise e from e
722735
error_message = e.message
723736
recommendation = ("We found that you specified a Virtual Machine Scale Set and not a VM. "
724737
"Use the --instance-id parameter to select the VMSS instance you want to connect to.")
725-
raise ResourceNotFoundError(
726-
error_message, recommendation=recommendation) from e
738+
raise ResourceNotFoundError(error_message, recommendation=recommendation) from e
727739

728-
if (result.diagnostics_profile is None or
729-
result.diagnostics_profile.boot_diagnostics is None or
730-
not result.diagnostics_profile.boot_diagnostics.enabled):
740+
if (result.get('diagnosticsProfile', {}).get('bootDiagnostics') is None or
741+
not result['diagnosticsProfile']['bootDiagnostics'].get('enabled')):
731742
error_message = "Azure Serial Console requires boot diagnostics to be enabled."
732743
recommendation = ('Use "az vm boot-diagnostics enable --name MyVM --resource-group MyResourceGroup" '
733744
'to enable boot diagnostics. You can specify a custom storage account with the '
734745
'parameter "--storage https://mystor.blob.windows.net/".')
735-
raise AzureConnectionError(
736-
error_message, recommendation=recommendation)
737-
if result.diagnostics_profile is not None:
738-
if result.diagnostics_profile.boot_diagnostics is not None:
739-
storage_account_url = result.diagnostics_profile.boot_diagnostics.storage_uri
740-
storage_account_region = get_storage_account_info(storage_account_url, scf)
746+
raise AzureConnectionError(error_message, recommendation=recommendation)
747+
if result.get('diagnosticsProfile', {}).get('bootDiagnostics') is not None:
748+
storage_account_url = result['diagnosticsProfile']['bootDiagnostics'].get('storageUri')
749+
storage_account_region = get_storage_account_info(storage_account_url, scf)
741750

742751
return result, storage_account_region
743752

0 commit comments

Comments
 (0)