Skip to content

Commit

Permalink
{vm-repair} Migrate off of msRestAzure api to equivalents (#8036)
Browse files Browse the repository at this point in the history
* migration dev

* logger improvement

* Update HISTORY.rst

* Update setup.py

* Update setup.py

* Update HISTORY.rst

* Update setup.py

* Update test_repair_commands.py

* Update setup.py

* Update HISTORY.rst
  • Loading branch information
Sandido authored Oct 21, 2024
1 parent 4775d23 commit d2f8d7a
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/vm-repair/HISTORY.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

Release History
===============
1.1.1
++++++
Migrated VM Repair off of the `msrestazure` API to `azure.core` and `azure.mgmt` APIs.

1.1.0
++++++
Added script for GT fixit button.
Expand Down
16 changes: 8 additions & 8 deletions src/vm-repair/azext_vm_repair/_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

from azure.cli.command_modules.vm.custom import get_vm, _is_linux_os
from azure.cli.command_modules.resource._client_factory import _resource_client_factory
from msrestazure.azure_exceptions import CloudError
from msrestazure.tools import parse_resource_id, is_valid_resource_id
from azure.core.exceptions import HttpResponseError
from azure.mgmt.core.tools import parse_resource_id, is_valid_resource_id

from .encryption_types import Encryption
from .exceptions import AzCommandError
Expand Down Expand Up @@ -245,9 +245,9 @@ def _classic_vm_exists(cmd, resource_group_name, vm_name):
api_version = _resolve_api_version(rcf, classic_vm_provider, None, vm_resource_type)
resource_client = rcf.resources
resource_client.get(resource_group_name, classic_vm_provider, '', vm_resource_type, vm_name, api_version)
except CloudError as cloudError:
except HttpResponseError as httpError:
# Resource does not exist or the API failed
logger.debug(cloudError)
logger.debug(httpError)
return False
except Exception as exception:
# Unknown error, so return false for default resource not found error message
Expand All @@ -262,13 +262,13 @@ def _validate_and_get_vm(cmd, resource_group_name, vm_name):
source_vm = None
try:
source_vm = get_vm(cmd, resource_group_name, vm_name)
except CloudError as cloudError:
logger.debug(cloudError)
if cloudError.error.error == resource_not_found_error and _classic_vm_exists(cmd, resource_group_name, vm_name):
except HttpResponseError as httpError:
logger.debug(httpError)
if httpError.error.error == resource_not_found_error and _classic_vm_exists(cmd, resource_group_name, vm_name):
# Given VM is classic VM (RDFE)
raise CLIError('The given VM \'{}\' is a classic VM. VM repair commands do not support classic VMs.'.format(vm_name))
# Unknown Error
raise CLIError(cloudError.message)
raise CLIError(httpError.message)

return source_vm

Expand Down
3 changes: 2 additions & 1 deletion src/vm-repair/azext_vm_repair/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from azure.cli.command_modules.vm.custom import get_vm, _is_linux_os
from azure.cli.command_modules.storage.storage_url_helpers import StorageResourceIdentifier
from msrestazure.tools import parse_resource_id
from azure.mgmt.core.tools import parse_resource_id
from .exceptions import AzCommandError, SkuNotAvailableError, UnmanagedDiskCopyError, WindowsOsNotAvailableError, RunScriptNotFoundForIdError, SkuDoesNotSupportHyperV, ScriptReturnsError, SupportingResourceNotFoundError, CommandCanceledByUserError

from .command_helper_class import command_helper
Expand Down Expand Up @@ -442,6 +442,7 @@ def run(cmd, vm_name, resource_group_name, run_id=None, repair_vm_id=None, custo

# If run_on_repair is False, then repair_vm is the source_vm (scripts run directly on source vm)
if run_on_repair:
logger.info('Parsing repair vm id when running on repair VM.')
repair_vm_id = parse_resource_id(repair_vm_id)
repair_vm_name = repair_vm_id['name']
repair_resource_group = repair_vm_id['resource_group']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -911,4 +911,47 @@ def test_vmrepair_TrustedLaunchVMFlag(self, resource_group):
# Check swapped OS disk
vms = self.cmd('vm list -g {rg} -o json').get_output_in_json()
source_vm = vms[0]
assert source_vm['storageProfile']['osDisk']['name'] == result['copied_disk_name']
assert source_vm['storageProfile']['osDisk']['name'] == result['copied_disk_name']

@pytest.mark.RepairVMIdAPIChange
class WindowsRunWithRepairVMId(LiveScenarioTest):

@ResourceGroupPreparer(location='westus2')
def test_vmrepair_WindowsRunWithRepairIdAPIChange(self, resource_group):
self.kwargs.update({
'vm': 'vm1',
'rg': resource_group
})

rgname = resource_group
vmname = 'vm1'

# Create test VM
self.cmd('vm create -g {} -n {} --admin-username azureadmin --admin-password !Passw0rd2024 --image Win2022Datacenter'.format(rgname, vmname))
vms = self.cmd('vm list -g {} -o json'.format(rgname)).get_output_in_json()
# Something wrong with vm create command if it fails here
assert len(vms) == 1

# Create repair vm
result = self.cmd('vm repair create -g {} -n {} --repair-username azureadmin --repair-password !Passw0rd2024 --unlock-encrypted-vm --encrypt-recovery-key !Passw0rd2024 --yes --distro Win2022Datacenter --verbose -o json'.format(rgname, vmname)).get_output_in_json()
assert result['status'] == STATUS_SUCCESS, result['error_message']

# Check repair VM
repair_vms = self.cmd('vm list -g {} -o json'.format(result['repair_resource_group'])).get_output_in_json()
assert len(repair_vms) == 1
repair_vm = repair_vms[0]
# Check attached data disk
assert repair_vm['storageProfile']['dataDisks'][0]['name'] == result['copied_disk_name']
repair_vm_id = repair_vm['id']
resourceGroup = resource_group
# az vm repair run -g $rgname -n $vmname --run-id win-hello-world --run-on-repair --repair-vm-id $resourceId --verbose --debug;
run_result = self.cmd('vm repair run -g {} -n {} --run-id win-crowdstrike-fix-bootloop-v2 --run-on-repair --repair-vm-id {}'.format(rgname, vmname, repair_vm_id))
assert result['status'] == STATUS_SUCCESS, result['error_message']

# Call Restore
self.cmd('vm repair restore -g {} -n {} --yes'.format(rgname, vmname))

# Check swapped OS disk
vms = self.cmd('vm list -g {} -o json'.format(rgname)).get_output_in_json()
source_vm = vms[0]
assert source_vm['storageProfile']['osDisk']['name'] == result['copied_disk_name']
2 changes: 1 addition & 1 deletion src/vm-repair/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from codecs import open
from setuptools import setup, find_packages

VERSION = "1.1.0"
VERSION = "1.1.1"

CLASSIFIERS = [
'Development Status :: 4 - Beta',
Expand Down

0 comments on commit d2f8d7a

Please sign in to comment.