diff --git a/.github/workflows/ms.insights.scheduledqueryrules.yml b/.github/workflows/ms.insights.scheduledqueryrules.yml index f08f10e600..a539560e17 100644 --- a/.github/workflows/ms.insights.scheduledqueryrules.yml +++ b/.github/workflows/ms.insights.scheduledqueryrules.yml @@ -106,8 +106,7 @@ jobs: - name: 'Using test file [${{ matrix.moduleTestFilePaths }}]' uses: ./.github/actions/templates/validateModuleDeployment with: - templateFilePath: '${{ env.modulePath }}/deploy.bicep' - parameterFilePath: '${{ env.modulePath }}/${{ matrix.moduleTestFilePaths }}' + templateFilePath: '${{ env.modulePath }}/${{ matrix.moduleTestFilePaths }}' location: '${{ env.location }}' resourceGroupName: '${{ env.resourceGroupName }}' subscriptionId: '${{ secrets.ARM_SUBSCRIPTION_ID }}' diff --git a/modules/Microsoft.Insights/scheduledQueryRules/.test/common/dependencies.bicep b/modules/Microsoft.Insights/scheduledQueryRules/.test/common/dependencies.bicep new file mode 100644 index 0000000000..9e9a8f2510 --- /dev/null +++ b/modules/Microsoft.Insights/scheduledQueryRules/.test/common/dependencies.bicep @@ -0,0 +1,24 @@ +@description('Optional. The location to deploy to.') +param location string = resourceGroup().location + +@description('Required. The name of the Managed Identity to create.') +param managedIdentityName string + +@description('Required. The name of the Log Analytics Workspace to create.') +param logAnalyticsWorkspaceName string + +resource managedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2018-11-30' = { + name: managedIdentityName + location: location +} + +resource logAnalyticsWorkspace 'Microsoft.OperationalInsights/workspaces@2021-06-01' = { + name: logAnalyticsWorkspaceName + location: location +} + +@description('The principal ID of the created Managed Identity.') +output managedIdentityPrincipalId string = managedIdentity.properties.principalId + +@description('The resource ID of the created Log Analytics Workspace.') +output logAnalyticsWorkspaceResourceId string = logAnalyticsWorkspace.id diff --git a/modules/Microsoft.Insights/scheduledQueryRules/.test/common/deploy.test.bicep b/modules/Microsoft.Insights/scheduledQueryRules/.test/common/deploy.test.bicep new file mode 100644 index 0000000000..7a42c660c9 --- /dev/null +++ b/modules/Microsoft.Insights/scheduledQueryRules/.test/common/deploy.test.bicep @@ -0,0 +1,90 @@ +targetScope = 'subscription' + +// ========== // +// Parameters // +// ========== // +@description('Optional. The name of the resource group to deploy for testing purposes.') +@maxLength(90) +param resourceGroupName string = 'ms.insights.scheduledqueryrules-${serviceShort}-rg' + +@description('Optional. The location to deploy resources to.') +param location string = deployment().location + +@description('Optional. A short identifier for the kind of deployment. Should be kept short to not run into resource-name length-constraints.') +param serviceShort string = 'isqrcom' + +// =========== // +// Deployments // +// =========== // + +// General resources +// ================= +resource resourceGroup 'Microsoft.Resources/resourceGroups@2021-04-01' = { + name: resourceGroupName + location: location +} + +module resourceGroupResources 'dependencies.bicep' = { + scope: resourceGroup + name: '${uniqueString(deployment().name, location)}-paramNested' + params: { + managedIdentityName: 'dep-<>-msi-${serviceShort}' + logAnalyticsWorkspaceName: 'dep-<>-law-${serviceShort}' + } +} + +// ============== // +// Test Execution // +// ============== // + +module testDeployment '../../deploy.bicep' = { + scope: resourceGroup + name: '${uniqueString(deployment().name)}-test-${serviceShort}' + params: { + name: '<>${serviceShort}001' + alertDescription: 'My sample Alert' + autoMitigate: false + criterias: { + allOf: [ + { + dimensions: [ + { + name: 'Computer' + operator: 'Include' + values: [ + '*' + ] + } + { + name: 'InstanceName' + operator: 'Include' + values: [ + '*' + ] + } + ] + metricMeasureColumn: 'AggregatedValue' + operator: 'GreaterThan' + query: 'Perf | where ObjectName == "LogicalDisk" | where CounterName == "% Free Space" | where InstanceName <> "HarddiskVolume1" and InstanceName <> "_Total" | summarize AggregatedValue = min(CounterValue) by Computer, InstanceName, bin(TimeGenerated,5m)' + threshold: 0 + timeAggregation: 'Average' + } + ] + } + evaluationFrequency: 'PT5M' + queryTimeRange: 'PT5M' + roleAssignments: [ + { + principalIds: [ + resourceGroupResources.outputs.managedIdentityPrincipalId + ] + roleDefinitionIdOrName: 'Reader' + } + ] + scopes: [ + resourceGroupResources.outputs.logAnalyticsWorkspaceResourceId + ] + suppressForMinutes: 'PT5M' + windowSize: 'PT5M' + } +} diff --git a/modules/Microsoft.Insights/scheduledQueryRules/.test/parameters.json b/modules/Microsoft.Insights/scheduledQueryRules/.test/parameters.json deleted file mode 100644 index 85cb87625b..0000000000 --- a/modules/Microsoft.Insights/scheduledQueryRules/.test/parameters.json +++ /dev/null @@ -1,71 +0,0 @@ -{ - "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#", - "contentVersion": "1.0.0.0", - "parameters": { - "name": { - "value": "myAlert01" - }, - "alertDescription": { - "value": "My sample Alert" - }, - "scopes": { - "value": [ - "/subscriptions/<>/resourcegroups/validation-rg/providers/microsoft.operationalinsights/workspaces/adp-<>-az-law-x-001" - ] - }, - "evaluationFrequency": { - "value": "PT5M" - }, - "windowSize": { - "value": "PT5M" - }, - "suppressForMinutes": { - "value": "PT5M" - }, - "queryTimeRange": { - "value": "PT5M" - }, - "autoMitigate": { - "value": false - }, - "criterias": { - "value": { - "allOf": [ - { - "query": "Perf | where ObjectName == \"LogicalDisk\" | where CounterName == \"% Free Space\" | where InstanceName <> \"HarddiskVolume1\" and InstanceName <> \"_Total\" | summarize AggregatedValue = min(CounterValue) by Computer, InstanceName, bin(TimeGenerated,5m)", - "timeAggregation": "Average", - "metricMeasureColumn": "AggregatedValue", - "dimensions": [ - { - "name": "Computer", - "operator": "Include", - "values": [ - "*" - ] - }, - { - "name": "InstanceName", - "operator": "Include", - "values": [ - "*" - ] - } - ], - "operator": "GreaterThan", - "threshold": 0 - } - ] - } - }, - "roleAssignments": { - "value": [ - { - "roleDefinitionIdOrName": "Reader", - "principalIds": [ - "<>" - ] - } - ] - } - } -} diff --git a/modules/Microsoft.Insights/scheduledQueryRules/deploy.bicep b/modules/Microsoft.Insights/scheduledQueryRules/deploy.bicep index 325eaa0b07..d706cccf5a 100644 --- a/modules/Microsoft.Insights/scheduledQueryRules/deploy.bicep +++ b/modules/Microsoft.Insights/scheduledQueryRules/deploy.bicep @@ -33,7 +33,7 @@ param targetResourceTypes array = [] param roleAssignments array = [] @description('Required. The list of resource IDs that this scheduled query rule is scoped to.') -param scopes array = [] +param scopes array @description('Optional. Severity of the alert. Should be an integer between [0-4]. Value of 0 is severest. Relevant and required only for rules of the kind LogAlert.') @allowed([ @@ -54,8 +54,8 @@ param windowSize string = '' @description('Optional. Actions to invoke when the alert fires.') param actions array = [] -@description('Optional. The rule criteria that defines the conditions of the scheduled query rule.') -param criterias object = {} +@description('Required. The rule criteria that defines the conditions of the scheduled query rule.') +param criterias object @description('Optional. Mute actions for the chosen period of time (in ISO 8601 duration format) after the alert is fired. If set, autoMitigate must be disabled.Relevant only for rules of the kind LogAlert.') param suppressForMinutes string = '' @@ -90,7 +90,6 @@ resource queryRule 'Microsoft.Insights/scheduledQueryRules@2021-02-01-preview' = } autoMitigate: (kind == 'LogAlert') ? autoMitigate : null criteria: criterias - description: alertDescription displayName: name enabled: enabled diff --git a/modules/Microsoft.Insights/scheduledQueryRules/readme.md b/modules/Microsoft.Insights/scheduledQueryRules/readme.md index 047dd5974e..cf99cbfb9a 100644 --- a/modules/Microsoft.Insights/scheduledQueryRules/readme.md +++ b/modules/Microsoft.Insights/scheduledQueryRules/readme.md @@ -23,6 +23,7 @@ This module deploys a scheduled query rule. | Parameter Name | Type | Description | | :-- | :-- | :-- | +| `criterias` | object | The rule criteria that defines the conditions of the scheduled query rule. | | `name` | string | The name of the Alert. | | `scopes` | array | The list of resource IDs that this scheduled query rule is scoped to. | @@ -33,7 +34,6 @@ This module deploys a scheduled query rule. | `actions` | array | `[]` | | Actions to invoke when the alert fires. | | `alertDescription` | string | `''` | | The description of the scheduled query rule. | | `autoMitigate` | bool | `True` | | The flag that indicates whether the alert should be automatically resolved or not. Relevant only for rules of the kind LogAlert. | -| `criterias` | object | `{object}` | | The rule criteria that defines the conditions of the scheduled query rule. | | `enabled` | bool | `True` | | The flag which indicates whether this scheduled query rule is enabled. | | `enableDefaultTelemetry` | bool | `True` | | Enable telemetry via the Customer Usage Attribution ID (GUID). | | `evaluationFrequency` | string | `''` | | How often the scheduled query rule is evaluated represented in ISO 8601 duration format. Relevant and required only for rules of the kind LogAlert. | @@ -169,7 +169,7 @@ The following module usage examples are retrieved from the content of the files >**Note**: Each example lists all the required parameters first, followed by the rest - each in alphabetical order. -

