azure-mgmt-apimanagement-py について
管理SDKを使ってAzure API Managementを運用するPythonスキルです。APIのインポート、製品、サブスクリプション、ポリシー、バックエンド、サービス設定を扱います。
主な機能
- 01APIMサービスの作成と管理
- 02仕様またはURLからAPIをインポート
- 03API一覧と製品の設定
- 04サブスクリプションとユーザーの管理
- 05APIポリシーと名前付き値の設定
- 06バックエンドサービスの設定
活用シーン
- 01APIをAPIMにインポート
- 02APIをアクセス製品にまとめる
- 03ポリシーとバックエンド設定の更新
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ sickn33/ agentic-awesome-skills --skill azure-mgmt-apimanagement-pyインストール後、Claude Code で/azure-mgmt-apimanagement-pyを入力して読み込まれたか確認します。
詳細
Python 用 Azure API Management SDK
Azure API Management サービス、API、製品、ポリシーを管理します。
インストール
pip install azure-mgmt-apimanagement
pip install azure-identity
環境変数
AZURE_SUBSCRIPTION_ID=your-subscription-id
認証
from azure.identity import DefaultAzureCredential
from azure.mgmt.apimanagement import ApiManagementClient
import os
client = ApiManagementClient(
credential=DefaultAzureCredential(),
subscription_id=os.environ["AZURE_SUBSCRIPTION_ID"]
)
書き込みとポリシーの境界
有料のインフラストラクチャを作成し、アクセス製品を公開し、API ポリシーを置き換える例です。実行前に、サブスクリプション、リソース、API、および許可されている操作を確認してください。既存のポリシー XML と継承されたベースポリシーを保持し、正確な差分を確認してから、認証とルーティングを検証してください。サンプルの完全なポリシー置換により、既存の保護機能が削除される可能性があります。プレースホルダーのシークレット値を使用したり、サブスクリプションキーを公開したりしないでください。
APIM サービスの作成
from azure.mgmt.apimanagement.models import (
ApiManagementServiceResource,
ApiManagementServiceSkuProperties,
SkuType
)
service = client.api_management_service.begin_create_or_update(
resource_group_name="my-resource-group",
service_name="my-apim",
parameters=ApiManagementServiceResource(
location="eastus",
publisher_email="[email protected]",
publisher_name="My Organization",
sku=ApiManagementServiceSkuProperties(
name=SkuType.DEVELOPER,
capacity=1
)
)
).result()
print(f"Created APIM: {service.name}")
OpenAPI から API をインポート
from azure.mgmt.apimanagement.models import (
ApiCreateOrUpdateParameter,
ContentFormat,
Protocol
)
api = client.api.begin_create_or_update(
resource_group_name="my-resource-group",
service_name="my-apim",
api_id="my-api",
parameters=ApiCreateOrUpdateParameter(
display_name="My API",
path="myapi",
protocols=[Protocol.HTTPS],
format=ContentFormat.OPENAPI_JSON,
value='{"openapi": "3.0.0", "info": {"title": "My API", "version": "1.0"}, "paths": {"/health": {"get": {"responses": {"200": {"description": "OK"}}}}}}'
)
).result()
print(f"Imported API: {api.display_name}")
URL から API をインポート
api = client.api.begin_create_or_update(
resource_group_name="my-resource-group",
service_name="my-apim",
api_id="petstore",
parameters=ApiCreateOrUpdateParameter(
display_name="Petstore API",
path="petstore",
protocols=[Protocol.HTTPS],
format=ContentFormat.OPENAPI_LINK,
value="https://petstore.swagger.io/v2/swagger.json"
)
).result()
API の一覧表示
apis = client.api.list_by_service(
resource_group_name="my-resource-group",
service_name="my-apim"
)
for api in apis:
print(f"{api.name}: {api.display_name} - {api.path}")
製品の作成
from azure.mgmt.apimanagement.models import ProductContract
product = client.product.create_or_update(
resource_group_name="my-resource-group",
service_name="my-apim",
product_id="premium",
parameters=ProductContract(
display_name="Premium",
description="Premium tier with unlimited access",
subscription_required=True,
approval_required=False,
state="published"
)
)
print(f"Created product: {product.display_name}")
製品への API の追加
client.product_api.create_or_update(
resource_group_name="my-resource-group",
service_name="my-apim",
product_id="premium",
api_id="my-api"
)
サブスクリプションの作成
from azure.mgmt.apimanagement.models import SubscriptionCreateParameters
subscription = client.subscription.create_or_update(
resource_group_name="my-resource-group",
service_name="my-apim",
sid="my-subscription",
parameters=SubscriptionCreateParameters(
display_name="My Subscription",
scope=f"/products/premium",
state="active"
)
)
print(f"Created subscription: {subscription.name}") # Never log subscription keys.
API ポリシーの設定
from azure.mgmt.apimanagement.models import PolicyContract
policy_xml = """
<policies>
<inbound>
<rate-limit calls="100" renewal-period="60" />
<set-header name="X-Custom-Header" exists-action="override">
<value>CustomValue</value>
</set-header>
</inbound>
<backend>
<forward-request />
</backend>
<outbound />
<on-error />
</policies>
"""
client.api_policy.create_or_update(
resource_group_name="my-resource-group",
service_name="my-apim",
api_id="my-api",
policy_id="policy",
parameters=PolicyContract(
value=policy_xml,
format="xml"
)
)
名前付きの値(シークレット)の作成
from azure.mgmt.apimanagement.models import NamedValueCreateContract
named_value = client.named_value.begin_create_or_update(
resource_group_name="my-resource-group",
service_name="my-apim",
named_value_id="backend-api-key",
parameters=NamedValueCreateContract(
display_name="Backend API Key",
value="secret-key-value",
secret=True
)
).result()
バックエンドの作成
from azure.mgmt.apimanagement.models import BackendContract
backend = client.backend.create_or_update(
resource_group_name="my-resource-group",
service_name="my-apim",
backend_id="my-backend",
parameters=BackendContract(
url="https://api.backend.example.com",
protocol="http",
description="My backend service"
)
)
ユーザーの作成
from azure.mgmt.apimanagement.models import UserCreateParameters
user = client.user.create_or_update(
resource_group_name="my-resource-group",
service_name="my-apim",
user_id="newuser",
parameters=UserCreateParameters(
email="[email protected]",
first_name="John",
last_name="Doe"
)
)
操作グループ
| グループ | 目的 |
|---|---|
api_management_service | APIM インスタンスの管理 |
api | API の操作 |
api_operation | API 操作の詳細 |
api_policy | API レベルのポリシー |
product | 製品の管理 |
product_api | 製品と API の関連付け |
subscription | サブスクリプションの管理 |
user | ユーザーの管理 |
named_value | 名前付きの値/シークレット |
backend | バックエンドサービス |
certificate | 証明書 |
gateway | セルフホスト型ゲートウェイ |
ベストプラクティス
- シークレットと構成には名前付きの値を使用します
- 適切なスコープ(グローバル、製品、API、操作)でポリシーを適用します
- API をまとめてアクセスを管理するには製品を使用します
- 監視のためにApplication Insights を有効化します
- バックエンドサービスを抽象化するにはバックエンドを使用します
- APIM のバージョン管理機能を使用してAPI をバージョン管理します
使用する場面
このスキルは、概要で説明されているワークフローまたはアクションを実行する場合に適用できます。
制限事項
- このスキルは、タスクが上記の説明された範囲に明確に該当する場合にのみ使用してください。
- 出力を、環境固有の検証、テスト、または専門家によるレビューの代わりとして扱わないでください。
- 必要な入力、権限、安全上の境界、または成功基準が不足している場合は、停止して確認を求めてください。
コメント
Markdown 対応