Azure 资源提供程序和类型

Azure 资源提供程序是 REST 操作的集,可实现特定 Azure 服务的功能。 例如,Key Vault 服务由名为 Microsoft.KeyVault 的资源提供程序组成。 资源提供程序定义管理保管库、机密、密钥和证书的 REST 操作

资源提供程序定义你可以部署到你的帐户的 Azure 资源。 资源类型的名称采用以下格式:{resource-provider}/{resource-type}。 Key Vault 的资源类型为 Microsoft.KeyVault/vaults

在本文中,学习如何:

  • 查看 Azure 中的所有资源提供程序
  • 检查资源提供程序的注册状态
  • 注册资源提供程序
  • 查看资源提供程序的资源类型
  • 查看资源类型的有效位置
  • 查看资源类型的有效 API 版本

有关将资源提供程序映射到 Azure 服务的列表,请参阅 Azure 服务的资源提供程序

注册资源提供程序

在使用资源提供程序之前,必须确保已为资源提供程序注册 Azure 订阅。 注册会配置你的订阅,使之与资源提供程序配合工作。

重要

请仅在准备好使用资源提供程序时注册该程序。 此注册步骤有助于在订阅中保持最低权限。 恶意用户无法使用未注册的资源提供程序。

注册不必要的资源提供程序可能会导致 Microsoft Entra 租户中出现无法识别的应用。 当你注册资源提供程序时,Microsoft 会为它添加应用。 这些应用通常是由 Azure 服务管理 API 添加的。 若要防止租户中出现不必要的应用,请仅注册所需的资源提供程序。

某些资源提供程序在默认情况下已注册。 有关默认情况下注册的资源提供程序的列表,请参阅 Azure 服务的资源提供程序

当你执行某些操作时,其他资源提供程序会自动注册。 当你通过门户创建资源时,系统通常会为你注册资源提供程序。 部署 Azure 资源管理器模板或 Bicep 文件时,会自动注册模板中定义的资源提供程序。 有时,模板中的资源需要模板之外的支持资源。 常见示例包括监视或安全资源。 需要手动注册这些资源提供程序。

对于其他方案,你可能需要手动注册资源提供程序。

重要

你的应用程序代码不应阻止为处于“正在注册”状态的资源提供程序创建资源 。 注册资源提供程序时,将针对每个受支持的区域单独执行该操作。 若要在某个区域中创建资源,只需在该区域中完成注册即可。 如果不阻止处于正在注册状态的资源提供程序,则你的应用程序可以以快得多的速度继续执行,无需等待所有区域都完成。

你必须具备为资源提供程序执行 /register/action 操作的权限。 此权限包含在“参与者”和“所有者”角色中。

当订阅中仍有某个资源提供程序的资源类型时,不能注销该资源提供程序。

当资源提供程序支持你需要使用的新位置时,重新注册资源提供程序。

Azure 门户

注册资源提供程序

查看所有资源提供程序和订阅的注册状态:

  1. 登录 Azure 门户

  2. 在 Azure 门户菜单上,搜索“订阅”。 从可用选项中选择它。

    Screenshot of searching for subscriptions in the Azure portal.

  3. 选择要查看的订阅。

    Screenshot of selecting a subscription in the Azure portal.

  4. 在左侧菜单中的“设置”下,选择“资源提供程序”。

    Screenshot of selecting resource providers in the Azure portal.

  5. 找到要注册的资源提供程序,然后选择“注册”。 若要在订阅中保留最小特权,请仅注册准备好使用的资源提供程序。

    Screenshot of registering a resource provider in the Azure portal.

    重要

    如前所述,不要阻止为处于正在注册状态的资源提供程序创建资源。 如果不阻止处于正在注册状态的资源提供程序,则你的应用程序可以以快得多的速度继续执行,无需等待所有区域都完成。

  6. 重新注册资源提供程序,以使用自上次注册以来新增的位置。

    Screenshot of reregistering a resource provider in the Azure portal.

查看资源提供程序

