gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 13:27:04 25 4
gpt4 key购买 nike

我当前正在编写一个用于访问有关 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: SubscriptionNotFoundMessage: 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com