diff --git a/README.md b/README.md index 92827202..0376a980 100644 --- a/README.md +++ b/README.md @@ -119,7 +119,7 @@ azd auth login az login ``` -The `makeline-service` supports both MongoDB and SQL API for accessing data in Azure CosmosDB. The default API is MongoDB, but you can use SQL API. To use the SQL API for Azure CosmosDB, you must provision the service using the `GlobalDocumentDB` account kind. You can set the Azure CosmosDB account kind by running the following command prior to running `azd up`: +The `makeline-service` supports both MongoDB and SQL API for accessing data in Azure CosmosDB. The default API is `MongoDB`, but you can use SQL API. To use the SQL API for Azure CosmosDB, you must provision the service using the `GlobalDocumentDB` account kind. You can set the Azure CosmosDB account kind by running the following command prior to running `azd up`: ```bash azd env set AZURE_COSMOSDB_ACCOUNT_KIND GlobalDocumentDB diff --git a/infra/cosmosdb.tf b/infra/cosmosdb.tf index 771ee7a6..f21af43d 100644 --- a/infra/cosmosdb.tf +++ b/infra/cosmosdb.tf @@ -8,7 +8,7 @@ resource "azurerm_cosmosdb_account" "example" { enable_automatic_failover = false dynamic "capabilities" { - for_each = var.cosmosdb_account_kind == "MongoDB" ? ["EnableAggregationPipeline", "mongoEnableDocLevelTTL", "MongoDBv3.4", "EnableMongo"] : ["EnableAggregationPipeline"] + for_each = local.cosmosdb_account_kind == "MongoDB" ? ["EnableAggregationPipeline", "mongoEnableDocLevelTTL", "MongoDBv3.4", "EnableMongo"] : ["EnableAggregationPipeline"] content { name = capabilities.value } @@ -32,7 +32,7 @@ resource "azurerm_cosmosdb_account" "example" { } resource "azurerm_cosmosdb_mongo_database" "example" { - count = var.cosmosdb_account_kind == "MongoDB" ? 1 : 0 + count = local.cosmosdb_account_kind == "MongoDB" ? 1 : 0 name = "orderdb" resource_group_name = azurerm_cosmosdb_account.example.resource_group_name account_name = azurerm_cosmosdb_account.example.name @@ -40,7 +40,7 @@ resource "azurerm_cosmosdb_mongo_database" "example" { } resource "azurerm_cosmosdb_mongo_collection" "example" { - count = var.cosmosdb_account_kind == "MongoDB" ? 1 : 0 + count = local.cosmosdb_account_kind == "MongoDB" ? 1 : 0 name = "orders" resource_group_name = azurerm_cosmosdb_account.example.resource_group_name account_name = azurerm_cosmosdb_account.example.name @@ -54,7 +54,7 @@ resource "azurerm_cosmosdb_mongo_collection" "example" { } resource "azurerm_cosmosdb_sql_database" "example" { - count = var.cosmosdb_account_kind == "GlobalDocumentDB" ? 1 : 0 + count = local.cosmosdb_account_kind == "GlobalDocumentDB" ? 1 : 0 name = "orderdb" resource_group_name = azurerm_cosmosdb_account.example.resource_group_name account_name = azurerm_cosmosdb_account.example.name @@ -62,7 +62,7 @@ resource "azurerm_cosmosdb_sql_database" "example" { } resource "azurerm_cosmosdb_sql_container" "example" { - count = var.cosmosdb_account_kind == "GlobalDocumentDB" ? 1 : 0 + count = local.cosmosdb_account_kind == "GlobalDocumentDB" ? 1 : 0 name = "orders" resource_group_name = azurerm_cosmosdb_account.example.resource_group_name account_name = azurerm_cosmosdb_account.example.name diff --git a/infra/locals.tf b/infra/locals.tf new file mode 100644 index 00000000..c8caf81b --- /dev/null +++ b/infra/locals.tf @@ -0,0 +1,7 @@ +locals { + name = "${random_pet.example.id}${random_integer.example.result}" + location = var.location + default_cosmosdb_account_kind = "MongoDB" + cosmosdb_account_kind = var.cosmosdb_account_kind != "" ? var.cosmosdb_account_kind : local.default_cosmosdb_account_kind + deploy_acr = var.deploy_acr == "true" ? true : false +} \ No newline at end of file diff --git a/infra/main.tf b/infra/main.tf index 53f12e57..83e75dd8 100644 --- a/infra/main.tf +++ b/infra/main.tf @@ -42,14 +42,6 @@ resource "random_pet" "example" { } } -locals { - name = "${random_pet.example.id}${random_integer.example.result}" - location = var.location - default_cosmosdb_account_kind = "MongoDB" - cosmosdb_account_kind = var.cosmosdb_account_kind != "" ? var.cosmosdb_account_kind : local.default_cosmosdb_account_kind - deploy_acr = var.deploy_acr == "true" ? true : false -} - data "azurerm_subscription" "current" {} data "azurerm_client_config" "current" {} diff --git a/infra/outputs.tf b/infra/outputs.tf index fc183192..3c67d098 100644 --- a/infra/outputs.tf +++ b/infra/outputs.tf @@ -55,11 +55,11 @@ output "db_account_name" { } output "db_api" { - value = var.cosmosdb_account_kind == "MongoDB" ? "mongodb" : "cosmosdbsql" + value = local.cosmosdb_account_kind == "MongoDB" ? "mongodb" : "cosmosdbsql" } output "db_uri" { - value = var.cosmosdb_account_kind == "MongoDB" ? "mongodb://${azurerm_cosmosdb_account.example.name}.mongo.cosmos.azure.com:10255/?retryWrites=false" : "https://${azurerm_cosmosdb_account.example.name}.documents.azure.com:443/" + value = local.cosmosdb_account_kind == "MongoDB" ? "mongodb://${azurerm_cosmosdb_account.example.name}.mongo.cosmos.azure.com:10255/?retryWrites=false" : "https://${azurerm_cosmosdb_account.example.name}.documents.azure.com:443/" } output "db_key" { diff --git a/infra/variables.tf b/infra/variables.tf index 449428ef..b540e38f 100644 --- a/infra/variables.tf +++ b/infra/variables.tf @@ -31,14 +31,14 @@ variable "k8s_namespace" { } variable "cosmosdb_account_kind" { - description = "value of cosmosdb account kind" + description = "value of cosmosdb account kind. this string value will be used to set the local variable" type = string default = "MongoDB" - validation { - condition = contains(["MongoDB", "GlobalDocumentDB"], var.cosmosdb_account_kind) - error_message = "Valid values for var: cosmosdb_account_kind are (MongoDB, GlobalDocumentDB)." - } + # validation { + # condition = contains(["MongoDB", "GlobalDocumentDB"], local.cosmosdb_account_kind) + # error_message = "Valid values for var: cosmosdb_account_kind are (MongoDB, GlobalDocumentDB)." + # } } variable "deploy_acr" {