查看特定资源提供程序的信息:

  1. 登录 Azure 门户

  2. 在 Azure 门户菜单中,选择“所有服务” 。

  3. 在“所有服务” 框中,输入“资源浏览器” ,然后选择“资源浏览器” 。

    Screenshot of selecting All services in the Azure portal to access Resource Explorer.

  4. 通过选择向右箭头来展开“提供程序” 。

    Screenshot of expanding the Providers section in the Azure Resource Explorer.

  5. 展开要查看的资源提供程序和资源类型。

    Screenshot of expanding a resource provider and resource type in the Azure Resource Explorer.

  6. 所有区域都支持 Resource Manager,但部署的资源可能无法在所有区域中受到支持。 此外,订阅可能存在一些限制,以防止用户使用某些支持该资源的区域。 资源浏览器显示资源类型的有效位置。

    Screenshot of displaying valid locations for a resource type in the Azure Resource Explorer.

  7. API 版本对应于资源提供程序的 REST API 操作的版本。 资源提供程序启用新功能时,会发布 REST API 的新版本。 资源浏览器显示资源类型的有效 API 版本。

    Screenshot of displaying valid API versions for a resource type in the Azure Resource Explorer.

Azure PowerShell

若要查看 Azure 中的所有资源提供程序和订阅的注册状态,请使用:

Get-AzResourceProvider -ListAvailable | Select-Object ProviderNamespace, RegistrationState

该命令返回:

ProviderNamespace                RegistrationState
-------------------------------- ------------------
Microsoft.ClassicCompute         Registered
Microsoft.ClassicNetwork         Registered
Microsoft.ClassicStorage         Registered
Microsoft.CognitiveServices      Registered
...

若要查看订阅的所有已注册资源提供程序,请使用:

 Get-AzResourceProvider -ListAvailable | Where-Object RegistrationState -eq "Registered" | Select-Object ProviderNamespace, RegistrationState | Sort-Object ProviderNamespace

若要在订阅中保留最小特权,请仅注册准备好使用的资源提供程序。 若要注册资源提供程序,请使用:

Register-AzResourceProvider -ProviderNamespace Microsoft.Batch

该命令返回:

ProviderNamespace : Microsoft.Batch
RegistrationState : Registering
ResourceTypes     : {batchAccounts, operations, locations, locations/quotas}
Locations         : {China East, China North, China East 2, China North 2}

重要

如前所述,不要阻止为处于正在注册状态的资源提供程序创建资源。 如果不阻止处于正在注册状态的资源提供程序,则你的应用程序可以以快得多的速度继续执行,无需等待所有区域都完成。

重新注册资源提供程序,以使用自上次注册以来新增的位置。 若要重新注册,请再次运行注册命令。

若要查看特定资源提供程序的信息,请使用:

Get-AzResourceProvider -ProviderNamespace Microsoft.Batch

该命令返回:

{ProviderNamespace : Microsoft.Batch
RegistrationState : Registered
ResourceTypes     : {batchAccounts}
Locations         : {China East, China North, China East 2, China North 2}

...

若要查看资源提供程序的资源类型,请使用:

(Get-AzResourceProvider -ProviderNamespace Microsoft.Batch).ResourceTypes.ResourceTypeName

该命令返回:

batchAccounts
operations
locations
locations/quotas

API 版本对应于资源提供程序的 REST API 操作的版本。 资源提供程序启用新功能时,会发布 REST API 的新版本。

若要获取资源类型可用的 API 版本,请使用:

((Get-AzResourceProvider -ProviderNamespace Microsoft.Batch).ResourceTypes | Where-Object ResourceTypeName -eq batchAccounts).ApiVersions

该命令返回:

2023-05-01
2022-10-01
2022-06-01
2022-01-01
2021-06-01
2021-01-01
...

所有区域都支持 Resource Manager,但部署的资源可能无法在所有区域中受到支持。 此外,订阅可能存在一些限制,以防止用户使用某些支持该资源的区域。

若要获取某一资源类型的受支持位置,请使用。

((Get-AzResourceProvider -ProviderNamespace Microsoft.Batch).ResourceTypes | Where-Object ResourceTypeName -eq batchAccounts).Locations

该命令返回:

China East
China North
China East 2
China North 2
...

Azure CLI

若要查看 Azure 中的所有资源提供程序和订阅的注册状态,请使用:

az provider list --query "[].{Provider:namespace, Status:registrationState}" --out table

该命令返回:

Provider                         Status
-------------------------------- ----------------
Microsoft.ClassicCompute         Registered
Microsoft.ClassicNetwork         Registered
Microsoft.ClassicStorage         Registered
Microsoft.CognitiveServices      Registered
...

若要查看订阅的所有已注册资源提供程序,请使用:

az provider list --query "sort_by([?registrationState=='Registered'].{Provider:namespace, Status:registrationState}, &Provider)" --out table

若要在订阅中保留最小特权,请仅注册准备好使用的资源提供程序。 若要注册资源提供程序,请使用:

az provider register --namespace Microsoft.Batch

此命令将返回“正在进行注册”的消息。

若要查看特定资源提供程序的信息,请使用:

az provider show --namespace Microsoft.Batch

该命令返回:

{
    "id": "/subscriptions/####-####/providers/Microsoft.Batch",
    "namespace": "Microsoft.Batch",
    "registrationsState": "Registering",
    "resourceTypes:" [
        ...
    ]
}

重要

如前所述,不要阻止为处于正在注册状态的资源提供程序创建资源。 如果不阻止处于正在注册状态的资源提供程序,则你的应用程序可以以快得多的速度继续执行,无需等待所有区域都完成。

若要查看资源提供程序的资源类型,请使用:

az provider show --namespace Microsoft.Batch --query "resourceTypes[*].resourceType" --out table

该命令返回:

Result
---------------
batchAccounts
operations
locations
locations/quotas

API 版本对应于资源提供程序的 REST API 操作的版本。 资源提供程序启用新功能时,会发布 REST API 的新版本。

若要获取资源类型可用的 API 版本,请使用:

az provider show --namespace Microsoft.Batch --query "resourceTypes[?resourceType=='batchAccounts'].apiVersions | [0]" --out table

该命令返回:

Result
---------------
2023-05-01
2022-10-01
2022-06-01
2022-01-01
...

所有区域都支持 Resource Manager,但部署的资源可能无法在所有区域中受到支持。 此外,订阅可能存在一些限制,以防止用户使用某些支持该资源的区域。

若要获取某一资源类型的受支持位置,请使用。

az provider show --namespace Microsoft.Batch --query "resourceTypes[?resourceType=='batchAccounts'].locations | [0]" --out table

该命令返回:

Result
---------------
China East
China North
...

Python

若要查看 Azure 中的所有资源提供程序和订阅的注册状态,请使用:

import os  
from azure.identity import DefaultAzureCredential  
from azure.mgmt.resource import ResourceManagementClient  
  
# Authentication  
credential = DefaultAzureCredential()  
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]  
  
# Initialize Resource Management client  
resource_management_client = ResourceManagementClient(credential, subscription_id)  
  
# List available resource providers and select ProviderNamespace and RegistrationState  
providers = resource_management_client.providers.list()  
  
for provider in providers:  
    print(f"ProviderNamespace: {provider.namespace}, RegistrationState: {provider.registration_state}")  

该命令返回:

ProviderNamespace: Microsoft.AlertsManagement, RegistrationState: Registered
ProviderNamespace: Microsoft.AnalysisServices, RegistrationState: Registered
ProviderNamespace: Microsoft.ApiManagement, RegistrationState: Registered
ProviderNamespace: Microsoft.Authorization, RegistrationState: Registered
ProviderNamespace: Microsoft.Batch, RegistrationState: Registered
...

若要查看订阅的所有已注册资源提供程序,请使用:

# List available resource providers with RegistrationState "Registered" and select ProviderNamespace and RegistrationState  
providers = resource_management_client.providers.list()  
registered_providers = [provider for provider in providers if provider.registration_state == "Registered"]  
  
# Sort by ProviderNamespace  
sorted_registered_providers = sorted(registered_providers, key=lambda x: x.namespace)  
  
for provider in sorted_registered_providers:  
    print(f"ProviderNamespace: {provider.namespace}, RegistrationState: {provider.registration_state}")  

若要在订阅中保留最小特权,请仅注册准备好使用的资源提供程序。 若要注册资源提供程序,请使用:

import os  
from azure.identity import DefaultAzureCredential  
from azure.mgmt.resource import ResourceManagementClient  
  
# Authentication  
credential = DefaultAzureCredential()  
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]  
  
# Initialize Resource Management client  
resource_management_client = ResourceManagementClient(credential, subscription_id)  
  
# Register resource provider  
provider_namespace = "Microsoft.Batch"  
registration_result = resource_management_client.providers.register(provider_namespace)  
  