Example 1: Parameters

+

Example 1: Common

@@ -177,13 +177,9 @@ The following module usage examples are retrieved from the content of the files ```bicep module scheduledQueryRules './Microsoft.Insights/scheduledQueryRules/deploy.bicep' = { - name: '${uniqueString(deployment().name)}-ScheduledQueryRules' + name: '${uniqueString(deployment().name)}-test-isqrcom' params: { // Required parameters - name: 'myAlert01' - // Non-required parameters - alertDescription: 'My sample Alert' - autoMitigate: false criterias: { allOf: [ { @@ -205,25 +201,29 @@ module scheduledQueryRules './Microsoft.Insights/scheduledQueryRules/deploy.bice ] metricMeasureColumn: 'AggregatedValue' operator: 'GreaterThan' - query: 'Perf | where ObjectName == \'LogicalDisk\' | where CounterName == \'% Free Space\' | where InstanceName <> \'HarddiskVolume1\' and InstanceName <> \'_Total\' | summarize AggregatedValue = min(CounterValue) by Computer InstanceName bin(TimeGenerated5m)' + query: '' threshold: 0 timeAggregation: 'Average' } ] } + name: '<>isqrcom001' + scopes: [ + '' + ] + // Non-required parameters + alertDescription: 'My sample Alert' + autoMitigate: false evaluationFrequency: 'PT5M' queryTimeRange: 'PT5M' roleAssignments: [ { principalIds: [ - '<>' + '' ] roleDefinitionIdOrName: 'Reader' } ] - scopes: [ - '/subscriptions/<>/resourcegroups/validation-rg/providers/microsoft.operationalinsights/workspaces/adp-<>-az-law-x-001' - ] suppressForMinutes: 'PT5M' windowSize: 'PT5M' } @@ -243,16 +243,6 @@ module scheduledQueryRules './Microsoft.Insights/scheduledQueryRules/deploy.bice "contentVersion": "1.0.0.0", "parameters": { // Required parameters - "name": { - "value": "myAlert01" - }, - // Non-required parameters - "alertDescription": { - "value": "My sample Alert" - }, - "autoMitigate": { - "value": false - }, "criterias": { "value": { "allOf": [ @@ -275,13 +265,28 @@ module scheduledQueryRules './Microsoft.Insights/scheduledQueryRules/deploy.bice ], "metricMeasureColumn": "AggregatedValue", "operator": "GreaterThan", - "query": "Perf | where ObjectName == \"LogicalDisk\" | where CounterName == \"% Free Space\" | where InstanceName <> \"HarddiskVolume1\" and InstanceName <> \"_Total\" | summarize AggregatedValue = min(CounterValue) by Computer, InstanceName, bin(TimeGenerated,5m)", + "query": "", "threshold": 0, "timeAggregation": "Average" } ] } }, + "name": { + "value": "<>isqrcom001" + }, + "scopes": { + "value": [ + "" + ] + }, + // Non-required parameters + "alertDescription": { + "value": "My sample Alert" + }, + "autoMitigate": { + "value": false + }, "evaluationFrequency": { "value": "PT5M" }, @@ -292,17 +297,12 @@ module scheduledQueryRules './Microsoft.Insights/scheduledQueryRules/deploy.bice "value": [ { "principalIds": [ - "<>" + "" ], "roleDefinitionIdOrName": "Reader" } ] }, - "scopes": { - "value": [ - "/subscriptions/<>/resourcegroups/validation-rg/providers/microsoft.operationalinsights/workspaces/adp-<>-az-law-x-001" - ] - }, "suppressForMinutes": { "value": "PT5M" },