-
Notifications
You must be signed in to change notification settings - Fork 22
Add Azure Bicep recipe for postgreSqlDatabases #157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
willtsai
wants to merge
13
commits into
main
Choose a base branch
from
willtsai/add-postgresql-azure-aws-recipes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 6 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
373aa21
Add Azure and AWS recipes for postgreSqlDatabases
willtsai f22774b
Remove AWS recipes, focus on Azure only
willtsai f2e0120
Remove Azure Terraform recipe, keep Bicep only
willtsai 9a869cb
Read credentials from connected secrets instead of params
willtsai e25bf54
Merge branch 'main' into willtsai/add-postgresql-azure-aws-recipes
willtsai c7fdf6a
Add initSql support to Azure PostgreSQL recipe
Copilot bf04897
Expose backup/HA settings as overridable parameters
Copilot 8786294
Merge branch 'main' into willtsai/add-postgresql-azure-aws-recipes
willtsai d0a185f
Fix ARM template validation error in recipe output
willtsai 102a658
Remove dependsOn from child resources to fix Radius ARM validation
willtsai 2073f05
Replace child resources with deployment script to fix Radius ARM vali…
willtsai aa18e06
Revert to parent: syntax following working Azure SQL recipe pattern
willtsai 863385e
working version reading k8s secrets
willtsai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
198 changes: 198 additions & 0 deletions
198
Data/postgreSqlDatabases/recipes/azure/bicep/azure-postgresql.bicep
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| @description('Information about what resource is calling this Recipe. Generated by Radius.') | ||
| param context object | ||
|
|
||
| @description('Location for the PostgreSQL Flexible Server. Defaults to the resource group location.') | ||
| param postgresqlLocation string = resourceGroup().location | ||
|
|
||
| @description('PostgreSQL major version.') | ||
| param postgresqlVersion string = '16' | ||
|
|
||
| @description('Storage size in GB.') | ||
| param storageSizeGb int = 32 | ||
|
|
||
| ////////////////////////////////////////// | ||
| // Common Radius variables | ||
| ////////////////////////////////////////// | ||
|
|
||
| var resourceName = context.resource.name | ||
| var applicationName = context.application != null ? context.application.name : '' | ||
| var environmentName = context.environment != null ? context.environment.name : '' | ||
|
|
||
| ////////////////////////////////////////// | ||
| // Secrets connection | ||
| ////////////////////////////////////////// | ||
|
|
||
| // The secretName property references a Radius.Security/secrets resource whose | ||
| // recipe provisions an Azure Key Vault and (optionally) a User-Assigned Identity. | ||
| // Radius resolves the connection and populates context.resource.connections with | ||
| // the secret resource's status, including computedValues and secrets. | ||
| // | ||
| // Radius wires connection data with full resource structure: | ||
| // context.resource.connections.<name>.properties.status.computedValues.<key> | ||
| // context.resource.connections.<name>.properties.status.secrets.<key>.Value | ||
|
|
||
| var secretName = context.resource.properties.secretName | ||
| var connections = context.resource.?connections ?? {} | ||
| #disable-next-line use-safe-access | ||
| var secretsConn = contains(connections, secretName) ? connections[secretName] : {} | ||
|
|
||
| var adminUsername = string(secretsConn.?properties.?status.?secrets.?USERNAME.?Value ?? '') | ||
| var adminPassword = string(secretsConn.?properties.?status.?secrets.?PASSWORD.?Value ?? '') | ||
|
|
||
| ////////////////////////////////////////// | ||
| // PostgreSQL variables | ||
| ////////////////////////////////////////// | ||
|
|
||
| var database = context.resource.properties.?database ?? 'postgres_db' | ||
| var sizeValue = context.resource.properties.?size ?? 'S' | ||
| var initSql = context.resource.properties.?initSql ?? '' | ||
| var hasInitSql = initSql != '' | ||
| var port = 5432 | ||
|
|
||
| var uniqueSuffix = substring(uniqueString(context.resource.id), 0, 13) | ||
| var serverName = 'psql-${uniqueSuffix}' | ||
|
|
||
| var skuMap = { | ||
| S: { | ||
| name: 'Standard_B1ms' | ||
| tier: 'Burstable' | ||
| } | ||
| M: { | ||
| name: 'Standard_D2s_v3' | ||
| tier: 'GeneralPurpose' | ||
| } | ||
| L: { | ||
| name: 'Standard_E2ds_v4' | ||
| tier: 'MemoryOptimized' | ||
| } | ||
| } | ||
|
|
||
| var tags = { | ||
| 'radapp.io-resource': resourceName | ||
| 'radapp.io-application': applicationName | ||
| 'radapp.io-environment': environmentName | ||
| } | ||
|
|
||
| ////////////////////////////////////////// | ||
| // PostgreSQL Flexible Server | ||
| ////////////////////////////////////////// | ||
|
|
||
| resource postgresServer 'Microsoft.DBforPostgreSQL/flexibleServers@2024-08-01' = { | ||
| name: serverName | ||
| location: postgresqlLocation | ||
| tags: tags | ||
| sku: { | ||
| name: skuMap[sizeValue].name | ||
| tier: skuMap[sizeValue].tier | ||
| } | ||
| properties: { | ||
| version: postgresqlVersion | ||
| administratorLogin: adminUsername | ||
| #disable-next-line use-secure-value-for-secure-inputs | ||
| administratorLoginPassword: adminPassword | ||
| storage: { | ||
| storageSizeGB: storageSizeGb | ||
| } | ||
| backup: { | ||
| backupRetentionDays: 7 | ||
| geoRedundantBackup: 'Disabled' | ||
| } | ||
| highAvailability: { | ||
| mode: 'Disabled' | ||
| } | ||
| } | ||
| } | ||
|
|
||
| resource firewallRule 'Microsoft.DBforPostgreSQL/flexibleServers/firewallRules@2024-08-01' = { | ||
| parent: postgresServer | ||
| name: 'AllowAllAzureServices' | ||
| properties: { | ||
| startIpAddress: '0.0.0.0' | ||
| endIpAddress: '0.0.0.0' | ||
| } | ||
| } | ||
|
|
||
| resource postgresDb 'Microsoft.DBforPostgreSQL/flexibleServers/databases@2024-08-01' = { | ||
| parent: postgresServer | ||
| name: database | ||
| properties: { | ||
| charset: 'UTF8' | ||
| collation: 'en_US.utf8' | ||
| } | ||
| } | ||
|
|
||
| ////////////////////////////////////////// | ||
| // Init SQL deployment script (optional) | ||
| ////////////////////////////////////////// | ||
|
|
||
| // When `initSql` is provided, run it against the newly created database using | ||
| // `psql` inside an Azure CLI deployment script container. This mirrors the | ||
| // Kubernetes recipe's `/docker-entrypoint-initdb.d/` behavior, executing the | ||
| // SQL once after the database is provisioned. The script depends on the | ||
| // firewall rule that allows Azure services so the container can reach the | ||
| // flexible server. | ||
| resource initSqlScript 'Microsoft.Resources/deploymentScripts@2023-08-01' = if (hasInitSql) { | ||
| name: 'init-sql-${uniqueSuffix}' | ||
| location: postgresqlLocation | ||
| tags: tags | ||
| kind: 'AzureCLI' | ||
| properties: { | ||
| azCliVersion: '2.60.0' | ||
| timeout: 'PT10M' | ||
| retentionInterval: 'PT1H' | ||
| cleanupPreference: 'OnSuccess' | ||
| environmentVariables: [ | ||
| { | ||
| name: 'PGHOST' | ||
| value: postgresServer.properties.fullyQualifiedDomainName | ||
| } | ||
| { | ||
| name: 'PGUSER' | ||
| value: adminUsername | ||
| } | ||
| { | ||
| name: 'PGPASSWORD' | ||
| secureValue: adminPassword | ||
| } | ||
| { | ||
| name: 'PGDATABASE' | ||
| value: postgresDb.name | ||
| } | ||
| { | ||
| name: 'PGPORT' | ||
| value: string(port) | ||
| } | ||
| { | ||
| name: 'PGSSLMODE' | ||
| value: 'require' | ||
| } | ||
| { | ||
| name: 'INIT_SQL' | ||
| secureValue: initSql | ||
| } | ||
| ] | ||
| scriptContent: ''' | ||
| set -euo pipefail | ||
| apk add --no-cache postgresql-client >/dev/null | ||
| printf '%s' "$INIT_SQL" | psql --quiet -v ON_ERROR_STOP=1 -f - | ||
| ''' | ||
| } | ||
| dependsOn: [ | ||
| firewallRule | ||
| ] | ||
| } | ||
|
|
||
| ////////////////////////////////////////// | ||
| // Output Radius result | ||
| ////////////////////////////////////////// | ||
|
|
||
| output result object = { | ||
| resources: [ | ||
| postgresServer.id | ||
| ] | ||
| values: { | ||
| host: postgresServer.properties.fullyQualifiedDomainName | ||
| port: port | ||
| database: postgresDb.name | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.