共用方式為


建立和使用自訂資源提供者

自訂資源提供者是 Azure 和端點之間的合約。 透過自訂資源提供者,您可以在 Azure 上變更工作流程。 此教學課程說明建立自訂資源提供者的流程。 如果您不熟悉 Azure 自訂資源提供者,請參閱 Azure 自訂資源提供者的概觀

建立自訂資源提供者

注意

本教學課程不會示範如何撰寫端點。 如果您沒有 RESTFUL 端點,請遵循撰寫 RESTful 端點的教學課程,這是目前教學課程的基礎。

當您建立端點之後,即可建立自訂資源提供者來產生提供者與端點之間的合約。 您可以透過自訂資源提供者來指定端點定義清單:

{
  "name": "myEndpointDefinition",
  "routingType": "Proxy",
  "endpoint": "https://<yourapp>.azurewebsites.net/api/<funcname>?code=<functionkey>"
}
屬性 必要 描述
name Yes 端點定義的名稱。 Azure 會透過其 API 在 /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.CustomProviders
/resourceProviders/{resourceProviderName}/{endpointDefinitionName} 底下公開此名稱
routingType No 端點合約類型。 如果未指定值,則會預設為 "Proxy"。
endpoint Yes 要作為要求路由傳送目的地的端點。 此端點會處理回應及要求的任何副作用。

endpoint的值是 Azure 函式應用程式的觸發程序 URL。 <yourapp><funcname><functionkey> 的預留位置必須取代為所建立函式應用程式的值。

定義自訂動作和資源

自訂資源提供者包含已在 actionsresourceTypes 屬性底下模型化的端點定義清單。 actions 屬性會對應至自訂資源提供者所公開的自訂動作,而 resourceTypes 屬性是自訂資源。 在此教學課程中,自訂資源提供者具有名為 myCustomActionactions 屬性和名為 myCustomResourcesresourceTypes 屬性。

{
  "properties": {
    "actions": [
      {
        "name": "myCustomAction",
        "routingType": "Proxy",
        "endpoint": "https://<yourapp>.azurewebsites.net/api/<funcname>?code=<functionkey>"
      }
    ],
    "resourceTypes": [
      {
        "name": "myCustomResources",
        "routingType": "Proxy",
        "endpoint": "https://<yourapp>.azurewebsites.net/api/<funcname>?code=<functionkey>"
      }
    ]
  },
  "location": "eastus"
}

部署自訂資源提供者

注意

您必須將 endpoint 值取代為先前教學課程所建立函式應用程式中的觸發程序 URL。

您可以使用 Azure Resource Manager 範本來部署上述自訂資源提供者:

{
    "$schema": "http://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "resources": [
        {
            "type": "Microsoft.CustomProviders/resourceProviders",
            "name": "myCustomProvider",
            "apiVersion": "2018-09-01-preview",
            "location": "eastus",
            "properties": {
                "actions": [
                    {
                        "name": "myCustomAction",
                        "routingType": "Proxy",
                        "endpoint": "https://<yourapp>.azurewebsites.net/api/<funcname>?code=<functionkey>"
                    }
                ],
                "resourceTypes": [
                    {
                        "name": "myCustomResources",
                        "routingType": "Proxy",
                        "endpoint": "https://<yourapp>.azurewebsites.net/api/<funcname>?code=<functionkey>"
                    }
                ]
            }
        }
    ]
}

使用自訂動作和資源

當您建立自訂資源提供者之後,就可以使用新的 Azure API。 下列各節將說明如何呼叫和使用自訂資源提供者。

自訂動作

Azure CLI

注意

您必須將 {subscriptionId}{resourceGroupName} 預留位置取代為您部署自訂資源提供者所在的訂用帳戶和資源群組。

az resource invoke-action --action myCustomAction \
                          --ids /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.CustomProviders/resourceProviders/myCustomProvider \
                          --request-body
                            '{
                                "hello": "world"
                            }'
參數 必要 描述
action Yes 自訂資源提供者中定義的動作名稱。
ids Yes 自訂資源提供者的資源識別碼。
request-body No 將會傳送至端點的要求本文。

自訂資源

注意

您必須將 {subscriptionId}{resourceGroupName} 預留位置取代為您部署自訂資源提供者所在的訂用帳戶和資源群組。

建立自訂資源

az resource create --is-full-object \
                   --id /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.CustomProviders/resourceProviders/myCustomProvider/myCustomResources/myTestResourceName1 \
                   --properties
                    '{
                        "location": "eastus",
                        "properties": {
                            "hello" : "world"
                        }
                    }'
參數 必要 描述
is-full-object Yes 指出屬性物件是否包含其他選項,例如位置、標籤、SKU 或方案。
id Yes 自訂資源的資源識別碼。 此識別碼是自訂資源提供者資源識別碼的延伸。
properties Yes 將會傳送至端點的要求本文。

刪除自訂資源

az resource delete --id /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.CustomProviders/resourceProviders/myCustomProvider/myCustomResources/myTestResourceName1
參數 必要 描述
id Yes 自訂資源的資源識別碼。 此識別碼是自訂資源提供者資源識別碼的延伸。

擷取自訂資源

az resource show --id /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.CustomProviders/resourceProviders/myCustomProvider/myCustomResources/myTestResourceName1
參數 必要 描述
id Yes 自訂資源的資源識別碼。 此識別碼是自訂資源提供者資源識別碼的延伸。

注意

當您完成自訂資源提供者的部署和使用之後,請記得清除所有建立的資源,包括 Azure 函數應用程式。

下一步

在此文章中,您已瞭解自訂資源提供者。 如需詳細資訊,請參閱