【问题标题】:Terrform Module for Azure APIM DefinitionsAzure APIM 定义的 Terraform 模块
【发布时间】:2021-09-23 21:21:31
【问题描述】:

我正在使用 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

}

【问题讨论】:

  • 能否提供您使用过的terraform代码,以便我们参考并提供解决方案。
  • 刚刚更新了代码,但不知道是否需要什么。需要知道什么是模块或如何使用 APIM 添加“定义”
  • 我在这篇文章中看到了定义结构是如何制作的:stackoverflow.com/questions/68961623/… 但找不到如何使用 terraform 添加它的地方

标签: terraform azure-api-management terraform-provider-azure


【解决方案1】:

我使用您的代码在我的环境中进行了一些更改,对此进行了测试。 如果您想将定义添加到操作中,则可以使用以下内容来完成:

代码:

    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 模式,然后通过添加以下内容在操作中使用它:

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"
  }
  }

已为其创建定义。

【讨论】:

  • 成功了!谢谢
  • 你好@kavyasaraboju-MT 有没有办法在相同的 API 上创建不同的操作?使用 Schemas 的方法?谁来做?
猜你喜欢
  • 2021-04-28
  • 2022-08-05
  • 1970-01-01
  • 2020-03-17
  • 2021-11-17
  • 1970-01-01
  • 1970-01-01
  • 2022-01-13
  • 2019-01-12
相关资源
最近更新 更多