fix 101-cosmos-db-serverside-functionality example (#163)

This commit is contained in:
Dingjia Chen 2023-02-16 20:29:14 -06:00 committed by GitHub
parent c4d0e56052
commit f4ddf1cd2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 44 deletions

View File

@ -1,10 +1,10 @@
resource "azurerm_resource_group" "example" { resource "azurerm_resource_group" "example" {
name = var.resource_group_name name = "${random_pet.prefix.id}-rg"
location = var.location location = var.location
} }
resource "azurerm_cosmosdb_account" "example" { resource "azurerm_cosmosdb_account" "example" {
name = var.cosmosdb_account_name name = "${random_pet.prefix.id}-cosmosdb"
location = var.cosmosdb_account_location location = var.cosmosdb_account_location
resource_group_name = azurerm_resource_group.example.name resource_group_name = azurerm_resource_group.example.name
offer_type = "Standard" offer_type = "Standard"
@ -26,18 +26,18 @@ resource "azurerm_cosmosdb_account" "example" {
] ]
} }
resource "azurerm_cosmosdb_sql_database" "example" { resource "azurerm_cosmosdb_sql_database" "main" {
name = var.cosmosdb_sqldb_name name = "${random_pet.prefix.id}-sqldb"
resource_group_name = azurerm_resource_group.example.name resource_group_name = azurerm_resource_group.example.name
account_name = azurerm_cosmosdb_account.example.name account_name = azurerm_cosmosdb_account.example.name
throughput = var.throughput throughput = var.throughput
} }
resource "azurerm_cosmosdb_sql_container" "example" { resource "azurerm_cosmosdb_sql_container" "example" {
name = var.sql_container_name name = "${random_pet.prefix.id}-sql-container"
resource_group_name = azurerm_resource_group.example.name resource_group_name = azurerm_resource_group.example.name
account_name = azurerm_cosmosdb_account.example.name account_name = azurerm_cosmosdb_account.example.name
database_name = azurerm_cosmosdb_sql_database.example.name database_name = azurerm_cosmosdb_sql_database.main.name
partition_key_path = "/definition/id" partition_key_path = "/definition/id"
partition_key_version = 1 partition_key_version = 1
throughput = 400 throughput = 400
@ -64,16 +64,16 @@ resource "azurerm_cosmosdb_sql_container" "example" {
} }
resource "azurerm_cosmosdb_sql_stored_procedure" "example" { resource "azurerm_cosmosdb_sql_stored_procedure" "example" {
name = var.sql_stored_procedure_name name = "${random_pet.prefix.id}-sql-stored-procedure"
resource_group_name = azurerm_resource_group.example.name resource_group_name = azurerm_resource_group.example.name
account_name = azurerm_cosmosdb_account.example.name account_name = azurerm_cosmosdb_account.example.name
database_name = azurerm_cosmosdb_sql_database.example.name database_name = azurerm_cosmosdb_sql_database.main.name
container_name = azurerm_cosmosdb_sql_container.example.name container_name = azurerm_cosmosdb_sql_container.example.name
body = "function () { var context = getContext(); var response = context.getResponse(); response.setBody('Hello, World'); }" body = "function () { var context = getContext(); var response = context.getResponse(); response.setBody('Hello, World'); }"
} }
resource "azurerm_cosmosdb_sql_trigger" "example" { resource "azurerm_cosmosdb_sql_trigger" "example" {
name = var.sql_trigger_name name = "${random_pet.prefix.id}-sql-trigger"
container_id = azurerm_cosmosdb_sql_container.example.id container_id = azurerm_cosmosdb_sql_container.example.id
body = "function validateToDoItemTimestamp(){var context=getContext();var request=context.getRequest();var itemToCreate=request.getBody();if(!('timestamp'in itemToCreate)){var ts=new Date();itemToCreate['timestamp']=ts.getTime();}request.setBody(itemToCreate);}" body = "function validateToDoItemTimestamp(){var context=getContext();var request=context.getRequest();var itemToCreate=request.getBody();if(!('timestamp'in itemToCreate)){var ts=new Date();itemToCreate['timestamp']=ts.getTime();}request.setBody(itemToCreate);}"
operation = "Create" operation = "Create"
@ -81,7 +81,12 @@ resource "azurerm_cosmosdb_sql_trigger" "example" {
} }
resource "azurerm_cosmosdb_sql_function" "example" { resource "azurerm_cosmosdb_sql_function" "example" {
name = var.sql_function_name name = "${random_pet.prefix.id}-sql-function"
container_id = azurerm_cosmosdb_sql_container.example.id container_id = azurerm_cosmosdb_sql_container.example.id
body = "function tax(income){if(income==undefined)throw'no input';if(income<1000)return income*0.1;else if(income<10000)return income*0.2;else return income*0.4;}" body = "function tax(income){if(income==undefined)throw'no input';if(income<1000)return income*0.1;else if(income<10000)return income*0.2;else return income*0.4;}"
} }
resource "random_pet" "prefix" {
prefix = var.prefix
length = 1
}

View File

@ -3,5 +3,5 @@ output "cosmosdb_account_id" {
} }
output "cosmosdb_sql_database_id" { output "cosmosdb_sql_database_id" {
value = azurerm_cosmosdb_sql_database.example.id value = azurerm_cosmosdb_sql_database.main.id
} }

View File

@ -1,8 +1,14 @@
terraform { terraform {
required_version = ">= 1.0"
required_providers { required_providers {
azurerm = { azurerm = {
source = "hashicorp/azurerm" source = "hashicorp/azurerm"
version = ">=3.0.0" version = ">= 3.0, < 4.0"
}
random = {
source = "hashicorp/random"
version = ">= 3.0"
} }
} }
} }

View File

@ -1,30 +1,24 @@
variable "resource_group_name" { variable "prefix" {
type = string type = string
description = "Resource group name" default = "cosmos-db-ss-func"
description = "Prefix of the resource name"
} }
variable "location" { variable "location" {
type = string type = string
default = "eastus"
description = "Resource group location" description = "Resource group location"
} }
variable "cosmosdb_account_name" {
type = string
description = "Cosmos db account name"
}
variable "cosmosdb_account_location" { variable "cosmosdb_account_location" {
type = string type = string
default = "eastus"
description = "Cosmos db account location" description = "Cosmos db account location"
} }
variable "cosmosdb_sqldb_name" {
type = string
description = "value"
}
variable "throughput" { variable "throughput" {
type = number type = number
default = 400
description = "Cosmos db database throughput" description = "Cosmos db database throughput"
validation { validation {
condition = var.throughput >= 400 && var.throughput <= 1000000 condition = var.throughput >= 400 && var.throughput <= 1000000
@ -35,23 +29,3 @@ variable "throughput" {
error_message = "Cosmos db throughput should be in increments of 100." error_message = "Cosmos db throughput should be in increments of 100."
} }
} }
variable "sql_container_name" {
type = string
description = "SQL API container name."
}
variable "sql_stored_procedure_name" {
type = string
description = "Stored procedure name"
}
variable "sql_trigger_name" {
type = string
description = "Trigger name"
}
variable "sql_function_name" {
type = string
description = "User defined function name"
}