1919from azure .cli .core .azclierror import ForbiddenError
2020from azure .cli .core ._profile import Profile
2121from azure .core .exceptions import ResourceNotFoundError as ComputeClientResourceNotFoundError
22- from azext_serialconsole ._client_factory import _compute_client_factory
2322from azext_serialconsole ._client_factory import cf_serialconsole
2423from azext_serialconsole ._client_factory import cf_serial_port
2524
@@ -680,21 +679,27 @@ def disable_serialconsole(cmd):
680679
681680
682681def 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