Share via


使用 Azure CLI 來管理 API 清查

本文說明如何使用 az apic api Azure CLI 中的命令,在 API 中心清查中新增和設定 API。 使用 Azure CLI 中的命令來編寫作業腳本,以管理 API 清查和 API 中心的其他層面。

必要條件

  • Azure 訂用帳戶中的 API 中心。 如果您尚未建立 API 中心,請參閱 快速入門:建立 API 中心

  • 對於 Azure CLI:

    注意

    az apic 命令需要 apic-extension Azure CLI 擴充功能。 如果您尚未使用 az apic 命令,當您執行第一個 az apic 命令時,會動態安裝擴充功能。 深入瞭解 Azure CLI 擴充功能

註冊 API、API 版本和定義

下列步驟示範如何建立 API,並建立單一 API 版本和 API 定義的關聯。 如需 Azure API 中心內數據模型的背景,請參閱 重要概念

建立 API

使用 az apic api create 命令在 API 中心建立 API。

下列範例會在 myResourceGroup 資源群組和 myAPICenter API 中心中建立名為 Petstore APIAPI。 API 是 REST API。

az apic api create  --resource-group myResourceGroup \
    --service myAPICenter --api-id petstore-api \
    --title "Petstore API" --type "rest"

注意

建立 API 之後,您可以使用 az apic api update 命令來更新 API 的屬性

建立 API 版本

使用 az apic api version create 命令來建立 API 的版本。

下列範例會針對您在上一節中建立的petstore-api API,建立名為 v1-0-0API 版本。 版本會設定為 測試 生命周期階段。

az apic api version create --resource-group myResourceGroup \
    --service myAPICenter --api-id petstore-api \
    --version-id v1-0-0 --title "v1-0-0" --lifecycle-stage "testing"

建立 API 定義並新增規格檔案

使用 az apic api definition 命令,為 API 版本新增定義和隨附的規格檔案。

建立定義

下列範例會使用 az apic api definition create 命令,針對您在上一節中建立的petstore-api API 版本建立名為 openapi 的定義。

az apic api definition create --resource-group myResourceGroup \
    --service myAPICenter --api-id petstore-api \
    --version-id v1-0-0 --definition-id openapi --title "OpenAPI"

匯入規格檔案

使用 az apic api definition import-specification 命令將規格檔案匯入定義。

下列範例會將 OpenAPI 規格檔案從可公開存取的 URL 匯入至您在上一個步驟中建立的 openapi 定義。 規格 name 資源的 和 version 屬性會以 JSON 的形式傳遞。

az apic api definition import-specification \
    --resource-group myResourceGroup --service myAPICenter \
    --api-id petstore-api --version-id v1-0-0 \
    --definition-id openapi --format "link" \
    --value 'https://petstore3.swagger.io/api/v3/openapi.json' \
    --specification '{"name":"openapi","version":"3.0.2"}'

提示

您可以將 參數設定 --formatinline ,並使用 --value 參數傳遞檔案內容,以內嵌匯入規格檔案。

匯出規格檔案

若要將 API 規格從 API 中心導出至本機檔案,請使用 az apic api definition export-specification 命令。

下列範例會將規格檔案從您在上一節中建立的 openapi 定義匯出至名為 specificationFile.json 的本機檔案。

az apic api definition export-specification \
    --resource-group myResourceGroup --service myAPICenter \
    --api-id petstore-api --version-id v1-0-0 \
    --definition-id openapi --file-name "/Path/to/specificationFile.json"

從規格檔案註冊 API - 單一步驟

您可以使用 az apic api register 命令,在單一步驟中從本機規格檔案註冊 API。 使用此選項時,會自動為 API 建立預設 API 版本和定義。

下列範例會從名為 specificationFile.json 的本機 OpenAPI 定義檔,在 myAPICenter API 中心註冊 API。

az apic api register --resource-group myResourceGroup \
    --service myAPICenter --api-location "/Path/to/specificationFile.json"
  • 命令會從定義檔中的值設定 API 屬性,例如名稱和類型。
  • 根據預設,命令會將 API 的 生命週期階段 設定為 設計
  • 它會建立名為 1-0-0 的預設 API 版本,並根據規格格式建立名為的預設定義(例如 openapi)。

註冊 API 之後,您可以使用 az apic api update、az apic api version updateaz apic api definition update 命令來更新 API 的屬性。

刪除 API 資源

使用 az apic api delete 命令來刪除 API 及其所有版本和定義資源。 例如:

az apic api delete \
    --resource-group myResoureGroup --service myAPICenter \
    --api-id petstore-api

若要刪除個別 API 版本和定義,請分別使用 az apic api version deleteaz apic api definition delete