#terraform #azure-bicep
Вопрос:
Как преобразовать переменные terraform в параметры/переменные, совместимые с бицепсами. Я очень новичок в кодировании бицепсов
Ниже приведены мои файлы terraform
# main.tf
locals {
all_endpoints_policy = templatefile(var.policy_templates.composition_path, merge(var.policy_templates.composition_defaults, {
inbound = [
var.apim_backends.fn_fake.set_backend_inbound_policy_element,
templatefile(var.policy_templates.inbound.platform_authorization, {
authorized_user_id_header = var.authorizer_config.header_names.authorized_user_id
authorized_detailed_user_header = var.authorizer_config.header_names.authorized_detailed_user
access_policy_header = var.authorizer_config.header_names.access_policy
authorizer_url = var.authorizer_config.urls.authorizer
validation_url = var.authorizer_config.urls.validator
allowed_roles = ["TEST_USER"]
allowed_impersonation_roles = ["ADMIN"]
})
]
}))
}
resource "azurerm_api_management_api_version_set" "fake_api_version_set_v1" {
name = var.api_version_set_name
display_name = var.api_display_name
resource_group_name = var.api_management_service.region_context.resource_group.name
api_management_name = var.api_management_service.name
versioning_scheme = "Segment"
description = <<EOT
Fake APIs that require authentication.
EOT
}
module "v1" {
source = "./v1"
resource_group_name = var.api_management_service.region_context.resource_group.name
api_header_subscription_key_name = var.api_header_subscription_key_name
api_query_subscription_key_name = var.api_query_subscription_key_name
api_management_name = var.api_management_service.name
api_name = "${var.api_name}-1"
api_display_name = var.api_display_name
api_path = var.api_path
api_protocols = var.api_protocols
version_set_id = azurerm_api_management_api_version_set.fake_api_version_set_v1.id
all_endpoints_policy = local.all_endpoints_policy
}
#########################################################################################
# variables.tf file for above main.tf file
variable "api_management_service" {}
variable "api_header_subscription_key_name" {}
variable "api_query_subscription_key_name" {}
variable "api_name" {
default = "fake-api"
}
variable "api_path" {
default = "fake"
}
variable "api_display_name" {
default = "Fake API"
}
variable "api_protocols" {
default = ["https"]
}
variable "api_version_set_name" {
default = "fake-api-vs-1"
}
variable "policy_templates" {
type = object({
composition_path = string
composition_defaults = any
inbound = object({
set_backend = any
platform_authorization = any
})
})
}
variable "apim_backends" {
type = object({
fn_fake = map(any)
})
}
variable "authorizer_config" {}
#########################################################################################
## v1 module main.tf file
resource "azurerm_api_management_api" "api" {
name = var.api_name
resource_group_name = var.resource_group_name
api_management_name = var.api_management_name
revision = var.api_revision
display_name = var.api_display_name
path = var.api_path
protocols = var.api_protocols
version = var.api_version
version_set_id = replace(var.version_set_id, "api-version-sets", "apiVersionSets")
subscription_required = true
subscription_key_parameter_names {
header = var.api_header_subscription_key_name
query = var.api_query_subscription_key_name
}
}
resource "azurerm_api_management_api_policy" "all_apis" {
api_management_name = var.api_management_name
api_name = azurerm_api_management_api.api.name
resource_group_name = var.resource_group_name
xml_content = replace(var.all_endpoints_policy, " ", "t")
}
#########################################################################################
# v1 module variables.tf file
variable "api_management_name" {}
variable "resource_group_name" {}
variable "api_header_subscription_key_name" {}
variable "api_query_subscription_key_name" {}
variable "version_set_id" {}
variable "api_name" {}
variable "api_path" {}
variable "api_display_name" {}
variable "api_protocols" {}
variable "api_version" {
default = "v1"
}
variable "api_revision" {
default = 1
}
variable "all_endpoints_policy" {}
Все файлы Терраформирования заканчиваются здесь.
all_endpoints_policy = templatefile(var.policy_templates.composition_path, merge(var.policy_templates.composition_defaults, {
inbound = [
var.apim_backends.fn_fake.set_backend_inbound_policy_element,
templatefile(var.policy_templates.inbound.platform_authorization, {
authorized_user_id_header = var.authorizer_config.header_names.authorized_user_id
authorized_detailed_user_header = var.authorizer_config.header_names.authorized_detailed_user
access_policy_header = var.authorizer_config.header_names.access_policy
authorizer_url = var.authorizer_config.urls.authorizer
validation_url = var.authorizer_config.urls.validator
allowed_roles = ["TEST_USER"]
allowed_impersonation_roles = ["ADMIN"]
})
]
}))
Я хочу преобразовать приведенную выше переменную terraform «all_endpoints_policy» в main.tf файл в эквивалентный бицепсу параметр/переменную, чтобы иметь возможность передать его в атрибут/свойство «значение» ресурса ниже бицепса
# bicep resource
resource symbolicname 'Microsoft.ApiManagement/service/apis/policies@2021-01-01-preview' = {
name: 'policy'
properties: {
format: 'xml'
value: 'string'
}
}
# terraform equivalent resource for above bicep resource
resource "azurerm_api_management_api_policy" "example" {
api_name = data.azurerm_api_management_api.example.name
api_management_name = data.azurerm_api_management_api.example.api_management_name
resource_group_name = data.azurerm_api_management_api.example.resource_group_name
xml_content = <<XML
<policies>
<inbound>
<find-and-replace from="xyz" to="abc" />
</inbound>
</policies>
XML
}
And I also want to know how to convert/represent the below terraform variables of variables.tf file for main.tf file into equivalent bicep params/variables as these variables are used in «all_endpoints_policy» variable of main.tf file.
variable "policy_templates" {
type = object({
composition_path = string
composition_defaults = any
inbound = object({
set_backend = any
platform_authorization = any
})
})
}
variable "apim_backends" {
type = object({
fn_fake = map(any)
})
}