快速入門:建立 Azure 自訂資源提供者和部署自訂資源
在本快速入門中,您會建立自定義資源提供者,並部署該資源提供者的自定義資源。 如需自定義資源提供者的詳細資訊,請參閱 Azure 自定義資源提供者概觀。
必要條件
備妥環境以使用 Azure CLI。
在 Azure Cloud Shell 中使用 Bash 環境。 如需詳細資訊,請參閱 Azure Cloud Shell 中的 Bash 快速入門。
若要在本地執行 CLI 參考命令,請安裝 Azure CLI。 若您在 Windows 或 macOS 上執行,請考慮在 Docker 容器中執行 Azure CLI。 如需詳細資訊,請參閱〈如何在 Docker 容器中執行 Azure CLI〉。
如果您使用的是本機安裝,請使用 az login 命令,透過 Azure CLI 來登入。 請遵循您終端機上顯示的步驟,完成驗證程序。 如需其他登入選項,請參閱使用 Azure CLI 登入。
出現提示時,請在第一次使用時安裝 Azure CLI 延伸模組。 如需擴充功能詳細資訊,請參閱使用 Azure CLI 擴充功能。
執行 az version 以尋找已安裝的版本和相依程式庫。 若要升級至最新版本,請執行 az upgrade。
Azure CLI 範例會針對REST
要求使用az rest
。 如需詳細資訊,請參閱 az rest。
部署自訂資源提供者
若要設定自定義資源提供者,請將範例範本部署至您的 Azure 訂用帳戶。
此樣本會將下列資源部署到您的訂用帳戶:
- 具有資源和動作作業的函式應用程式。
- 儲存體 帳戶,用來儲存透過自定義資源提供者建立的使用者。
- 定義自訂資源類型和動作的自訂資源提供者。 它會使用函式應用程式端點來傳送要求。
- 來自自訂資源提供者的自定義資源。
若要部署自定義資源提供者,請使用 Azure CLI、PowerShell 或 Azure 入口網站。
這個範例會提示您輸入資源群組、位置和提供者的函式應用程式名稱。 這些名稱會儲存在其他命令中使用的變數中。 az group create 和 az deployment group create 命令會部署資源。
read -p "Enter a resource group name:" rgName &&
read -p "Enter the location (i.e. eastus):" location &&
read -p "Enter the provider's function app name:" funcName &&
templateUri="https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/custom-providers/customprovider.json" &&
az group create --name $rgName --location "$location" &&
az deployment group create --resource-group $rgName --template-uri $templateUri --parameters funcName=$funcName &&
echo "Press [ENTER] to continue ..." &&
read
若要從 Azure 入口網站 部署範本,請選取 [部署至 Azure] 按鈕。
檢視自定義資源提供者和資源
在入口網站中,自定義資源提供者是隱藏的資源類型。 若要確認已部署資源提供者,請移至資源群組,然後選取 [ 顯示隱藏類型]。
若要查看您所部署的自訂資源,請使用 GET
資源類型的作業。 JSON 回應中顯示的資源類型 Microsoft.CustomProviders/resourceProviders/users
包含範本所建立的資源。
GET https://management.azure.com/subscriptions/<sub-id>/resourceGroups/<rg-name>/providers/Microsoft.CustomProviders/resourceProviders/<provider-name>/users?api-version=2018-09-01-preview
subID=$(az account show --query id --output tsv)
requestURI="https://management.azure.com/subscriptions/$subID/resourceGroups/$rgName/providers/Microsoft.CustomProviders/resourceProviders/$funcName/users?api-version=2018-09-01-preview"
az rest --method get --uri $requestURI
您會收到回應:
{
"value": [
{
"id": "/subscriptions/<sub-id>/resourceGroups/<rg-name>/providers/Microsoft.CustomProviders/resourceProviders/<provider-name>/users/ana",
"name": "ana",
"properties": {
"FullName": "Ana Bowman",
"Location": "Moon",
"provisioningState": "Succeeded"
},
"type": "Microsoft.CustomProviders/resourceProviders/users"
}
]
}
通話動作
您的自訂資源提供者也有名為 ping
的動作。 處理要求的程式代碼會在函式應用程式中實作。 動作 ping
會以問候語回復。
若要傳送 ping
要求,請在 POST
動作上使用 作業。
POST https://management.azure.com/subscriptions/<sub-id>/resourceGroups/<rg-name>/providers/Microsoft.CustomProviders/resourceProviders/<provider-name>/ping?api-version=2018-09-01-preview
pingURI="https://management.azure.com/subscriptions/$subID/resourceGroups/$rgName/providers/Microsoft.CustomProviders/resourceProviders/$funcName/ping?api-version=2018-09-01-preview"
az rest --method post --uri $pingURI
您會收到回應:
{
"message": "hello <function-name>.azurewebsites.net",
"pingcontent": {
"source": "<function-name>.azurewebsites.net"
}
}
使用 PUT 建立資源
在本快速入門中,範本使用資源類型 Microsoft.CustomProviders/resourceProviders/users
來部署資源。 您也可以使用 PUT
作業來建立資源。 例如,如果未使用範本部署資源,作業 PUT
將會建立資源。
在此範例中,因為範本已經部署資源,因此 PUT
作業會建立新的資源。
PUT https://management.azure.com/subscriptions/<sub-id>/resourceGroups/<rg-name>/providers/Microsoft.CustomProviders/resourceProviders/<provider-name>/users/<resource-name>?api-version=2018-09-01-preview
{"properties":{"FullName": "Test User", "Location": "Earth"}}
addURI="https://management.azure.com/subscriptions/$subID/resourceGroups/$rgName/providers/Microsoft.CustomProviders/resourceProviders/$funcName/users/testuser?api-version=2018-09-01-preview"
az rest --method put --uri $addURI --body "{'properties':{'FullName': 'Test User', 'Location': 'Earth'}}"
您會收到回應:
{
"id": "/subscriptions/<sub-ID>/resourceGroups/<rg-name>/providers/Microsoft.CustomProviders/resourceProviders/<provider-name>/users/testuser",
"name": "testuser",
"properties": {
"FullName": "Test User",
"Location": "Earth",
"provisioningState": "Succeeded"
},
"type": "Microsoft.CustomProviders/resourceProviders/users"
}
您可以從檢視自定義資源提供者和資源區段重新執行GET
作業,以顯示已建立的兩個資源。 此範例顯示來自 Azure CLI 命令的輸出。
{
"value": [
{
"id": "/subscriptions/<sub-id>/resourceGroups/<rg-name>/providers/Microsoft.CustomProviders/resourceProviders/<provider-name>/users/ana",
"name": "ana",
"properties": {
"FullName": "Ana Bowman",
"Location": "Moon",
"provisioningState": "Succeeded"
},
"type": "Microsoft.CustomProviders/resourceProviders/users"
},
{
"id": "/subscriptions/<sub-id>/resourceGroups/<rg-name>/providers/Microsoft.CustomProviders/resourceProviders/<provider-name>/users/testuser",
"name": "testuser",
"properties": {
"FullName": "Test User",
"Location": "Earth",
"provisioningState": "Succeeded"
},
"type": "Microsoft.CustomProviders/resourceProviders/users"
}
]
}
自訂資源提供者命令
使用自定義提供者命令來處理您的自訂資源提供者。
列出自定義資源提供者
list
使用 命令來顯示訂用帳戶中的所有自訂資源提供者。 預設值會列出目前訂用帳戶的自定義資源提供者,或者您可以指定 --subscription
參數。 若要列出資源群組,請使用 --resource-group
參數。
az custom-providers resource-provider list --subscription $subID
[
{
"actions": [
{
"endpoint": "https://<provider-name>.azurewebsites.net/api/{requestPath}",
"name": "ping",
"routingType": "Proxy"
}
],
"id": "/subscriptions/<sub-id>/resourceGroups/<rg-name>/providers/Microsoft.CustomProviders/resourceproviders/<provider-name>",
"location": "eastus",
"name": "<provider-name>",
"provisioningState": "Succeeded",
"resourceGroup": "<rg-name>",
"resourceTypes": [
{
"endpoint": "https://<provider-name>.azurewebsites.net/api/{requestPath}",
"name": "users",
"routingType": "Proxy, Cache"
}
],
"tags": {},
"type": "Microsoft.CustomProviders/resourceproviders",
"validations": null
}
]
顯示屬性
show
使用 命令來顯示自定義資源提供者的屬性。 輸出格式類似於 list
輸出。
az custom-providers resource-provider show --resource-group $rgName --name $funcName
建立新資源
create
使用 命令來建立或更新自訂資源提供者。 這個範例會 actions
更新 和 resourceTypes
。
az custom-providers resource-provider create --resource-group $rgName --name $funcName \
--action name=ping endpoint=https://myTestSite.azurewebsites.net/api/{requestPath} routing_type=Proxy \
--resource-type name=users endpoint=https://myTestSite.azurewebsites.net/api/{requestPath} routing_type="Proxy, Cache"
"actions": [
{
"endpoint": "https://myTestSite.azurewebsites.net/api/{requestPath}",
"name": "ping",
"routingType": "Proxy"
}
],
"resourceTypes": [
{
"endpoint": "https://myTestSite.azurewebsites.net/api/{requestPath}",
"name": "users",
"routingType": "Proxy, Cache"
}
],
更新提供者的標記
此命令 update
只會更新自定義資源提供者的標籤。 在 Azure 入口網站 中,自定義資源提供者的應用程式服務會顯示標記。
az custom-providers resource-provider update --resource-group $rgName --name $funcName --tags new=tag
"tags": {
"new": "tag"
},
刪除自訂資源提供者
命令 delete
會提示您,並只刪除自定義資源提供者。 儲存體帳戶、App Service 和 App Service 方案不會刪除。 刪除提供者之後,系統會將您傳回命令提示字元。
az custom-providers resource-provider delete --resource-group $rgName --name $funcName
清除資源
如果您已完成本文中建立的資源,您可以刪除資源群組。 當您刪除資源群組時,會刪除該資源群組中的所有資源。
az group delete --resource-group $rgName
下一步
如需自定義資源提供者的簡介,請參閱下列文章: