Skip to content

Commit

Permalink
update db to infra/app
Browse files Browse the repository at this point in the history
  • Loading branch information
Menghua1 committed Sep 29, 2024
1 parent 8f34f52 commit 3399e7f
Show file tree
Hide file tree
Showing 27 changed files with 339 additions and 652 deletions.
77 changes: 77 additions & 0 deletions templates/todo/common/infra/bicep/app/cosmos-mongo-db-avm.bicep
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
param accountName string
param location string = resourceGroup().location
param tags object = {}
param cosmosDatabaseName string = ''
param keyVaultResourceId string
param connectionStringKey string = 'AZURE-COSMOS-CONNECTION-STRING'
param collections array = [
{
name: 'TodoList'
id: 'TodoList'
shardKey: {
keys: [
'Hash'
]
}
indexes: [
{
key: {
keys: [
'_id'
]
}
}
]
}
{
name: 'TodoItem'
id: 'TodoItem'
shardKey: {
keys: [
'Hash'
]
}
indexes: [
{
key: {
keys: [
'_id'
]
}
}
]
}
]

var defaultDatabaseName = 'Todo'
var actualDatabaseName = !empty(cosmosDatabaseName) ? cosmosDatabaseName : defaultDatabaseName

module cosmos 'br/public:avm/res/document-db/database-account:0.6.0' = {
name: 'cosmos'
params: {
locations: [
{
failoverPriority: 0
isZoneRedundant: false
locationName: location
}
]
name: accountName
location: location
mongodbDatabases: [
{
name: actualDatabaseName
tags: tags
collections: collections
}
]
secretsExportConfiguration: {
keyVaultResourceId: keyVaultResourceId
primaryWriteConnectionStringSecretName: connectionStringKey
}
}
}

output connectionStringKey string = connectionStringKey
output databaseName string = cosmos.outputs.name
output endpoint string = cosmos.outputs.endpoint
58 changes: 58 additions & 0 deletions templates/todo/common/infra/bicep/app/cosmos-sql-db-avm.bicep
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
param accountName string
param location string = resourceGroup().location
param tags object = {}
param connectionStringKey string = 'AZURE-COSMOS-CONNECTION-STRING'
param databaseName string = ''
param keyVaultResourceId string
param principalId string = ''

var defaultDatabaseName = 'Todo'
var actualDatabaseName = !empty(databaseName) ? databaseName : defaultDatabaseName

module cosmos 'br/public:avm/res/document-db/database-account:0.6.0' = {
name: 'cosmos'
params: {
name: accountName
location: location
tags: tags
locations: [
{
failoverPriority: 0
locationName: location
isZoneRedundant: false
}
]
secretsExportConfiguration:{
keyVaultResourceId: keyVaultResourceId
primaryWriteConnectionStringSecretName: connectionStringKey
}
capabilitiesToAdd: [ 'EnableServerless' ]
automaticFailover: false
sqlDatabases: [
{
name: actualDatabaseName
containers: [
{
name: 'TodoList'
paths: [ 'id' ]
}
{
name: 'TodoItem'
paths: [ 'id' ]
}
]
}
]
sqlRoleAssignmentsPrincipalIds: [ principalId ]
sqlRoleDefinitions: [
{
name: 'writer'
}
]
}
}

output accountName string = cosmos.outputs.name
output connectionStringKey string = connectionStringKey
output databaseName string = actualDatabaseName
output endpoint string = cosmos.outputs.endpoint
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
param location string = resourceGroup().location

@description('Application user name')
param appUser string = 'appUser'
param appUser string

@description('SQL Server administrator name')
param sqlAdmin string = 'sqlAdmin'

@description('The name for sql database ')
param sqlDatabaseName string
param sqlDatabaseName string = ''

@description('Resource name for sql service')
param sqlServiceName string
Expand All @@ -20,6 +20,35 @@ param sqlAdminPassword string
@description('Application user password')
param appUserPassword string

param tags object = {}

var defaultDatabaseName = 'Todo'
var actualDatabaseName = !empty(sqlDatabaseName) ? sqlDatabaseName : defaultDatabaseName

module sqlServer 'br/public:avm/res/sql/server:0.2.0' = {
name: 'sqlservice'
params: {
name: sqlServiceName
administratorLogin: sqlAdmin
administratorLoginPassword: sqlAdminPassword
location: location
tags: tags
publicNetworkAccess: 'Enabled'
databases: [
{
name: actualDatabaseName
}
]
firewallRules: [
{
name: 'Azure Services'
startIpAddress: '0.0.0.1'
endIpAddress: '255.255.255.254'
}
]
}
}

