gre*_*omb 5 python azure azure-virtual-machine azure-sdk-python azure-functions
我当前正在编写一个可访问有关 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
Run Code Online (Sandbox Code Playgroud)
当尝试运行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.
Run Code Online (Sandbox Code Playgroud)
我正在使用azure.mgmt.compute与pip install azure-mgmt-compute=17.0.0b1. 请注意,我还使用 Azure 政府帐户。有办法解决这个错误吗?我也尝试过使用ServicePrincipalCredentialsand但遇到了不同的错误 -一位同事get_azure_credentials()建议我使用and 密钥保管库。DefaultAzureCredentials
代码没有问题,在我这边工作得很好。并且错误消息显示了原因:
azure.core.exceptions.ResourceNotFoundError:(SubscriptionNotFound)找不到订阅“xxxx”。代码:SubscriptionNotFound 消息:找不到订阅“xxxx”。
看来你在本地机器上运行 python 代码。我建议您首先使用 Azure CLI 登录,然后检查您在 python 代码中使用的订阅 ID 是否正确。
| 归档时间: |
|
| 查看次数: |
2425 次 |
| 最近记录: |