print(f"ProviderNamespace: {registration_result.namespace}, RegistrationState: {registration_result.registration_state}")  

该命令返回:

ProviderNamespace: Microsoft.Batch, RegistrationState: Registered

重要

如前所述,不要阻止为处于正在注册状态的资源提供程序创建资源。 如果不阻止处于正在注册状态的资源提供程序,则你的应用程序可以以快得多的速度继续执行,无需等待所有区域都完成。

重新注册资源提供程序,以使用自上次注册以来新增的位置。 若要重新注册,请再次运行注册命令。

若要查看特定资源提供程序的信息,请使用:

import os  
from azure.identity import DefaultAzureCredential  
from azure.mgmt.resource import ResourceManagementClient  
  
# Authentication  
credential = DefaultAzureCredential()  
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]  
  
# Initialize Resource Management client  
resource_management_client = ResourceManagementClient(credential, subscription_id)  
  
# Get resource provider by ProviderNamespace  
provider_namespace = "Microsoft.Batch"  
provider = resource_management_client.providers.get(provider_namespace)  
  
print(f"ProviderNamespace: {provider.namespace}, RegistrationState: {provider.registration_state}\n")  
  
# Add resource types, locations, and API versions with new lines to separate results  
for resource_type in provider.resource_types:  
    print(f"ResourceType: {resource_type.resource_type}\nLocations: {', '.join(resource_type.locations)}\nAPIVersions: {', '.join(resource_type.api_versions)}\n")  

该命令返回:

ProviderNamespace: Microsoft.Batch, RegistrationState: Registered

ResourceType: batchAccounts
Locations: China North, China East
APIVersions: 2023-05-01, 2022-10-01, 2022-06-01, 2022-01-01, 2021-06-01, 2021-01-01, 2020-09-01, 2020-05-01, 2020-03-01-preview, 2020-03-01, 2019-08-01, 2019-04-01, 2018-12-01, 2017-09-01, 2017-05-01, 2017-01-01, 2015-12-01, 2015-09-01, 2015-07-01, 2014-05-01-privatepreview

...

若要查看资源提供程序的资源类型,请使用:

# Get resource provider by ProviderNamespace  
provider_namespace = "Microsoft.Batch"  
provider = resource_management_client.providers.get(provider_namespace)  
  
# Get ResourceTypeName of the resource types  
resource_type_names = [resource_type.resource_type for resource_type in provider.resource_types]  
  
for resource_type_name in resource_type_names:  
    print(resource_type_name)  

该命令返回:

batchAccounts
batchAccounts/pools
batchAccounts/detectors
batchAccounts/certificates
operations
locations
locations/quotas
locations/checkNameAvailability
locations/accountOperationResults
locations/virtualMachineSkus
locations/cloudServiceSkus

API 版本对应于资源提供程序的 REST API 操作的版本。 资源提供程序启用新功能时,会发布 REST API 的新版本。

若要获取资源类型可用的 API 版本,请使用:

# Get resource provider by ProviderNamespace  
provider_namespace = "Microsoft.Batch"  
provider = resource_management_client.providers.get(provider_namespace)  
  
# Filter resource type by ResourceTypeName and get its ApiVersions  
resource_type_name = "batchAccounts"  
api_versions = [  
    resource_type.api_versions  
    for resource_type in provider.resource_types  
    if resource_type.resource_type == resource_type_name  
]  
  
for api_version in api_versions[0]:  
    print(api_version)  

该命令返回:

2023-05-01
2022-10-01
2022-06-01
2022-01-01
...

所有区域都支持 Resource Manager,但部署的资源可能无法在所有区域中受到支持。 此外,订阅可能存在一些限制,以防止用户使用某些支持该资源的区域。

若要获取某一资源类型的受支持位置,请使用。

# Get resource provider by ProviderNamespace  
provider_namespace = "Microsoft.Batch"  
provider = resource_management_client.providers.get(provider_namespace)  
  
# Filter resource type by ResourceTypeName and get its Locations  
resource_type_name = "batchAccounts"  
locations = [  
    resource_type.locations  
    for resource_type in provider.resource_types  
    if resource_type.resource_type == resource_type_name  
]  
  
for location in locations[0]:  
    print(location)  

该命令返回:

China East 2
China North 2
...

后续步骤