Python 接收错误代码:SubscriptionNotFound消息:尝试获取有关Azure虚拟机的数据时未找到订阅?
我目前正在编写一个脚本,用于访问Azure虚拟机的详细信息。这是我目前掌握的代码:Python 接收错误代码:SubscriptionNotFound消息:尝试获取有关Azure虚拟机的数据时未找到订阅?,python,azure,azure-functions,azure-virtual-machine,azure-sdk-python,Python,Azure,Azure Functions,Azure Virtual Machine,Azure Sdk Python,我目前正在编写一个脚本,用于访问Azure虚拟机的详细信息。这是我目前掌握的代码: """ Instantiate the ComputeManagementClient with the appropriate credentials. @return ComputeManagementClient object """ def get_access_to_virtual_machine(): subscri
"""
Instantiate the ComputeManagementClient with the appropriate credentials.
@return ComputeManagementClient object
"""
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)
return compute_client
"""
Check to see if Azure Virtual Machine exists and the state of the virtual machine.
"""
def get_azure_vm(resource_group_name, virtual_machine_name):
compute_client = get_access_to_virtual_machine()
vm_data = compute_client.virtual_machines.get(resource_group_name,
virtual_machine_name,
expand = 'instanceView')
return vm_data
尝试运行获取azure\u虚拟机(key.RESOURCE\u组,key.VIRTUAL\u机器\u名称)
时(我确信该虚拟机具有正确的凭据),我得到以下错误输出(请注意,我现在用“xxxx”替换了实际订阅ID):
回溯(最近一次呼叫最后一次):
文件“/Users/shilpakancharla/Documents/function_app/WeedsMediaUploadTrigger/event_process.py”,第62行,在
vm\u data=get\u azure\u vm(key.RESOURCE\u组,key.VIRTUAL\u机器\u名称)
文件“”,第2行,在get_azure_vm中
文件“/usr/local/lib/python3.9/site packages/retry/api.py”,第73行,在retry\u decorator中
返回重试内部(部分(f,*args,**kwargs),异常,尝试,延迟,最大延迟,退避,抖动,
文件“/usr/local/lib/python3.9/site packages/retry/api.py”,第33行,在“retry\u internal”中
返回f()
文件“/Users/shilpakancharla/Documents/function\u app/WeedsMediaUploadTrigger/event\u process.py”,第55行,在get\u azure\u vm中
vm\u data=compute\u client.virtual\u machines.get(资源组名称,
文件“/usr/local/lib/python3.9/site packages/azure/mgmt/compute/v2019\u 12\u 01/operations/\u virtual\u machines\u operations.py”,第641行,在get中
映射错误(状态代码=响应。状态代码,响应=响应,错误映射=错误映射)
文件“/usr/local/lib/python3.9/site packages/azure/core/exceptions.py”,第102行,映射错误
提出错误
azure.core.exceptions.ResourceNotFoundError:(SubscriptionNotFound)找不到订阅“xxxx”。
代码:SubscriptionNotFound
消息:找不到订阅“xxxx”。
我使用的是
azure.mgmt.compute
的beta预览版,它是与pip install azure mgmt compute=17.0.0b1
一起安装的。请注意,我还使用了一个azure政府帐户。是否有办法解决此错误?我还尝试了使用ServicePrincipalCredentials
和获取azure\u凭据()
但遇到了不同的错误-同事建议我使用DefaultAzureCredentials
和密钥保管库。代码没有问题,在我这方面工作正常。错误消息显示了原因:
azure.core.exceptions.ResourceNotFoundError:(SubscriptionNotFound)
找不到订阅“xxxx”。代码:SubscriptionNotFound
消息:找不到订阅“xxxx”
您似乎在本地计算机上运行python代码。我建议您先使用Azure CLI登录,然后检查您在python代码中使用的订阅id是否正确。此问题有任何更新吗?它解决了您的问题吗?如果您有任何答复,请告诉我。
Traceback (most recent call last):
File "/Users/shilpakancharla/Documents/function_app/WeedsMediaUploadTrigger/event_process.py", line 62, in <module>
vm_data = 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 55, in get_azure_vm
vm_data = 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: (SubscriptionNotFound) The subscription 'xxxx' could not be found.
Code: SubscriptionNotFound
Message: The subscription 'xxxx' could not be found.