Skip to content

Commit 185efe1

Browse files
authored
Merge pull request #41 from Fr4nc3/bicepdefaults
fix: Create Pgsql Database and run the scripts to create the tables and add extensions
2 parents 7c38f12 + 7fc608d commit 185efe1

File tree

12 files changed

+967
-382
lines changed

12 files changed

+967
-382
lines changed

code/tests/functional/app_config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
logger = logging.getLogger(__name__)
88
encoded_account_key = str(base64.b64encode(b"some-blob-account-key"), "utf-8")
99

10+
1011
class AppConfig:
1112
before_config: dict[str, str] = {}
1213
config: dict[str, str | None] = {

infra/app/storekeys.bicep

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ resource postgresInfoSecret 'Microsoft.KeyVault/vaults/secrets@2022-07-01' = if
110110
? string({
111111
user: postgresDatabaseAdminUserName
112112
dbname: postgresDatabaseName
113-
host: '${postgresServerName}.postgres.database.azure.com'
113+
host: postgresServerName
114114
password: postgresDatabaseAdminPassword
115115
})
116116
: ''
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
@description('Specifies the location for resources.')
2+
param solutionLocation string
3+
4+
param baseUrl string
5+
param keyVaultName string
6+
param identity string
7+
param postgresSqlServerName string
8+
9+
resource create_index 'Microsoft.Resources/deploymentScripts@2020-10-01' = {
10+
kind:'AzureCLI'
11+
name: 'create_postgres_table'
12+
location: solutionLocation // Replace with your desired location
13+
identity: {
14+
type: 'UserAssigned'
15+
userAssignedIdentities: {
16+
'${identity}' : {}
17+
}
18+
}
19+
properties: {
20+
azCliVersion: '2.52.0'
21+
primaryScriptUri: '${baseUrl}scripts/run_create_table_script.sh'
22+
arguments: '${baseUrl} ${keyVaultName} ${resourceGroup().name} ${postgresSqlServerName}' // Specify any arguments for the script
23+
timeout: 'PT1H' // Specify the desired timeout duration
24+
retentionInterval: 'PT1H' // Specify the desired retention interval
25+
cleanupPreference:'OnSuccess'
26+
}
27+
}

infra/core/database/postgresdb.bicep

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
param solutionName string
22
param solutionLocation string
3+
param managedIdentityObjectId string
4+
param managedIdentityObjectName string
35
@description('The name of the SQL logical server.')
46
param serverName string = '${solutionName}-postgres'
57

@@ -35,7 +37,11 @@ resource serverName_resource 'Microsoft.DBforPostgreSQL/flexibleServers@2023-12-
3537
version: version
3638
administratorLogin: administratorLogin
3739
administratorLoginPassword: administratorLoginPassword
38-
40+
authConfig: {
41+
tenantId: subscription().tenantId
42+
activeDirectoryAuth: 'Enabled'
43+
passwordAuth: 'Enabled'
44+
}
3945
highAvailability: {
4046
mode: 'Disabled'
4147
}
@@ -53,6 +59,34 @@ resource serverName_resource 'Microsoft.DBforPostgreSQL/flexibleServers@2023-12-
5359
}
5460
}
5561

62+
resource delayScript 'Microsoft.Resources/deploymentScripts@2020-10-01' = {
63+
name: 'waitForServerReady'
64+
location: resourceGroup().location
65+
kind: 'AzurePowerShell'
66+
properties: {
67+
azPowerShellVersion: '3.0'
68+
scriptContent: 'start-sleep -Seconds 180'
69+
cleanupPreference: 'Always'
70+
retentionInterval: 'PT1H'
71+
}
72+
dependsOn: [
73+
serverName_resource
74+
]
75+
}
76+
77+
resource azureADAdministrator 'Microsoft.DBforPostgreSQL/flexibleServers/administrators@2022-12-01' = {
78+
parent: serverName_resource
79+
name: managedIdentityObjectId
80+
properties: {
81+
principalType: 'SERVICEPRINCIPAL'
82+
principalName: managedIdentityObjectName
83+
tenantId: subscription().tenantId
84+
}
85+
dependsOn: [
86+
delayScript
87+
]
88+
}
89+
5690
// resource serverName_firewallrules 'Microsoft.DBforPostgreSQL/flexibleServers/firewallRules@2021-06-01' = [for rule in firewallrules: {
5791
// parent: serverName_resource
5892
// name: rule.Name
@@ -71,7 +105,7 @@ resource firewall_all 'Microsoft.DBforPostgreSQL/flexibleServers/firewallRules@2
71105
endIpAddress: '255.255.255.255'
72106
}
73107
dependsOn: [
74-
serverName_resource
108+
delayScript
75109
]
76110
}
77111

