diff --git a/Data/mySqlDatabases/README.md b/Data/mySqlDatabases/README.md index 409ecaa5..30ce7c93 100644 --- a/Data/mySqlDatabases/README.md +++ b/Data/mySqlDatabases/README.md @@ -13,6 +13,7 @@ A list of available Recipes for this resource type, including links to the Bicep |Platform| IaC Language| Recipe Name | Stage | |---|---|---|---| | Kubernetes | Bicep | kubernetes-mysql.bicep | Alpha | +| Azure | Bicep | azure-database-for-mysql.bicep | Alpha | ## Recipe Input Properties @@ -21,6 +22,7 @@ Properties for the **Radius.Data/mySqlDatabases** resource type are provided via - `context.properties.database`(string, optional): The name of the database. Defaults to the `application-name` if not provided. - `context.properties.username`(string, optional): The username for connecting to the database. Defaults to the `application-name-user` if not provided. - `context.properties.version`(string, optional): The major MySQL server version in the X.Y format. Defaults to the version `8.4` if not provided. +- `context.properties.tags`(object, optional): The user-defined tags that will be applied to the resource. Default is null. ## Recipe Output Properties diff --git a/Data/mySqlDatabases/mySqlDatabases.yaml b/Data/mySqlDatabases/mySqlDatabases.yaml index b0c807fe..b042664c 100644 --- a/Data/mySqlDatabases/mySqlDatabases.yaml +++ b/Data/mySqlDatabases/mySqlDatabases.yaml @@ -2,52 +2,58 @@ namespace: Radius.Data types: mySqlDatabases: description: | - The Radius.Data/mySqlDatabases Resource Type deploys a MySQL database. To deploy a new MySQL database, add the mySqlDatabases resource to the application definition Bicep file. + # Radius.Data/mySqlDatabases + The **Radius.Data/mySqlDatabases** Resource Type deploys a MySQL database. To deploy a new MySQL database, add the mySqlDatabases resource to the application definition Bicep file: + + ```bicep extension radius - param environment string + param environment string resource myApplication 'Applications.Core/applications@2023-10-01-preview' = { ... } resource database 'Radius.Data/mySqlDatabases@2025-08-01-preview' = { - name: 'database' - properties: { - application: myApplication.id - environment: environment - } + name: 'database' + properties: { + application: myApplication.id + environment: environment + } } + ``` - To connect your container to the database, create a connection from the Container resource to the database as shown below. + To connect your container to the database, create a connection from the Container resource to the database as shown below: + ```bicep resource frontend 'Applications.Core/containers@2023-10-01-preview' = { - name: 'frontend' - properties: { - application: myApplication.id - environment: environment - container: { - image: 'frontend:1.25' - ports: { - web: { - containerPort: 8080 + name: 'frontend' + properties: { + application: myApplication.id + environment: environment + container: { + image: 'frontend:1.25' + ports: { + web: { + containerPort: 8080 + } + } + } + connections: { + mysqldb: { + source: database.id + } } - } - } - connections: { - mysqldb: { - source: database.id - } } - } } + ``` The connection automatically injects environment variables into the container for all properties from the database. The environment variables are named `CONNECTION__`. In this example, the connection name is `mysqldb` so the environment variables will be: - - CONNECTION_MYSQLDB_DATABASE - CONNECTION_MYSQLDB_USERNAME - CONNECTION_MYSQLDB_PASSWORD - CONNECTION_MYSQLDB_VERSION - CONNECTION_MYSQLDB_HOST - CONNECTION_MYSQLDB_PORT + + * `CONNECTION_MYSQLDB_DATABASE` + * `CONNECTION_MYSQLDB_USERNAME` + * `CONNECTION_MYSQLDB_PASSWORD` + * `CONNECTION_MYSQLDB_VERSION` + * `CONNECTION_MYSQLDB_HOST` + * `CONNECTION_MYSQLDB_PORT` apiVersions: '2025-08-01-preview': @@ -62,14 +68,24 @@ types: description: "(Optional) The Radius Application ID. `myApplication.id` for example." database: type: string - description: "(Optional) The name of the database." + description: "(Optional) The name of the database. Assumed to be `myApplication.name` if not specified." username: type: string - description: "(Optional) The username for connecting to the database." + description: "(Optional) The username for connecting to the database. Assumed to be `_user` if not specified." version: type: string enum: ['5.7', '8.0', '8.4'] - description: "(Optional) The major MySQL server version in the X.Y format. Assumed to be 8.4 if not specified." + description: "(Optional) The major MySQL server version in the X.Y format. Assumed to be 8.4 if not specified." + tags: + type: object + description: "(Optional) The user-defined tags that will be applied to the resource. Default is null." + additionalProperties: + type: string + description: "(Optional) Tag name." + properties: + value: + type: string + description: "(Optional) Tag value." password: type: string description: "(Read-only) The password for connecting to the database." diff --git a/Data/mySqlDatabases/recipes/azure-database-for-mysql/bicep/azure-database-for-mysql.bicep b/Data/mySqlDatabases/recipes/azure-database-for-mysql/bicep/azure-database-for-mysql.bicep new file mode 100644 index 00000000..48746610 --- /dev/null +++ b/Data/mySqlDatabases/recipes/azure-database-for-mysql/bicep/azure-database-for-mysql.bicep @@ -0,0 +1,85 @@ +@description('Information about what resource is calling this Recipe. Generated by Radius. For more information visit https://docs.radapp.io/reference/context-schema/ ') +param context object + +@description('Name of the MySQL database. Defaults to the application name.') +param database string = context.resource.properties.?database ?? '${context.application.name}' + +@maxLength(32) +@description('MySQL username. Defaults to _user') +param username string = context.resource.properties.?username ?? '${context.application.name}_user' + +@description('The major MySQL server version in the X.Y format. Defaults to the version 8.4 if not provided.') +@allowed([ + '5.7' + '8.0' + '8.4' +]) +param version string = context.resource.properties.?version ?? '8.4' + +@description('The user-defined tags that will be applied to the resource. Default is null.') +param tags object = context.resource.properties.?tags ?? {} + +@description('The Radius specific tags that will be applied to the resource') +var radiusTags = { + 'radapp.io-environment': context.environment.id + 'radapp.io-application': context.application == null ? '' : context.application.id + 'radapp.io-resource': context.resource.id +} + +@description('Location to deploy the resources') +var location string = resourceGroup().location + +@description('Unique name for the MySQL deployment and service.') +var uniqueName = 'mysql-${uniqueString(context.resource.id, resourceGroup().id)}' + +@description('The port the MySQL server listens on.') +var port = 3306 + +@description('MySQL server root password.') +@secure() +param root_password string = uniqueString(context.resource.id, newGuid()) + +resource mysqlServer 'Microsoft.DBforMySQL/flexibleServers@2024-12-30' = { + name: uniqueName + location: location + tags: union(tags, radiusTags) + sku: { + name: 'Standard_B1ms' + tier: 'Burstable' + } + properties: { + createMode: 'Default' + version: (version == '8.0') ? '8.0.21' : version + administratorLogin: username + administratorLoginPassword: root_password + databasePort: port + storage: { + storageSizeGB: 32 + } + network: { + publicNetworkAccess: 'Enabled' + } + } + + resource mysqlDatabase 'databases' = { + name: database + properties: { + charset: 'utf8' + collation: 'utf8_general_ci' + } + } +} + + +output result object = { + values: { + host: mysqlServer.properties.fullyQualifiedDomainName + port: port + database: database + username: username + } + secrets: { + #disable-next-line outputs-should-not-contain-secrets + password: root_password + } +} diff --git a/Data/mySqlDatabases/recipes/kubernetes/bicep/kubernetes-mysql.bicep b/Data/mySqlDatabases/recipes/kubernetes/bicep/kubernetes-mysql.bicep index 684b8e1c..4b15c4d9 100644 --- a/Data/mySqlDatabases/recipes/kubernetes/bicep/kubernetes-mysql.bicep +++ b/Data/mySqlDatabases/recipes/kubernetes/bicep/kubernetes-mysql.bicep @@ -9,8 +9,9 @@ extension kubernetes with { @description('Name of the MySQL database. Defaults to the application name.') param database string = context.resource.properties.?database ?? '${context.application.name}' -@description('MySQL username. Defaults to -user') -param username string = context.resource.properties.?username ?? '${context.application.name}-user' +@description('MySQL username. Defaults to _user') +@maxLength(32) +param username string = context.resource.properties.?username ?? '${context.application.name}_user' @description('The major MySQL server version in the X.Y format. Defaults to the version 8.4 if not provided.') @allowed([ @@ -20,6 +21,9 @@ param username string = context.resource.properties.?username ?? '${context.appl ]) param version string = context.resource.properties.?version ?? '8.4' +@description('The user-defined tags that will be applied to the resource. Default is null.') +param tags object = context.resource.properties.?tags ?? {} + @description('Unique name for the MySQL deployment and service.') var uniqueName = 'mysql-${uniqueString(context.resource.id)}' @@ -38,6 +42,7 @@ var root_password string = uniqueString(context.resource.id, guid(uniqueName, 'r resource mySql 'apps/Deployment@v1' = { metadata: { name: uniqueName + labels: tags } spec: { selector: {