Skip to content

Commit 71a1363

Browse files
feat(accounting-reconciliation): CHK-4676 add cosmos db on qi and accounting reconciliation collections (#3544)
* feat(accounting-reconciliation): add cosmos db on qi domain and accounting reconciliation collections * fix: minor fix for qi cosmos * feat(cosmos): set accounting reconciliation collections default TTL to 180 days * Update src/domains/qi-common/03_cosmosdb.tf * chore: minore renaming refactorgin db * chore: update qi cosmos conf --------- Co-authored-by: Simone infante <52280205+infantesimone@users.noreply.github.com>
1 parent 330c10b commit 71a1363

File tree

8 files changed

+295
-1
lines changed

8 files changed

+295
-1
lines changed

src/domains/qi-common/01_network.tf

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ data "azurerm_resource_group" "rg_vnet_italy" {
2525
name = local.vnet_italy_resource_group_name
2626
}
2727

28+
data "azurerm_subnet" "aks_subnet" {
29+
name = local.aks_subnet_name
30+
virtual_network_name = local.vnet_name
31+
resource_group_name = local.vnet_resource_group_name
32+
}
33+
34+
data "azurerm_private_dns_zone" "cosmos" {
35+
name = local.cosmos_dns_zone_name
36+
resource_group_name = local.cosmos_dns_zone_resource_group_name
37+
}
38+
2839
#
2940
# Eventhub
3041
#
@@ -43,4 +54,21 @@ resource "azurerm_subnet" "eventhub_qi_snet" {
4354
resource_group_name = data.azurerm_resource_group.rg_vnet_italy.name
4455
virtual_network_name = data.azurerm_virtual_network.vnet_italy.name
4556
address_prefixes = var.cidr_subnet_qi_evh
46-
}
57+
}
58+
59+
module "cosmosdb_qi_snet" {
60+
source = "./.terraform/modules/__v3__/subnet"
61+
62+
name = "${local.project}-cosmosb-snet"
63+
resource_group_name = local.vnet_resource_group_name
64+
virtual_network_name = local.vnet_name
65+
66+
address_prefixes = var.cidr_subnet_cosmosdb_qi
67+
68+
private_endpoint_network_policies_enabled = true
69+
70+
service_endpoints = [
71+
"Microsoft.Web",
72+
"Microsoft.AzureCosmosDB",
73+
]
74+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
resource "azurerm_resource_group" "cosmosdb_qi_rg" {
2+
name = "${local.project}-cosmosdb-rg"
3+
location = var.location
4+
5+
tags = module.tag_config.tags
6+
}
7+
8+
module "cosmosdb_account_qi_mongodb" {
9+
10+
source = "./.terraform/modules/__v3__/cosmosdb_account"
11+
12+
name = "${local.project}-cosmos-account"
13+
location = var.location
14+
resource_group_name = azurerm_resource_group.cosmosdb_qi_rg.name
15+
domain = var.domain
16+
17+
offer_type = var.cosmos_mongo_db_params.offer_type
18+
kind = var.cosmos_mongo_db_params.kind
19+
capabilities = var.cosmos_mongo_db_params.capabilities
20+
# mongo_server_version = var.cosmos_mongo_db_params.server_version
21+
enable_free_tier = var.cosmos_mongo_db_params.enable_free_tier
22+
23+
public_network_access_enabled = var.cosmos_mongo_db_params.public_network_access_enabled
24+
private_endpoint_enabled = var.cosmos_mongo_db_params.private_endpoint_enabled
25+
subnet_id = module.cosmosdb_qi_snet.id
26+
private_dns_zone_mongo_ids = [data.azurerm_private_dns_zone.cosmos.id]
27+
is_virtual_network_filter_enabled = var.cosmos_mongo_db_params.is_virtual_network_filter_enabled
28+
allowed_virtual_network_subnet_ids = var.cosmos_mongo_db_params.public_network_access_enabled ? [] : [data.azurerm_subnet.aks_subnet.id]
29+
30+
consistency_policy = var.cosmos_mongo_db_params.consistency_policy
31+
main_geo_location_location = azurerm_resource_group.cosmosdb_qi_rg.location
32+
main_geo_location_zone_redundant = var.cosmos_mongo_db_params.main_geo_location_zone_redundant
33+
additional_geo_locations = var.cosmos_mongo_db_params.additional_geo_locations
34+
35+
backup_continuous_enabled = var.cosmos_mongo_db_params.backup_continuous_enabled
36+
enable_provisioned_throughput_exceeded_alert = var.cosmos_mongo_db_params.enable_provisioned_throughput_exceeded_alert
37+
38+
tags = module.tag_config.tags
39+
}
40+
41+
resource "azurerm_cosmosdb_mongo_database" "accounting_reconciliation" {
42+
43+
name = "accounting-reconciliation"
44+
resource_group_name = azurerm_resource_group.cosmosdb_qi_rg.name
45+
account_name = module.cosmosdb_account_qi_mongodb.name
46+
47+
throughput = var.cosmos_mongo_db_accounting_reconciliation_params.enable_autoscaling || var.cosmos_mongo_db_accounting_reconciliation_params.enable_serverless ? null : var.cosmos_mongo_db_accounting_reconciliation_params.throughput
48+
49+
dynamic "autoscale_settings" {
50+
for_each = var.cosmos_mongo_db_accounting_reconciliation_params.enable_autoscaling && !var.cosmos_mongo_db_accounting_reconciliation_params.enable_serverless ? [""] : []
51+
content {
52+
max_throughput = var.cosmos_mongo_db_accounting_reconciliation_params.max_throughput
53+
}
54+
}
55+
56+
}
57+
58+
# Collections
59+
locals {
60+
accounting_reconciliation_collections = [
61+
{
62+
name = "accounting-zip"
63+
indexes = [
64+
{
65+
keys = ["_id"]
66+
unique = true
67+
}
68+
]
69+
shard_key = "_id",
70+
default_ttl_seconds = "15552000" # 180 days = 180d * 24h * 60m * 60s
71+
},
72+
{
73+
name = "accounting-xml"
74+
indexes = [
75+
{
76+
keys = ["_id"]
77+
unique = true
78+
}
79+
]
80+
shard_key = "_id",
81+
default_ttl_seconds = "15552000" # 180 days = 180d * 24h * 60m * 60s
82+
}
83+
]
84+
}
85+
86+
module "cosmosdb_accounting_reconciliation_collections" {
87+
88+
source = "./.terraform/modules/__v3__/cosmosdb_mongodb_collection"
89+
for_each = { for index, coll in local.accounting_reconciliation_collections : coll.name => coll }
90+
91+
name = each.value.name
92+
resource_group_name = azurerm_resource_group.cosmosdb_qi_rg.name
93+
94+
cosmosdb_mongo_account_name = module.cosmosdb_account_qi_mongodb.name
95+
cosmosdb_mongo_database_name = azurerm_cosmosdb_mongo_database.accounting_reconciliation.name
96+
97+
indexes = each.value.indexes
98+
shard_key = each.value.shard_key
99+
default_ttl_seconds = each.value.default_ttl_seconds
100+
lock_enable = var.env_short != "p" ? false : true
101+
}

src/domains/qi-common/99_locals.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ locals {
2828
internal_dns_zone_name = "${var.dns_zone_internal_prefix}.${var.external_domain}"
2929
internal_dns_zone_resource_group_name = "${local.product}-vnet-rg"
3030

31+
cosmos_dns_zone_name = "privatelink.mongo.cosmos.azure.com"
32+
cosmos_dns_zone_resource_group_name = "${local.product}-vnet-rg"
33+
3134
aks_subnet_name = "${var.prefix}-${var.env_short}-${var.location_short}-${var.env}-aks-snet"
3235

3336
azdo_managed_identity_rg_name = "pagopa-${var.env_short}-identity-rg"

src/domains/qi-common/99_variables.tf

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,46 @@ variable "eventhubs_bdi" {
230230
}))
231231
default = []
232232
}
233+
234+
# CosmosDb
235+
236+
variable "cosmos_mongo_db_params" {
237+
type = object({
238+
enabled = bool
239+
capabilities = list(string)
240+
offer_type = string
241+
server_version = string
242+
kind = string
243+
consistency_policy = object({
244+
consistency_level = string
245+
max_interval_in_seconds = number
246+
max_staleness_prefix = number
247+
})
248+
enable_free_tier = bool
249+
main_geo_location_zone_redundant = bool
250+
additional_geo_locations = list(object({
251+
location = string
252+
failover_priority = number
253+
zone_redundant = bool
254+
}))
255+
private_endpoint_enabled = bool
256+
public_network_access_enabled = bool
257+
is_virtual_network_filter_enabled = bool
258+
backup_continuous_enabled = bool
259+
enable_provisioned_throughput_exceeded_alert = bool
260+
})
261+
}
262+
263+
variable "cosmos_mongo_db_accounting_reconciliation_params" {
264+
type = object({
265+
enable_serverless = bool
266+
enable_autoscaling = bool
267+
throughput = number
268+
max_throughput = number
269+
})
270+
}
271+
272+
variable "cidr_subnet_cosmosdb_qi" {
273+
type = list(string)
274+
description = "Cosmos DB address space for qi."
275+
}

