我正在使用 Terraform 部署 Azure Api 管理。到目前为止,我能够创建服务、API、策略和操作,但没有一个模块可以在其中找到操作的 json 格式的定义以及如何将操作与定义关联起来。
这是我找到的所有组件:link
有没有办法以某种方式将它们包含在 terraform 中?
data "azurerm_resource_group" "rg" {
name = "${var.resource_group_name}"
}
data "azurerm_api_management" "apim_service" {
name = "${var.apim_service}"
resource_group_name = "${var.resource_group_name}"
}
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "=2.66.0"
}
}
}
# Configure the Microsoft Azure Provider
provider "azurerm" {
features {}
}
resource "azurerm_api_management_api" "sample-api" {
name = "Test-2"
resource_group_name = data.azurerm_resource_group.rg.name
api_management_name = data.azurerm_api_management.apim_service.name
revision = "1"
display_name = "API-1"
path = "API"
protocols = ["https", "http"]
description = "example"
}
resource "azurerm_api_management_api_operation" "get-info" {
operation_id = "info"
api_name = azurerm_api_management_api.sample-api.name
api_management_name = data.azurerm_api_management.apim_service.name
resource_group_name = data.azurerm_resource_group.rg.name
display_name = "Get info Testing"
method = "GET"
url_template = "/info"
description = "foo"
response {
status_code = 200
}
response {
status_code = 400
}
response {
status_code = 401
}
response {
status_code = 403
}
response {
status_code = 404
}
}
resource "azurerm_api_management_api_policy" "sample-api" {
api_name = azurerm_api_management_api.sample-api.name
api_management_name = data.azurerm_api_management.apim_service.name
resource_group_name = data.azurerm_resource_group.rg.name
#operation_id = azurerm_api_management_api_operation.sample-api.operation_id
xml_content = <<-XML
<policies>
<inbound>
<base />
<rewrite-uri template="/api/info" copy-params="true" />
<set-backend-service base-url="https://foo-dev.azurewebsites.net" />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
XML
}
我通过做一些更改,使用您的代码在我的环境中对此进行了测试。 如果您想将定义添加到操作中,则可以使用如下所示的方法来完成:
代码:
data "azurerm_resource_group" "rg" {
name = "Your resource group name"
}
data "azurerm_api_management" "apim_service" {
name = "your apim service name"
resource_group_name = data.azurerm_resource_group.rg.name
}
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "=2.66.0"
}
}
}
# Configure the Microsoft Azure Provider
provider "azurerm" {
features {}
}
resource "azurerm_api_management_api" "sample-api" {
name = "Test-2"
resource_group_name = data.azurerm_resource_group.rg.name
api_management_name = data.azurerm_api_management.apim_service.name
revision = "1"
display_name = "API-1"
path = "API"
protocols = ["https", "http"]
description = "example"
}
resource "azurerm_api_management_api_schema" "example" {
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 = "example-schema"
content_type = "application/vnd.oai.openapi.components+json"
value = <<JSON
{
"properties": {
"contentType": "application/vnd.oai.openapi.components+json",
"document": {
"components": {
"schemas": {
"Definition1": {
"type": "object",
"properties": {
"String1": {
"type": "string"
}
}
},
"Definition2": {
"type": "object",
"properties": {
"String2": {
"type": "integer"
}
}
}
}
}
}
}
}
JSON
}
resource "azurerm_api_management_api_operation" "get-info" {
operation_id = "info"
api_name = azurerm_api_management_api.sample-api.name
api_management_name = data.azurerm_api_management.apim_service.name
resource_group_name = data.azurerm_resource_group.rg.name
display_name = "Get info Testing"
method = "POST"
url_template = "/info"
description = "foo"
request {
representation {
schema_id = azurerm_api_management_api_schema.example.schema_id
content_type = azurerm_api_management_api_schema.example.content_type
sample = azurerm_api_management_api_schema.example.value
type_name = "test"
}
}
response {
status_code = 200
}
response {
status_code = 400
}
response {
status_code = 401
}
response {
status_code = 403
}
response {
status_code = 404
}
}
输出:
注意:这只是一个示例。您可以在中提供您自己的定义
值。resource "azurerm_api_management_api_schema" "example"
至于解决方案,我们创建了 api schema,然后通过添加以下内容在操作中使用它:
request {
representation {
schema_id = azurerm_api_management_api_schema.example.schema_id
content_type = azurerm_api_management_api_schema.example.content_type
sample = azurerm_api_management_api_schema.example.value
type_name = "test"
}
}
已为其创建定义。