@@ -83,15 +117,15 @@ resource firewall_azure 'Microsoft.DBforPostgreSQL/flexibleServers/firewallRules
83117
endIpAddress: '0.0.0.0'
84118
}
85119
dependsOn: [
86-
firewall_all
120+
delayScript
87121
]
88122
}
89123

90124
resource configurations 'Microsoft.DBforPostgreSQL/flexibleServers/configurations@2023-12-01-preview' = {
91125
name: 'azure.extensions'
92126
parent: serverName_resource
93127
properties: {
94-
value: 'vector'
128+
value: 'pg_diskann'
95129
source: 'user-override'
96130
}
97131
dependsOn: [
@@ -102,7 +136,7 @@ resource configurations 'Microsoft.DBforPostgreSQL/flexibleServers/configuration
102136

103137
output postgresDbOutput object = {
104138
postgresSQLName: serverName_resource.name
105-
postgreSQLServerName: serverName_resource.name
139+
postgreSQLServerName: '${serverName_resource.name}.postgres.database.azure.com'
106140
postgreSQLDatabaseName: 'postgres'
107141
postgreSQLDbUser: administratorLogin
108142
postgreSQLDbPwd: administratorLoginPassword

infra/core/security/keyvault.bicep

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ metadata description = 'Creates an Azure Key Vault.'
22
param name string
33
param location string = resourceGroup().location
44
param tags object = {}
5+
param managedIdentityObjectId string
56

67
param principalId string = ''
78

@@ -12,18 +13,43 @@ resource keyVault 'Microsoft.KeyVault/vaults@2022-07-01' = {
1213
properties: {
1314
tenantId: subscription().tenantId
1415
sku: { family: 'A', name: 'standard' }
15-
accessPolicies: !empty(principalId)
16-
? [
17-
{
18-
objectId: principalId
19-
permissions: { secrets: [ 'get', 'list' ] }
20-
tenantId: subscription().tenantId
21-
}
22-
]
23-
: []
16+
accessPolicies: !empty(principalId)
17+
? [
18+
{
19+
objectId: principalId
20+
permissions: { secrets: [ 'get', 'list' ] }
21+
tenantId: subscription().tenantId
22+
}, {
23+
objectId: managedIdentityObjectId
24+
permissions: { secrets: [ 'get', 'list' ] }
25+
tenantId: subscription().tenantId
26+
}
27+
]
28+
: [
29+
{
30+
objectId: managedIdentityObjectId
31+
permissions: { secrets: [ 'get', 'list' ] }
32+
tenantId: subscription().tenantId
33+
}
34+
]
2435
}
2536
}
2637

38+
// @description('This is the built-in Key Vault Administrator role.')
39+
// resource kvAdminRole 'Microsoft.Authorization/roleDefinitions@2018-01-01-preview' existing = {
40+
// scope: resourceGroup()
41+
// name: '00482a5a-887f-4fb3-b363-3b7fe8e74483'
42+
// }
43+
44+
// resource roleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
45+
// name: guid(resourceGroup().id, managedIdentityObjectId, kvAdminRole.id)
46+
// properties: {
47+
// principalId: managedIdentityObjectId
48+
// roleDefinitionId:kvAdminRole.id
49+
// principalType: 'ServicePrincipal'
50+
// }
51+
// }
52+
2753
output endpoint string = keyVault.properties.vaultUri
2854
output name string = keyVault.name
29-
output id string = keyVault.id
55+
output id string = keyVault.id
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// ========== Managed Identity ========== //
2+
targetScope = 'resourceGroup'
3+
4+
@minLength(3)
5+
@maxLength(15)
6+
@description('Solution Name')
7+
param solutionName string
8+
9+
@description('Solution Location')
10+
param solutionLocation string
11+
12+
@description('Name')
13+
param miName string = '${ solutionName }-managed-identity'
14+
15+
resource managedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' = {
16+
name: miName
17+
location: solutionLocation
18+
tags: {
19+
app: solutionName
20+
location: solutionLocation
21+
}
22+
}
23+
24+
@description('This is the built-in owner role. See https://docs.microsoft.com/azure/role-based-access-control/built-in-roles#owner')
25+
resource ownerRoleDefinition 'Microsoft.Authorization/roleDefinitions@2018-01-01-preview' existing = {
26+
scope: resourceGroup()
27+
name: '8e3af657-a8ff-443c-a75c-2fe8c4bcb635'
28+
}
29+
30+
resource roleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
31+
name: guid(resourceGroup().id, managedIdentity.id, ownerRoleDefinition.id)
32+
properties: {
33+
principalId: managedIdentity.properties.principalId
34+
roleDefinitionId: ownerRoleDefinition.id
35+
principalType: 'ServicePrincipal'
36+
}
37+
}
38+
39+
output managedIdentityOutput object = {
40+
id: managedIdentity.id
41+
objectId: managedIdentity.properties.principalId
42+
name: miName
43+
}

infra/main.bicep

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ param azureOpenAIVisionModelCapacity int = 10
148148
'langchain'
149149
'prompt_flow'
150150
])
151-
param orchestrationStrategy string = 'openai_function'
151+
param orchestrationStrategy string = 'semantic_kernel'
152152

