Skip to content

Instantly share code, notes, and snippets.

@nayanshah
Created April 24, 2024 06:59
Show Gist options
  • Save nayanshah/7c86cfe6c9882c9e8133cee6ce17c9bb to your computer and use it in GitHub Desktop.
Save nayanshah/7c86cfe6c9882c9e8133cee6ce17c9bb to your computer and use it in GitHub Desktop.
Auto-generated bicep file to read all config files in a directory

Bicep doesn't support loading all config files in a directory even if all filenames are hardcoded in a constant array.

A workaround for this is would be to generate a .bicep file that calls loadTextContent multiple times instead of in a loop. This can be done using T4 Text Templates

T4.Build nuget package contains MSBuild logic to generate the .bicep file and can be referenced in a .csproj that compiles the bicep files.

		<PackageReference Include="T4.Build" Version="0.2.4" PrivateAssets="All" />

Note: this works on Windows only

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
<#@ output extension=".bicep" #>
///
/// DO NOT EDIT Alerts.bicep
/// Autogenerated from Alerts.tt during the build
///
param location string
param appInsightsId string
<#
var alertDefs = Directory.EnumerateFiles(this.Host.ResolvePath("."), "*.json")
.Select(f => Path.GetFileNameWithoutExtension(f));
#>
// Load all alerts defined in ./alerts folder
var alertDefinitions = [
<# foreach (var name in alertDefs) { #>
{
definition: loadTextContent('./alerts/<#= name #>.json')
}
<# } #>
]
module monitorAlerts './modules/CreateAlert.bicep' = [for alert in alertDefinitions: {
name: 'alert-${alert.definition.name}'
params: {
location: location
appInsightsId: appInsightsId
alertDefinition: alert.definition
}
}]
{
"name": "sample",
"severity": 3,
"enabled": true,
"evaluationFrequency": "PT10M",
"windowSize": "PT30M",
"criteria": {
"timeAggregation": "Count",
"dimensions": [],
"operator": "GreaterThan",
"threshold": 10,
"failingPeriods": {
"numberOfEvaluationPeriods": 1,
"minFailingPeriodsToAlert": 1
}
},
"autoMitigate": true,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment