python - 收到错误代码 : SubscriptionNotFound Message: The subscription not found when trying to get data about Azure Virtual Machine?

标签 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():
    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

当尝试运行我确信确实具有正确凭据的 get_azure_vm(key.RESOURCE_GROUP, key.VIRTUAL_MACHINE_NAME) 时,我收到以下错误输出(请注意,我将实际订阅 ID 替换为现在为“xxxx”):

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.

我使用的是 azure.mgmt.compute 的 Beta 预览版,它是通过 pip install azure-mgmt-compute=17.0.0b1 安装的。请注意,我还使用 Azure 政府帐户。有办法解决这个错误吗?我还尝试过使用 ServicePrincipalCredentialsget_azure_credentials() 但遇到了不同的错误 - 同事建议我使用 DefaultAzureCredentials 和 key 保管库.

最佳答案

代码没有问题,在我这边运行得很好。并且错误消息显示了原因:

azure.core.exceptions.ResourceNotFoundError: (SubscriptionNotFound) The subscription 'xxxx' could not be found. Code: SubscriptionNotFound Message: The subscription 'xxxx' could not be found.

看来您在本地计算机上运行了 python 代码。我建议您先使用 Azure CLI 登录,然后检查您在 python 代码中使用的订阅 ID 是否正确。

关于python - 收到错误代码 : SubscriptionNotFound Message: The subscription not found when trying to get data about Azure Virtual Machine?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67808019/

相关文章:

python - 在单元测试中使用 doctests

python - 将多列中的值汇总到字典中定义的组中

azure - 设备未以 xamarin 形式在 azure 推送通知中心注册

azure - 仅对资源组具有 READER 访问权限的用户是否仍可以使用资源(不是管理,而是实际使用)

azure - 通过 ARM 模板使用 Http5xx 警报创建 Azure 函数失败

python - 如何漂亮地打印 JSON 文件?

python - Numpy:具有不同值的索引样本组

azure - 在 Azure Function 中处理 OneDrive 文件后是否可以删除该文件?

c# - 通过 C# 代码扩展 azure 函数

Azure:编排文件传输 - 哪些 Azure 组件最适合?