153153
@description('Chat conversation type: custom or byod.')
154154
@allowed([
@@ -315,20 +315,14 @@ param azureCosmosDBAccountName string = 'cosmos-${resourceToken}'
315315
@description('Azure Postgres DB Account Name')
316316
param azurePostgresDBAccountName string = 'postgres-${resourceToken}'
317317

318-
@description('Whether or not to enable chat history')
319-
@allowed([
320-
'true'
321-
'false'
322-
])
323-
param chatHistoryEnabled string = 'true'
324-
325318
var blobContainerName = 'documents'
326319
var queueName = 'doc-processing'
327320
var clientKey = '${uniqueString(guid(subscription().id, deployment().name))}${newGuidString}'
328321
var eventGridSystemTopicName = 'doc-processing'
329322
var tags = { 'azd-env-name': environmentName }
330323
var rgName = 'rg-${environmentName}'
331324
var keyVaultName = 'kv-${resourceToken}'
325+
var baseUrl = 'https://raw.githubusercontent.com/Fr4nc3/chat-with-your-data-solution-accelerator/bicepdefaults/'
332326
var azureOpenAIModelInfo = string({
333327
model: azureOpenAIModel
334328
modelName: azureOpenAIModelName
@@ -350,6 +344,16 @@ resource rg 'Microsoft.Resources/resourceGroups@2021-04-01' = {
350344
tags: tags
351345
}
352346

347+
// ========== Managed Identity ========== //
348+
module managedIdentityModule './core/security/managed-identity.bicep' = if (databaseType == 'postgres') {
349+
name: 'deploy_managed_identity'
350+
params: {
351+
solutionName: resourceToken
352+
solutionLocation: location
353+
}
354+
scope: rg
355+
}
356+
353357
module cosmosDBModule './core/database/cosmosdb.bicep' = if (databaseType == 'cosmos') {
354358
name: 'deploy_cosmos_db'
355359
params: {
@@ -364,8 +368,10 @@ module postgresDBModule './core/database/postgresdb.bicep' = if (databaseType ==
364368
params: {
365369
solutionName: azurePostgresDBAccountName
366370
solutionLocation: 'eastus2'
371+
managedIdentityObjectId: managedIdentityModule.outputs.managedIdentityOutput.objectId
372+
managedIdentityObjectName: managedIdentityModule.outputs.managedIdentityOutput.name
367373
}
368-
scope: resourceGroup(resourceGroup().name)
374+
scope: rg
369375
}
370376

371377
// Store secrets in a keyvault
@@ -377,6 +383,7 @@ module keyvault './core/security/keyvault.bicep' = if (useKeyVault || authType =
377383
location: location
378384
tags: tags
379385
principalId: principalId
386+
managedIdentityObjectId: managedIdentityModule.outputs.managedIdentityOutput.objectId
380387
}
381388
}
382389

@@ -764,7 +771,6 @@ module web_docker './app/web.bicep' = if (hostingModel == 'container') {
764771
ORCHESTRATION_STRATEGY: orchestrationStrategy
765772
CONVERSATION_FLOW: conversationFlow
766773
LOGLEVEL: logLevel
767-
CHAT_HISTORY_ENABLED: chatHistoryEnabled
768774

769775
// Add database type to settings
770776
AZURE_DATABASE_TYPE: databaseType
@@ -849,7 +855,6 @@ module adminweb './app/adminweb.bicep' = if (hostingModel == 'code') {
849855
FUNCTION_KEY: clientKey
850856
ORCHESTRATION_STRATEGY: orchestrationStrategy
851857
LOGLEVEL: logLevel
852-
CHAT_HISTORY_ENABLED: chatHistoryEnabled
853858
}
854859
}
855860
}
@@ -923,7 +928,6 @@ module adminweb_docker './app/adminweb.bicep' = if (hostingModel == 'container')
923928
FUNCTION_KEY: clientKey
924929
ORCHESTRATION_STRATEGY: orchestrationStrategy
925930
LOGLEVEL: logLevel
926-
CHAT_HISTORY_ENABLED: chatHistoryEnabled
927931
}
928932
}
929933
}
@@ -1226,6 +1230,19 @@ module machineLearning 'app/machinelearning.bicep' = if (orchestrationStrategy =
12261230
}
12271231
}
12281232