module deploymentScript 'br/public:avm/res/resources/deployment-script:0.1.3' = {
name: 'deployment-script'
params: {
Expand All @@ -42,11 +71,11 @@ module deploymentScript 'br/public:avm/res/resources/deployment-script:0.1.3' =
}
{
name: 'DBNAME'
value: !empty(sqlDatabaseName) ? sqlDatabaseName : 'Todo'
value: actualDatabaseName
}
{
name: 'DBSERVER'
value: '${sqlServiceName}${environment().suffixes.sqlServerHostname}'
value: '${sqlServer.outputs.name}${environment().suffixes.sqlServerHostname}'
}
{
name: 'SQLCMDPASSWORD'
Expand Down Expand Up @@ -75,3 +104,6 @@ SCRIPT_END
'''
}
}

output databaseName string = actualDatabaseName
output sqlServerName string = sqlServer.outputs.name
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ param applicationInsightsDashboardName string = ''
param applicationInsightsName string = ''
param appServicePlanName string = ''
param cosmosAccountName string = ''
param cosmosDatabaseName string = ''
param keyVaultName string = ''
param logAnalyticsName string = ''
param resourceGroupName string = ''
param webServiceName string = ''
param apimServiceName string = ''
param connectionStringKey string = 'AZURE-COSMOS-CONNECTION-STRING'
param apimApiName string = 'todo-api'
param apimLoggerName string = 'app-insights-logger'

Expand All @@ -40,7 +38,6 @@ param principalId string = ''
var abbrs = loadJsonContent('../../../../../../common/infra/bicep/abbreviations.json')
var resourceToken = toLower(uniqueString(subscription().id, environmentName, location))
var tags = { 'azd-env-name': environmentName }
var actualDatabaseName = !empty(cosmosDatabaseName) ? cosmosDatabaseName : 'Todo'

// Organize resources in a resource group
resource rg 'Microsoft.Resources/resourceGroups@2021-04-01' = {
Expand Down Expand Up @@ -79,8 +76,8 @@ module api '../../../../../common/infra/bicep/app/api-appservice-avm.bicep' = {
}
appSettings: {
AZURE_KEY_VAULT_ENDPOINT: keyVault.outputs.uri
AZURE_COSMOS_CONNECTION_STRING_KEY: connectionStringKey
AZURE_COSMOS_DATABASE_NAME: actualDatabaseName
AZURE_COSMOS_CONNECTION_STRING_KEY: cosmos.outputs.connectionStringKey
AZURE_COSMOS_DATABASE_NAME: cosmos.outputs.databaseName
AZURE_COSMOS_ENDPOINT: cosmos.outputs.endpoint
API_ALLOW_ORIGINS: web.outputs.SERVICE_WEB_URI
SCM_DO_BUILD_DURING_DEPLOYMENT: false
Expand Down Expand Up @@ -119,46 +116,15 @@ module accessKeyVault 'br/public:avm/res/key-vault/vault:0.5.1' = {
}

// The application database
module cosmos 'br/public:avm/res/document-db/database-account:0.6.0' = {
module cosmos '../../../../../common/infra/bicep/app/cosmos-sql-db-avm.bicep' = {
name: 'cosmos'
scope: rg
params: {
name: !empty(cosmosAccountName) ? cosmosAccountName : '${abbrs.documentDBDatabaseAccounts}${resourceToken}'
accountName: !empty(cosmosAccountName) ? cosmosAccountName : '${abbrs.documentDBDatabaseAccounts}${resourceToken}'
location: location
locations: [
{
failoverPriority: 0
locationName: location
isZoneRedundant: false
}
]
secretsExportConfiguration:{
keyVaultResourceId: keyVault.outputs.resourceId
primaryWriteConnectionStringSecretName: connectionStringKey
}
capabilitiesToAdd: [ 'EnableServerless' ]
automaticFailover: false
sqlDatabases: [
{
name: actualDatabaseName
containers: [
{
name: 'TodoList'
paths: [ 'id' ]
}
{
name: 'TodoItem'
paths: [ 'id' ]
}
]
}
]
sqlRoleAssignmentsPrincipalIds: [ principalId ]
sqlRoleDefinitions: [
{
name: 'writer'
}
]
tags: tags
keyVaultResourceId: keyVault.outputs.resourceId
principalId: principalId
}
}

Expand All @@ -167,7 +133,7 @@ module apiCosmosSqlRoleAssign 'br/public:avm/res/document-db/database-account:0.
name: 'api-cosmos-access'
scope: rg
params: {
name: cosmos.outputs.name
name: cosmos.outputs.accountName
location: location
sqlRoleAssignmentsPrincipalIds: [ api.outputs.SERVICE_API_IDENTITY_PRINCIPAL_ID ]
sqlRoleDefinitions: [
Expand Down Expand Up @@ -292,8 +258,8 @@ module apimApi 'br/public:avm/ptn/azd/apim-api:0.1.0' = {

// Data outputs
output AZURE_COSMOS_ENDPOINT string = cosmos.outputs.endpoint
output AZURE_COSMOS_CONNECTION_STRING_KEY string = connectionStringKey
output AZURE_COSMOS_DATABASE_NAME string = actualDatabaseName
output AZURE_COSMOS_CONNECTION_STRING_KEY string = cosmos.outputs.connectionStringKey
output AZURE_COSMOS_DATABASE_NAME string = cosmos.outputs.databaseName

// App outputs
output APPLICATIONINSIGHTS_CONNECTION_STRING string = applicationInsights.outputs.connectionString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ repo:
patterns:
- "**/main.bicep"

- from: cosmos-sql-db-avm.bicep
to: db-avm.bicep
patterns:
- "**/main.bicep"

- from: ../../../../api/common/openapi.yaml
to: ../../src/api/wwwroot/openapi.yaml
patterns:
Expand Down Expand Up @@ -105,6 +110,9 @@ repo:
- from: ../../../../common/infra/bicep/app/cosmos-sql-db.bicep
to: ./infra/app/db.bicep

- from: ../../../../common/infra/bicep/app/cosmos-sql-db-avm.bicep
to: ./infra/app/db-avm.bicep

- from: ../../../../common/infra/bicep/app/applicationinsights-dashboard.bicep
to: ./infra/app/applicationinsights-dashboard.bicep

Expand Down
Loading

0 comments on commit 3399e7f

Please sign in to comment.