0

如何将地形变量转换为与二头肌兼容的参数/变量。我对二头肌编码很陌生

以下是我的地形文件

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

所有 Terraform 文件到此结束。

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

我想将 main.tf 文件中的上述“all_endpoints_policy”terraform 变量转换为二头肌等效参数/变量,以便能够将其传递给二头肌资源下方的“值”属性/属性

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

而且我还想知道如何将以下用于 main.tf 文件的 variables.tf 文件的 terraform 变量转换/表示为等效的二头肌参数/变量,因为这些变量在 main.tf 文件的“all_endpoints_policy”变量中使用。

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

4

0 回答 0