Azure APIM 定义的 Terraform 模块

Terrform Module for Azure APIM Definitions

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

输出:

NOTE: This is just an example .You can provide your own definition in the resource "azurerm_api_management_api_schema" "example" value.

至于解决方案,我们创建了 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"
  }
  }

为其创建了定义。