src/domains/qi-common/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
| Name | Source | Version |
1414
|------|--------|---------|
1515
| <a name="module___v3__"></a> [\_\_v3\_\_](#module\_\_\_v3\_\_) | git::https://github.com/pagopa/terraform-azurerm-v3 | f3485105e35ce8c801209dcbb4ef72f3d944f0e5 |
16+
| <a name="module_cosmosdb_account_qi_mongodb"></a> [cosmosdb\_account\_qi\_mongodb](#module\_cosmosdb\_account\_qi\_mongodb) | ./.terraform/modules/__v3__/cosmosdb_account | n/a |
17+
| <a name="module_cosmosdb_accounting_reconciliation_collections"></a> [cosmosdb\_accounting\_reconciliation\_collections](#module\_cosmosdb\_accounting\_reconciliation\_collections) | ./.terraform/modules/__v3__/cosmosdb_mongodb_collection | n/a |
18+
| <a name="module_cosmosdb_qi_snet"></a> [cosmosdb\_qi\_snet](#module\_cosmosdb\_qi\_snet) | ./.terraform/modules/__v3__/subnet | n/a |
1619
| <a name="module_eventhub_namespace_qi"></a> [eventhub\_namespace\_qi](#module\_eventhub\_namespace\_qi) | ./.terraform/modules/__v3__/eventhub | n/a |
1720
| <a name="module_eventhub_qi_configuration"></a> [eventhub\_qi\_configuration](#module\_eventhub\_qi\_configuration) | ./.terraform/modules/__v3__/eventhub_configuration | n/a |
1821
| <a name="module_identity_cd_01"></a> [identity\_cd\_01](#module\_identity\_cd\_01) | github.com/pagopa/terraform-azurerm-v3//github_federated_identity | n/a |
@@ -29,6 +32,7 @@
2932
| [azuread_application.qi_app](https://registry.terraform.io/providers/hashicorp/azuread/2.38.0/docs/resources/application) | resource |
3033
| [azuread_application_password.qi_app_pwd](https://registry.terraform.io/providers/hashicorp/azuread/2.38.0/docs/resources/application_password) | resource |
3134
| [azuread_service_principal.qi_sp](https://registry.terraform.io/providers/hashicorp/azuread/2.38.0/docs/resources/service_principal) | resource |
35+
| [azurerm_cosmosdb_mongo_database.accounting_reconciliation](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cosmosdb_mongo_database) | resource |
3236
| [azurerm_key_vault_access_policy.ad_group_policy](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/key_vault_access_policy) | resource |
3337
| [azurerm_key_vault_access_policy.adgroup_developers_policy](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/key_vault_access_policy) | resource |
3438
| [azurerm_key_vault_access_policy.adgroup_externals_policy](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/key_vault_access_policy) | resource |
@@ -50,6 +54,7 @@
5054
| [azurerm_key_vault_secret.qi_service_principal_client_id](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/key_vault_secret) | resource |
5155
| [azurerm_key_vault_secret.qi_service_principal_client_secret](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/key_vault_secret) | resource |
5256
| [azurerm_private_dns_a_record.ingress](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/private_dns_a_record) | resource |
57+
| [azurerm_resource_group.cosmosdb_qi_rg](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group) | resource |
5358
| [azurerm_resource_group.qi_evh_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group) | resource |
5459
| [azurerm_resource_group.qi_rg](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group) | resource |
5560
| [azurerm_resource_group.sec_rg](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group) | resource |
@@ -76,12 +81,14 @@
7681
| [azurerm_log_analytics_workspace.log_analytics](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/log_analytics_workspace) | data source |
7782
| [azurerm_monitor_action_group.email](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/monitor_action_group) | data source |
7883
| [azurerm_monitor_action_group.slack](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/monitor_action_group) | data source |
84+
| [azurerm_private_dns_zone.cosmos](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/private_dns_zone) | data source |
7985
| [azurerm_private_dns_zone.eventhub](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/private_dns_zone) | data source |
8086
| [azurerm_private_dns_zone.internal](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/private_dns_zone) | data source |
8187
| [azurerm_resource_group.identity_rg](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/resource_group) | data source |
8288
| [azurerm_resource_group.monitor_rg](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/resource_group) | data source |
8389
| [azurerm_resource_group.rg_event_private_dns_zone](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/resource_group) | data source |
8490
| [azurerm_resource_group.rg_vnet_italy](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/resource_group) | data source |
91+
| [azurerm_subnet.aks_subnet](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/subnet) | data source |
8592
| [azurerm_subscription.current](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/subscription) | data source |
8693
| [azurerm_user_assigned_identity.iac_federated_azdo](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/user_assigned_identity) | data source |
8794
| [azurerm_virtual_network.vnet](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/virtual_network) | data source |
@@ -91,7 +98,10 @@
9198

9299
| Name | Description | Type | Default | Required |
93100
|------|-------------|------|---------|:--------:|
101+
| <a name="input_cidr_subnet_cosmosdb_qi"></a> [cidr\_subnet\_cosmosdb\_qi](#input\_cidr\_subnet\_cosmosdb\_qi) | Cosmos DB address space for qi. | `list(string)` | n/a | yes |
94102
| <a name="input_cidr_subnet_qi_evh"></a> [cidr\_subnet\_qi\_evh](#input\_cidr\_subnet\_qi\_evh) | Address prefixes evh | `list(string)` | n/a | yes |
103+
| <a name="input_cosmos_mongo_db_accounting_reconciliation_params"></a> [cosmos\_mongo\_db\_accounting\_reconciliation\_params](#input\_cosmos\_mongo\_db\_accounting\_reconciliation\_params) | n/a | <pre>object({<br/> enable_serverless = bool<br/> enable_autoscaling = bool<br/> throughput = number<br/> max_throughput = number<br/> })</pre> | n/a | yes |
104+
| <a name="input_cosmos_mongo_db_params"></a> [cosmos\_mongo\_db\_params](#input\_cosmos\_mongo\_db\_params) | n/a | <pre>object({<br/> enabled = bool<br/> capabilities = list(string)<br/> offer_type = string<br/> server_version = string<br/> kind = string<br/> consistency_policy = object({<br/> consistency_level = string<br/> max_interval_in_seconds = number<br/> max_staleness_prefix = number<br/> })<br/> enable_free_tier = bool<br/> main_geo_location_zone_redundant = bool<br/> additional_geo_locations = list(object({<br/> location = string<br/> failover_priority = number<br/> zone_redundant = bool<br/> }))<br/> private_endpoint_enabled = bool<br/> public_network_access_enabled = bool<br/> is_virtual_network_filter_enabled = bool<br/> backup_continuous_enabled = bool<br/> enable_provisioned_throughput_exceeded_alert = bool<br/> })</pre> | n/a | yes |
95105
| <a name="input_dns_zone_internal_prefix"></a> [dns\_zone\_internal\_prefix](#input\_dns\_zone\_internal\_prefix) | The dns subdomain. | `string` | `null` | no |
96106
| <a name="input_domain"></a> [domain](#input\_domain) | n/a | `string` | n/a | yes |
97107
| <a name="input_ehns_alerts_enabled"></a> [ehns\_alerts\_enabled](#input\_ehns\_alerts\_enabled) | Event hub alerts enabled? | `bool` | n/a | yes |

src/domains/qi-common/env/weu-dev/terraform.tfvars

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,38 @@ ehns_metric_alerts_qi = {
123123
# ],
124124
# },
125125
}
126+
127+
### Cosmos
128+
129+
cosmos_mongo_db_params = {
130+
enabled = true
131+
kind = "MongoDB"
132+
capabilities = ["EnableMongo", "EnableServerless"]
133+
offer_type = "Standard"
134+
consistency_policy = {
135+
consistency_level = "BoundedStaleness"
136+
max_interval_in_seconds = 5
137+
max_staleness_prefix = 100000
138+
}
139+
server_version = "6.0"
140+
main_geo_location_zone_redundant = false
141+
enable_free_tier = false
142+
143+
additional_geo_locations = []
144+
private_endpoint_enabled = false
145+
public_network_access_enabled = true
146+
is_virtual_network_filter_enabled = false
147+
148+
backup_continuous_enabled = false
149+
enable_provisioned_throughput_exceeded_alert = false
150+
151+
}
152+
153+
cosmos_mongo_db_accounting_reconciliation_params = {
154+
enable_serverless = true
155+
enable_autoscaling = true
156+
max_throughput = 1000
157+
throughput = 1000
158+
}
159+
160+
cidr_subnet_cosmosdb_qi = ["10.1.132.0/24"]

src/domains/qi-common/env/weu-prod/terraform.tfvars

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,42 @@ ehns_metric_alerts_qi = {
139139
# ],
140140
# },
141141
}
142+
143+
### Cosmos
144+
145+
cosmos_mongo_db_params = {
146+
enabled = true
147+
kind = "MongoDB"
148+
capabilities = ["EnableMongo", "DisableRateLimitingResponses"]
149+
offer_type = "Standard"
150+
consistency_policy = {
151+
consistency_level = "BoundedStaleness"
152+
max_interval_in_seconds = 300
153+
max_staleness_prefix = 100000
154+
}
155+
server_version = "7.0"
156+
main_geo_location_zone_redundant = true
157+
enable_free_tier = false
158+
159+
additional_geo_locations = [{
160+
location = "northeurope"
161+
failover_priority = 1
162+
zone_redundant = false
163+
}]
164+
private_endpoint_enabled = true
165+
public_network_access_enabled = false
166+
is_virtual_network_filter_enabled = true
167+
168+
backup_continuous_enabled = true
169+
enable_provisioned_throughput_exceeded_alert = false
170+
171+
}
172+
173+
cosmos_mongo_db_accounting_reconciliation_params = {
174+
enable_serverless = false
175+
enable_autoscaling = true
176+
max_throughput = 5000
177+
throughput = 1000
178+
}
179+
180+
cidr_subnet_cosmosdb_qi = ["10.1.132.0/24"]

src/domains/qi-common/env/weu-uat/terraform.tfvars

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,38 @@ ehns_metric_alerts_qi = {
123123
# ],
124124
# },
125125
}
126+
127+
### Cosmos
128+
129+
cosmos_mongo_db_params = {
130+
enabled = true
131+
kind = "MongoDB"
132+
capabilities = ["EnableMongo", "DisableRateLimitingResponses"]
133+
offer_type = "Standard"
134+
consistency_policy = {
135+
consistency_level = "BoundedStaleness"
136+
max_interval_in_seconds = 5
137+
max_staleness_prefix = 100000
138+
}
139+
server_version = "6.0"
140+
main_geo_location_zone_redundant = false
141+
enable_free_tier = false
142+
143+
additional_geo_locations = []
144+
private_endpoint_enabled = true
145+
public_network_access_enabled = false
146+
is_virtual_network_filter_enabled = true
147+
148+
backup_continuous_enabled = false
149+
enable_provisioned_throughput_exceeded_alert = false
150+
151+
}
152+
153+
cosmos_mongo_db_accounting_reconciliation_params = {
154+
enable_serverless = true
155+
enable_autoscaling = true
156+
max_throughput = 1000
157+
throughput = 1000
158+
}
159+
160+
cidr_subnet_cosmosdb_qi = ["10.1.132.0/24"]

0 commit comments

Comments
 (0)