如何从OpenAPI定义提取操作写入Azure APIM的Terraform Schema配置
Azure APIM Schema 配置及OpenAPI内容提取指南
1. 基础概念澄清
你当前使用的azurerm_api_management_api_schema资源用于存储API用到的全局数据模型定义,不需要将GET/POST等操作定义填入该资源的配置中。操作定义属于API本身的配置范畴,可通过直接导入OpenAPI文件或单独创建操作资源实现。
2. 从OpenAPI中提取Schema内容步骤
- 下载目标OpenAPI JSON文件,定位到文件根节点下的
components->schemas字段,该字段下的所有内容就是你需要填入Schema资源的模型定义 - 替换你现有Terraform代码中
value字段内schemas节点下的占位内容即可
3. 完整配置示例
示例1:单独创建APIM Schema资源
resource "azurerm_api_management_api_schema" "conference_schema" { api_name = azurerm_api_management_api.sample-api.name api_management_name = azurerm_api_management_api.sample-api.api_management_name resource_group_name = azurerm_api_management_api.sample-api.resource_group_name schema_id = "conference-schema" content_type = "application/vnd.oai.openapi.components+json" value = <<JSON { "properties": { "contentType": "application/vnd.oai.openapi.components+json", "document": { "components": { "schemas": { // 此处粘贴从OpenAPI文件中提取的所有schemas定义内容 } } } } } JSON }
示例2:直接导入OpenAPI自动创建所有操作与关联Schema
如果你需要批量创建OpenAPI中定义的所有GET/POST操作,无需单独配置Schema和操作资源,直接在API资源中添加导入块即可:
resource "azurerm_api_management_api" "conference_api" { name = "conference-api" api_management_name = azurerm_api_management.example.name resource_group_name = azurerm_resource_group.example.name revision = "1" display_name = "Conference API" path = "conference" protocols = ["https"] import { content_format = "openapi+json" // 此处填入本地OpenAPI文件路径,也可直接传入文件内容字符串 content_value = file("${path.module}/conference-api.json") } }
4. 可选:单独创建API操作
如果你需要手动创建单个操作,可使用azurerm_api_management_api_operation资源,请求/响应模型直接关联已创建的Schema即可。
内容的提问来源于stack exchange,提问作者Chanafot
相关产品推荐
相关产品推荐

