|
| 1 | +#------------------------------------------------------------ |
| 2 | +# Local configuration - Default (required). |
| 3 | +#------------------------------------------------------------ |
| 4 | +locals { |
| 5 | + resource_group_name = element(coalescelist(data.azurerm_resource_group.rgrp.*.name, azurerm_resource_group.rg.*.name, [""]), 0) |
| 6 | + location = element(coalescelist(data.azurerm_resource_group.rgrp.*.location, azurerm_resource_group.rg.*.location, [""]), 0) |
| 7 | + # if_threat_detection_policy_enabled = var.enable_threat_detection_policy ? [{}] : [] |
| 8 | +} |
| 9 | + |
| 10 | +#--------------------------------------------------------- |
| 11 | +# Resource Group Creation or selection - Default is "false" |
| 12 | +#---------------------------------------------------------- |
| 13 | +data "azurerm_resource_group" "rgrp" { |
| 14 | + count = var.create_resource_group == false ? 1 : 0 |
| 15 | + name = var.resource_group_name |
| 16 | +} |
| 17 | + |
| 18 | +resource "azurerm_resource_group" "rg" { |
| 19 | + count = var.create_resource_group ? 1 : 0 |
| 20 | + name = var.resource_group_name |
| 21 | + location = var.location |
| 22 | + tags = merge({ "Name" = format("%s", var.resource_group_name) }, var.tags, ) |
| 23 | +} |
| 24 | + |
| 25 | +data "azurerm_client_config" "current" {} |
| 26 | + |
| 27 | +data "azurerm_log_analytics_workspace" "logws" { |
| 28 | + count = var.log_analytics_workspace_name != null ? 1 : 0 |
| 29 | + name = var.log_analytics_workspace_name |
| 30 | + resource_group_name = local.resource_group_name |
| 31 | +} |
| 32 | + |
| 33 | +#--------------------------------------------------------- |
| 34 | +# Storage Account to keep Audit logs - Default is "false" |
| 35 | +#---------------------------------------------------------- |
| 36 | +resource "random_string" "str" { |
| 37 | + count = var.enable_threat_detection_policy ? 1 : 0 |
| 38 | + length = 6 |
| 39 | + special = false |
| 40 | + upper = false |
| 41 | + keepers = { |
| 42 | + name = var.storage_account_name |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +resource "azurerm_storage_account" "storeacc" { |
| 47 | + count = var.enable_threat_detection_policy ? 1 : 0 |
| 48 | + name = var.storage_account_name == null ? "stsqlauditlogs${element(concat(random_string.str.*.result, [""]), 0)}" : substr(var.storage_account_name, 0, 24) |
| 49 | + resource_group_name = local.resource_group_name |
| 50 | + location = local.location |
| 51 | + account_kind = "StorageV2" |
| 52 | + account_tier = "Standard" |
| 53 | + account_replication_type = "GRS" |
| 54 | + enable_https_traffic_only = true |
| 55 | + min_tls_version = "TLS1_2" |
| 56 | + tags = merge({ "Name" = format("%s", "stsqlauditlogs") }, var.tags, ) |
| 57 | +} |
| 58 | + |
| 59 | +resource "random_password" "main" { |
| 60 | + count = var.admin_password == null ? 1 : 0 |
| 61 | + length = var.random_password_length |
| 62 | + min_upper = 4 |
| 63 | + min_lower = 2 |
| 64 | + min_numeric = 4 |
| 65 | + special = false |
| 66 | + |
| 67 | + keepers = { |
| 68 | + administrator_login_password = var.postgresql_server_name |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +#---------------------------------------------------------------- |
| 73 | +# Adding PostgreSQL Server creation and settings - Default is "True" |
| 74 | +#----------------------------------------------------------------- |
| 75 | +resource "azurerm_postgresql_server" "main" { |
| 76 | + name = format("%s", var.postgresql_server_name) |
| 77 | + resource_group_name = local.resource_group_name |
| 78 | + location = local.location |
| 79 | + administrator_login = var.admin_username == null ? "sqladmin" : var.admin_username |
| 80 | + administrator_login_password = var.admin_password == null ? random_password.main.0.result : var.admin_password |
| 81 | + sku_name = var.postgresql_server_settings.sku_name |
| 82 | + version = var.postgresql_server_settings.version |
| 83 | + storage_mb = var.postgresql_server_settings.storage_mb |
| 84 | + auto_grow_enabled = var.postgresql_server_settings.auto_grow_enabled |
| 85 | + backup_retention_days = var.postgresql_server_settings.backup_retention_days |
| 86 | + geo_redundant_backup_enabled = var.postgresql_server_settings.geo_redundant_backup_enabled |
| 87 | + infrastructure_encryption_enabled = var.postgresql_server_settings.infrastructure_encryption_enabled |
| 88 | + public_network_access_enabled = var.postgresql_server_settings.public_network_access_enabled |
| 89 | + ssl_enforcement_enabled = var.postgresql_server_settings.ssl_enforcement_enabled |
| 90 | + ssl_minimal_tls_version_enforced = var.postgresql_server_settings.ssl_minimal_tls_version_enforced |
| 91 | + create_mode = var.create_mode |
| 92 | + creation_source_server_id = var.create_mode != "Default" ? var.creation_source_server_id : null |
| 93 | + restore_point_in_time = var.create_mode == "PointInTimeRestore" ? var.restore_point_in_time : null |
| 94 | + tags = merge({ "Name" = format("%s", var.postgresql_server_name) }, var.tags, ) |
| 95 | + |
| 96 | + dynamic "identity" { |
| 97 | + for_each = var.identity == true ? [1] : [0] |
| 98 | + content { |
| 99 | + type = "SystemAssigned" |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + dynamic "threat_detection_policy" { |
| 104 | + for_each = var.enable_threat_detection_policy == true ? [1] : [] |
| 105 | + content { |
| 106 | + enabled = var.enable_threat_detection_policy |
| 107 | + disabled_alerts = var.disabled_alerts |
| 108 | + email_account_admins = var.email_addresses_for_alerts != null ? true : false |
| 109 | + email_addresses = var.email_addresses_for_alerts |
| 110 | + retention_days = var.log_retention_days |
| 111 | + storage_account_access_key = azurerm_storage_account.storeacc.0.primary_access_key |
| 112 | + storage_endpoint = azurerm_storage_account.storeacc.0.primary_blob_endpoint |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +#------------------------------------------------------------ |
| 118 | +# Adding PostgreSQL Server Database - Default is "true" |
| 119 | +#------------------------------------------------------------ |
| 120 | +resource "azurerm_postgresql_database" "main" { |
| 121 | + name = var.postgresql_server_settings.database_name |
| 122 | + resource_group_name = local.resource_group_name |
| 123 | + server_name = azurerm_postgresql_server.main.name |
| 124 | + charset = var.postgresql_server_settings.charset |
| 125 | + collation = var.postgresql_server_settings.collation |
| 126 | +} |
| 127 | + |
0 commit comments