为azure python delete_by_id方法获取正确的api版本

Dys*_*roz 4 python cloud api azure

我正在研究根据附加到这些资源的标签自动删除 Azure 资源。

我正在使用 Azure SDK for python ( https://github.com/Azure/azure-sdk-for-python ) - 我找到了如何获取我的资源列表,并且我可以使用ResourceManagementClientresources.delete_by_id删除它们方法。但是,此方法需要 2 个参数 - 资源 ID(我从ResourceManagementClient列出的资源中获取)和 API 版本(每种资源类型都不同)。

如何确定应将哪个 API 版本传递给该方法?我试图在SDK的文档和代码中找到一些东西,但我无法找到合适的解决方案。

API 版本甚至可以进行硬编码,但它需要适用于所有资源类型。

当使用某些 api 版本(例如 2018-05-01)时,我收到某些资源类型的错误:

Azure Error: NoRegisteredProviderFound
Message: No registered resource provider found for location 'westeurope' and API version '['2018-05-01']' for type 'virtualMachines'. The supported api-versions are '2015-05-01-preview, 2015-06-15, 2016-03-30, 2016-04-30-preview, 2016-08-30, 2017-03-30, 2017-12-01, 2018-04-01, 2018-06-01, 2018-10-01, 2019-03-01'. The supported locations are 'eastus, eastus2, westus, centralus, northcentralus, southcentralus, northeurope, westeurope, eastasia, southeastasia, japaneast, japanwest, australiaeast, australiasoutheast, brazilsouth, southindia, centralindia, westindia, canadacentral, canadaeast, westus2, westcentralus, uksouth, ukwest, koreacentral, koreasouth, francecentral, southafricanorth'.
ERROR: 'CloudError' object has no attribute '__traceback__'
Run Code Online (Sandbox Code Playgroud)

Lau*_*uel 5

我建议采用与 CLI 实现相同的方法,对 ARM 进行初始调用以获取从资源提供程序/资源类型到 API 版本的可能映射,并使用它在调用中注入正确的 api 版本。

得到这个映射将是列表提供者调用

添加 Mgmt 示例存储库,查找 ResourceManagementClient: https: //github.com/Azure-Samples/azure-samples-python-management

编辑:我在 MS 的 Python SDK 团队工作。