azure - 如何使用 Terraform azurerm 指定 Microsoft Azure API 操作的响应表示的示例

标签 azure terraform azure-api-management azure-rm

我正在使用 Terraform 在 API 管理中指定对 Azure API 的操作。现在操作的功能很简单:无论传入什么,只需模拟相同的响应即可。手动,这很简单。您只需编辑操作的前端,单击响应选项卡,选择响应,添加表示形式,然后在示例字段中指定返回。然后,您只需指定入站策略并选择该特定响应。完成。

事实证明,在 Terraform 中做同样的事情并不那么容易。唯一缺少的部分是定义返回内容的 Sample 字段。我尝试按照此处的文档进行操作:https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/api_management_api_operation .

这是我的代码:

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 2.98.0"
    }
  }

  required_version = ">= 1.1.0"
}

provider "azurerm" {
  features {
    api_management {
      purge_soft_delete_on_destroy = true
    }
  }
}

resource "azurerm_resource_group" "matt_test" {
  name     = "matt_test"
  location = "westus2"
}

resource "azurerm_api_management" "matts-apim" {
  name                = "matts-apim"
  location            = azurerm_resource_group.matt_test.location
  resource_group_name = azurerm_resource_group.matt_test.name
  publisher_name      = "Publisher"
  publisher_email     = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5a3723743f373b33361a3f373b333674393537" rel="noreferrer noopener nofollow">[email protected]</a>"
  sku_name            = "Consumption_0"
}


resource "azurerm_api_management_api" "carto-replacement-api" {
  name                = "carto-replacement-api"
  resource_group_name = azurerm_resource_group.matt_test.name
  api_management_name = azurerm_api_management.matts-apim.name
  revision            = "1"
  display_name        = "CARTO Replacement"
  path                = "carto"
  protocols           = ["https"]
}

locals {
  request_example_value = {
    property = "value"
  }
  response_example_value = {
    response = "response value"
  }
}

resource "azurerm_api_management_api_operation" "bbox" {
  operation_id        = "bbox"
  api_name            = azurerm_api_management_api.carto-replacement-api.name
  api_management_name = azurerm_api_management_api.carto-replacement-api.api_management_name
  resource_group_name = azurerm_api_management_api.carto-replacement-api.resource_group_name
  display_name        = "Bounding Box"
  method              = "POST"
  url_template        = "/bbox"

  request {
    description = ""

    header {
      name     = "Content-Type"
      required = "false"
      type     = "string"
      values   = ["application/json"]
    }

    header {
      name     = "Authorization"
      required = "true"
      type     = "string"
    }

    representation {
      content_type = "application/json"
      example {
        name = "cirrus 10m terrain response"
        value = jsonencode(local.request_example_value)
      }
    }
  }

  response {
    status_code = 200
  }

  response {
    status_code = 201

    representation {
      content_type = "application/json"
      example {
        name  = "Cirrus 10m Terrain Response"
        value = jsonencode(local.response_example_value)
      }
    }
  }
}

resource "azurerm_api_management_api_operation_policy" "bbox_inbound_policy" {
  api_name            = azurerm_api_management_api_operation.bbox.api_name
  api_management_name = azurerm_api_management_api_operation.bbox.api_management_name
  resource_group_name = azurerm_api_management_api_operation.bbox.resource_group_name
  operation_id        = azurerm_api_management_api_operation.bbox.operation_id

  xml_content = <<XML
<policies>
    <inbound>
        <base />
        <mock-response status-code="201" content-type="application/json" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies> 
    XML
}

请注意我尝试在此处提供请求和响应的示例。在这两种情况下,表示形式都会显示在 Azure 门户中,但示例字段仍为空。我不知道如何在 Terraform 中指定它。任何帮助将不胜感激。

作为引用,Azure 门户 UI 中的外观如下: enter image description here

最佳答案

我遇到了同样的问题,并将示例的名称命名为“default”解决了它。

  response {
    status_code = 200
    description = "200"
    representation {
      content_type = "application/json"
      example {
        name = "default"
        value = jsonencode({
          response = "ok"
        })
      }
    }
  }

关于azure - 如何使用 Terraform azurerm 指定 Microsoft Azure API 操作的响应表示的示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71443774/

相关文章:

azure - 创建 Azure Api 管理时 Vnet 未列出

caching - 用于输出缓存的 Azure Redis 缓存和角色内缓存之间的性能差异

azure - 如何使用 terraform 将来自 SQL 托管实例的连接字符串存储在 Azure Key Vault 中

azure - 如何本地化 Azure API 管理开发人员门户使用的字符串?

azure - Azure Functions PowerShell API 的 OpenAPI 规范配置

Azure Api 管理从 swagger 导入 API

javascript - 如何使用 Javascript/JQuery 使用 X509 证书发出 REST 请求?

terraform - 如何将 Terraform 堆栈嵌套到顶层和模块之外

azure - Terraform 云使用 Azure 运行触发器

google-cloud-platform - 无法为我的 Postgres Cloud SQL 实例启用私有(private) IP