When trying to get information about a virtual machine, I am met with the following error:

Traceback (most recent call last):
  File "/Users/shilpakancharla/Documents/function_app/WeedsMediaUploadTrigger/event_process.py", line 73, in <module>
    vm_data, vm_status = get_azure_vm(key.RESOURCE_GROUP, key.VIRTUAL_MACHINE_NAME)
  File "<decorator-gen-2>", line 2, in get_azure_vm
  File "/usr/local/lib/python3.9/site-packages/retry/api.py", line 73, in retry_decorator
    return __retry_internal(partial(f, *args, **kwargs), exceptions, tries, delay, max_delay, backoff, jitter,
  File "/usr/local/lib/python3.9/site-packages/retry/api.py", line 33, in __retry_internal
    return f()
  File "/Users/shilpakancharla/Documents/function_app/WeedsMediaUploadTrigger/event_process.py", line 65, in get_azure_vm
    vm_instance = compute_client.virtual_machines.get(resource_group_name, 
  File "/usr/local/lib/python3.9/site-packages/azure/mgmt/compute/v2019_12_01/operations/_virtual_machines_operations.py", line 641, in get
    map_error(status_code=response.status_code, response=response, error_map=error_map)
  File "/usr/local/lib/python3.9/site-packages/azure/core/exceptions.py", line 102, in map_error
    raise error
azure.core.exceptions.ResourceNotFoundError: Operation returned an invalid status 'Not Found'

Does this mean that get() is not a valid function for compute_client.virtual_machines? It says that it is valid in the documentation, but I am not sure why this error is arising. This is my implementation, and I use the call vm_data, vm_status = get_azure_vm(key.RESOURCE_GROUP, key.VIRTUAL_MACHINE_NAME). I am confident that all my credentials are correct.

def get_access_to_virtual_machine():
    subscription_id = key.SUBSCRIPTION_ID
    credentials = DefaultAzureCredential(authority = AzureAuthorityHosts.AZURE_GOVERNMENT, 
                                        exclude_environment_credential = True,
                                        exclude_managed_identity_credential = True,
                                        exclude_shared_token_cache_credential = True)
    client = KeyClient(vault_url = key.VAULT_URL, credential = credentials)
    compute_client = ComputeManagementClient(credentials, subscription_id, 
                                            base_url = 'https://portal.azure.us')
    return compute_client
def get_azure_vm(resource_group_name, virtual_machine_name):
    compute_client = get_access_to_virtual_machine()
    vm_instance = compute_client.virtual_machines.get(resource_group_name, 
                                                virtual_machine_name)
    vm_status = vm_instance.instance_view.statuses[1].display_status
    return vm_instance, vm_status
1

There are 1 answers

0
Stanley Gong On

You should specify expand , if you want to get a VM instance View . Try the code below :

compute_client.virtual_machines.get(resource_group_name, virtual_machine_name,expand='instanceView').instance_view.statuses[1].display_status

Result: enter image description here