1
votes

Expected Behaviour

I would like to make a boolean variable 'var.enable_retention_policy' true based on the output of mapped value 'var.ds_allmetrics_retention_days'

Actual Behaviour

throws error (see Error output)

Error Output

Error: Inconsistent conditional result types

  on ..\db\main.tf line 37, in module "diagnostic_mssql_db":
  37:     "WorkloadManagement"     = var.workload_management > 0 ? var.workload_management : var.enable_retention_policy

The true and false result expressions must have consistent types. The given
expressions are number and bool, respectively.

Terraform (and AzureRM Provider) Version

Affected Resource(s)

  • azurerm_v2.41.0
  • terraform v0.13.0

Terraform Configuration Files

Main.tf

data "azurerm_storage_account" "storage_account" {
  name                = var.storage_account
  resource_group_name = var.sa_resource_group
}

resource "azurerm_monitor_diagnostic_setting" "diagsetting" {
  name               = var.target_resource_name
  target_resource_id = var.target_resource_id
  storage_account_id = data.azurerm_storage_account.storage_account.id

  dynamic "log" {
    for_each = keys(var.ds_log_api_endpoints)
    content {
      category = log.value
      enabled  = true

      retention_policy {
        enabled = var.enable_retention_policy
        days    = lookup(var.ds_log_api_endpoints, log.value, 0)
      }
    }
  }

  dynamic "metric" {
    for_each = keys(var.ds_allmetrics_retention_days)
    content {
      category = metric.value

      retention_policy {
        enabled = var.enable_retention_policy
        days    = lookup(var.ds_allmetrics_retention_days, metric.value, 0)
      }
    }
  }
}

Variables.tf


# boolean variable
variable "enable_retention_policy" {
  type        = bool
  description = "toggle on/off the retention policy for the metric"
  default     = false
}

# mapped values
variable "ds_allmetrics_retention_days" {
  type        = map
  description = "Azure monitor diagnostic setting category for retention in days of target resource."
  default     = {}
}

Module call

module "diagnostic_mssql_db" {
  source                  = "github.com/faraday23/terraform-azurerm-monitor-diagnostic-setting.git"
  storage_account         = var.storage_account
  sa_resource_group       = var.sa_resource_group
  target_resource_id      = azurerm_mssql_database.db.id
  target_resource_name    = azurerm_mssql_database.db.name
  enable_retention_policy = var.enable_retention_policy
  ds_log_api_endpoints = { "AutomaticTuning" = var.automatic_tuning > 0 ? var.automatic_tuning : var.enable_retention_policy,
    "Blocks"                      = var.blocks > 0 ? var.blocks : var.enable_retention_policy,
    "DatabaseWaitStatistics"      = var.database_wait_statistics > 0 ? var.database_wait_statistics : var.enable_retention_policy,
    "Deadlocks"                   = var.deadlocks > 0 ? var.deadlocks : var.enable_retention_policy,
    "Errors"                      = var.error_log > 0 ? var.error_log : var.enable_retention_policy,
    "Timeouts"                    = var.timeouts > 0 ? var.timeouts : var.enable_retention_policy,
    "QueryStoreRuntimeStatistics" = var.query_store_runtime_statistics > 0 ? var.query_store_runtime_statistics : var.enable_retention_policy,
    "QueryStoreWaitStatistics"    = var.query_store_wait_statistics > 0 ? var.query_store_wait_statistics : var.enable_retention_policy,
    "SQLInsights"                 = var.sql_insights > 0 ? var.sql_insights : var.enable_retention_policy
  }

  ds_allmetrics_retention_days = { "Basic" = var.basic > 0 ? var.basic : var.enable_retention_policy,
    "InstanceAndAppAdvanced" = var.instance_and_app_advanced > 0 ? var.instance_and_app_advanced : var.enable_retention_policy,
    "WorkloadManagement"     = var.workload_management > 0 ? var.workload_management : var.enable_retention_policy
  }
}
1

1 Answers

1
votes

The error message indicates that the issue is with WorkloadManagement. In your case you have mixture of bool and int which is incorrect. Conditional Expressions must be of same type:

The two result values may be of any type, but they must both be of the same type so that Terraform can determine what type the whole conditional expression will return without knowing the condition value.

Thus, you can use tonumber to change bool to int:

Update based on Martin comment

The original answer used tonumber(var.enable_retention_policy) which was incorrect, as you can't convert bool to number. To correct answer is then:

"WorkloadManagement" = (var.workload_management > 0 
                       ? var.workload_management 
                       : (var.enable_retention_policy ? 1 : 0))