Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ReAdd: readEnvironmentVariable function with default param (Azure#10849)
This PR re-creates a new Bicep Function 'readEnviromentVariable' to enable users to read environment variables to set bicep variables values as earlier merged pr Azure#10726 got reverted. This Feature in only enabled on the .BicepParams Files. ## Contributing a feature * [X] I have opened a new issue for the proposal, or commented on an existing one, and ensured that the Bicep maintainers are good with the design of the feature being implemented. Addresses setting default values for #Azure#9576 * [X] I have included "Fixes #{issue_number}" in the PR description, so GitHub can link to the issue and close it when the PR is merged * [X] I have appropriate test coverage of my new feature This PR extends the LoadEnvironmentVariable fucntion available for .bicepparam files to have an optional argument to set a default value to use if the environment variable did not exist. ### Signature **function readEnvironmentVariable(variableName: string, [default: string]): string** Reads the specified Environment variable as bicep string. Variable loading occurs during compilation, not at runtime. user can set a default string value to be returned/used if the environment variable does not exist. ### Syntax examples ```bicep // read string value param stringfromEnvironmentVariables = readEnvironmentVariable('stringVariableName') // read int value param intfromEnvironmentVariables = int(readEnvironmentVariable('intVariableName')) //read a boolen value, and set default value to false if env variable not found param boolfromEnvironmentVariables = bool(readEnvironmentVariable('boolVariableName','false')) ``` ### Code samples min.bicep ``` bicep @secure() param administratorLoginPassword string // SQL SERVER // https://docs.microsoft.com/en-us/azure/templates/microsoft.sql/servers?tabs=bicep resource sqlServer 'Microsoft.Sql/servers@2021-11-01' = { name: name location: location properties: { administratorLogin: administratorLogin administratorLoginPassword: administratorLoginPassword minimalTlsVersion: '1.2' version: '12.0' } tags:{ GeneratedBy: 'Bicep_test' Units: string(counter) } } // https://docs.microsoft.com/en-us/azure/templates/microsoft.sql/servers/firewallrules?tabs=bicep resource sqlFirewallRuleAzure 'Microsoft.Sql/servers/firewallRules@2021-11-01-preview' = { parent: sqlServer name: 'AzureAccess' properties: { endIpAddress: '0.0.0.0' startIpAddress: '0.0.0.0' } } output id string = sqlServer.id output name string = sqlServer.name output fqdn string = sqlServer.properties.fullyQualifiedDomainName ``` params.bicepparam ```bicep using './main.bicep' param name='mydb' param location=toUpper('westus') param administratorLogin=readEnvironmentVariable('AdminLogin') param administratorLoginPassword= readEnvironmentVariable('AdminPassword') param counter=int(readEnvironmentVariable('Counter','1')) ``` Set Env var values **AdminPassword=ABC , AdminLogin=user **. then execute bicep build-param ```bash bicep build-params, ./params.bicepparam --outfile ./generatedparam.json --bicep-file./main.bicep ``` Expected behavior: output file generatedparam.json generated successfully with values loaded form AdminPassword, and AdminLogin values environment variables, while Counter value set from the default vaue to 1 ```json { "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#", "contentVersion": "1.0.0.0", "parameters": { "name": { "value": "mydb" }, "location": { "value": "WESTUS" }, "administratorLogin": { "value": "user" }, "administratorLoginPassword": { "value": "ABC" }, "counter": { "value": 1 } } } ``` Set Env var values **AdminPassword=ABC , AdminLogin=user , Counter=3**. then execute bicep build-param ```bash bicep build-params, ./params.bicepparam --outfile ./generatedparam.json --bicep-file./main.bicep ``` Expected behavior: output file generatedparam.json generated successfully with values loaded form evironment variables. ```json { "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#", "contentVersion": "1.0.0.0", "parameters": { "name": { "value": "mydb" }, "location": { "value": "WESTUS" }, "administratorLogin": { "value": "user" }, "administratorLoginPassword": { "value": "ABC" }, "counter": { "value": 3 } } } ``` Unset (AdminPassword , AdminLogin) environment Variables, then execute bicep build-param : Expected behavoiur: Build-param fail to generate the json output with clear Error message ![image](https://github.com/Azure/bicep/assets/34171319/8f592365-9bb4-407d-91da-f687bfd9e9ff) --------- Co-authored-by: Hattan Shobokshi <[email protected]>
- Loading branch information