1233+
module createIndex './core/database/deploy_create_table_script.bicep' = if (databaseType == 'postgres') {
1234+
name : 'deploy_create_table_script'
1235+
params:{
1236+
solutionLocation: location
1237+
identity:managedIdentityModule.outputs.managedIdentityOutput.id
1238+
baseUrl:baseUrl
1239+
keyVaultName:keyvault.outputs.name
1240+
postgresSqlServerName: postgresDBModule.outputs.postgresDbOutput.postgresSQLName
1241+
}
1242+
scope: rg
1243+
dependsOn:[keyvault, postgresDBModule, storekeys]
1244+
}
1245+
12291246
output APPLICATIONINSIGHTS_CONNECTION_STRING string = monitoring.outputs.applicationInsightsConnectionString
12301247
output AZURE_APP_SERVICE_HOSTING_MODEL string = hostingModel
12311248
output AZURE_BLOB_STORAGE_INFO string = replace(azureBlobStorageInfo, '$STORAGE_ACCOUNT_KEY','')

infra/main.bicepparam

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ param orchestrationStrategy = readEnvironmentVariable('ORCHESTRATION_STRATEGY',
1919
param logLevel = readEnvironmentVariable('LOGLEVEL', 'INFO')
2020
param recognizedLanguages = readEnvironmentVariable('AZURE_SPEECH_RECOGNIZER_LANGUAGES', 'en-US,fr-FR,de-DE,it-IT')
2121
param conversationFlow = readEnvironmentVariable('CONVERSATION_FLOW', 'custom')
22-
param chatHistoryEnabled = readEnvironmentVariable('CHAT_HISTORY_ENABLED', 'true')
2322

2423
//Azure Search
2524
param azureSearchFieldId = readEnvironmentVariable('AZURE_SEARCH_FIELDS_ID', 'id')

0 commit comments

Comments
 (0)