SuperAPI AI 工具集

azure-mgmt-apimanagement-py

部署与运维

查看项目

更新于 10-16 02:08

技能介绍

用途

azure-mgmt-apimanagement 是 Azure API Management 的 Python SDK,用于以编程方式管理 Azure API Management(APIM)服务。它覆盖 APIM 实例、API、产品、订阅、策略、用户、命名值、后端、证书和自托管网关等资源的创建与维护。

主要能力

  • 管理 APIM 实例(api_management_service)
  • 管理 API 及其操作(api、api_operation)
  • 从 OpenAPI 定义或 URL 导入 API
  • 管理产品及产品与 API 的关联(product、product_api)
  • 管理订阅(subscription)
  • 设置 API 级策略(api_policy)
  • 管理用户(user)
  • 管理命名值与密钥(named_value)
  • 管理后端服务(backend)
  • 管理证书(certificate)
  • 管理自托管网关(gateway)

安装

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"]
)

使用方式示例

创建 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="admin@example.com",
        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"Subscription key: {subscription.primary_key}")

设置 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="user@example.com",
        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 进行版本管理

适用场景

该技能适用于执行概述中描述的工作流或操作,即通过 Python 自动化管理 Azure API Management 的各类资源。

限制

  • 仅在任务明确匹配上述范围时使用此技能。
  • 不要将输出视为环境特定验证、测试或专家审查的替代品。
  • 如果缺少必需的输入、权限、安全边界或成功标准,请停止并请求澄清。