Skip to content

Commit

Permalink
fix: cosmosdb default to mongo
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldotyu committed Jan 24, 2024
1 parent a81cce9 commit 325588d
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions infra/cosmosdb.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -32,15 +32,15 @@ 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
throughput = 400
}

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
Expand All @@ -54,15 +54,15 @@ 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
throughput = 400
}

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
Expand Down
7 changes: 7 additions & 0 deletions infra/locals.tf
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 0 additions & 8 deletions infra/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {}

Expand Down
4 changes: 2 additions & 2 deletions infra/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
10 changes: 5 additions & 5 deletions infra/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down

0 comments on commit 325588d

Please sign in to comment.