I´m using Terraform to deploy Azure Api management. So far Was able to create the service, api, policies and operations but there is not a module in where I can find definitions in json format for the operations and how I can associate an operation with a definition.
This are all the components that I found: link
Is there a way of include them with terraform somehow?
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
}
I tested this on my environment using your code by doing some changes. If you want to add the definition to the operation, then you can do it by using something like below:
CODE:
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "=2.66.0"
}
}
}
# Configure the Microsoft Azure Provider
provider "azurerm" {
features {}
}
# Data sources
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
}
# Resources
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
}
}
Output:
NOTE: This is just an example .You can provide your own definition in the
resource "azurerm_api_management_api_schema" "example"
value.
As for the solution we have created api schema and then used it in operation by adding the below:
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"
}
}
for which the definition has been created.