Add App Insights Diagnostic Settings Support (#317)

* init

* bicep working

* lint

* line length
This commit is contained in:
Ozi Boms 2023-04-10 10:13:52 -07:00 коммит произвёл GitHub
Родитель 58a19c9ba0
Коммит e64dd9acc6
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
9 изменённых файлов: 160 добавлений и 6 удалений

Просмотреть файл

@ -33,6 +33,7 @@
"Confirm-CosmosDBSqlRoleDefinition",
"Confirm-DataFactory",
"Confirm-DataFactoryLinkedService",
"Confirm-DiagnosticSetting",
"Confirm-EventHub",
"Confirm-EventHubConsumerGroup",
"Confirm-EventHubNamespace",

Просмотреть файл

@ -20,6 +20,7 @@
ContainerRegistry
DataFactory
DataFactoryLinkedService
DiagnosticSetting
EventHub
EventHubConsumerGroup
EventHubNamespace

Просмотреть файл

@ -0,0 +1,50 @@
# INLINE_SKIP
using module ./../Classes/ConfirmResult.psm1
. $PSScriptRoot/../Private/Connect-Account.ps1
# end INLINE_SKIP
function Confirm-DiagnosticSetting{
<#
.SYNOPSIS
Confirms that a Diagnostic Setting exists.
.DESCRIPTION
The Confirm-AzBPDiagnosticSetting cmdlet gets a Diagnostic Setting using the specified Diagnostic Setting name
and the specified Resource Id.
.PARAMETER Name
The name of the Diagnostic Setting.
.PARAMETER ResourceId
The Id of the Resource.
.EXAMPLE
Confirm-AzBPDiagnosticSetting -Name "benchpresstest"
-ResourceId "/subscriptions/{subscriptionId}/resourceGroups/{rg}/providers/Microsoft.ContainerService/managedClusters/aksnqpog"
.INPUTS
System.String
.OUTPUTS
ConfirmResult
#>
[CmdletBinding()]
[OutputType([ConfirmResult])]
param (
[Parameter(Mandatory=$true)]
[string]$Name,
[Parameter(Mandatory=$true)]
[string]$ResourceId
)
Begin {
$connectResults = Connect-Account
}
Process {
$resource = Get-AzDiagnosticSetting -ResourceId $ResourceId -Name $Name
[ConfirmResult]::new($resource, $connectResults.AuthenticationData)
}
End { }
}

Просмотреть файл

@ -81,8 +81,12 @@ function Confirm-Resource {
the id of the Role Definition.
.PARAMETER ClusterName
If the Azure resource is associated with an AKS Cluster (e.g, AKS Node Pool) this is the parameter to use to pass
the AKS cluster name.
If testing an Azure resource that is associated with an AKS Cluster (e.g, AKS Node Pool) this is the parameter
to use to pass the AKS cluster name.
.PARAMETER ResourceId
If testing an Azure resource that is associated with a Resource ID (e.g., Diagnostic Setting)
this is the parameter to use to pass the Resource ID.
.PARAMETER PropertyKey
The name of the property to check on the resource.
@ -170,6 +174,9 @@ function Confirm-Resource {
[Parameter(Mandatory = $false)]
[string]$JobName,
[Parameter(Mandatory = $false)]
[string]$ResourceId,
[Parameter(Mandatory = $false)]
[string]$RoleAssignmentId,
@ -203,6 +210,7 @@ function Confirm-Resource {
RoleAssignmentId = $RoleAssignmentId
RoleDefinitionId = $RoleDefinitionId
ClusterName = $ClusterName
ResourceId = $ResourceId
}
$confirmResult = Get-ResourceByType @resourceParams

Просмотреть файл

@ -13,6 +13,7 @@ using module ./../Classes/ResourceType.psm1
. $PSScriptRoot/Confirm-CosmosDBSqlRoleDefinition.ps1
. $PSScriptRoot/Confirm-DataFactory.ps1
. $PSScriptRoot/Confirm-DataFactoryLinkedService.ps1
. $PSScriptRoot/Confirm-DiagnosticSetting.ps1
. $PSScriptRoot/Confirm-EventHub.ps1
. $PSScriptRoot/Confirm-EventHubConsumerGroup.ps1
. $PSScriptRoot/Confirm-EventHubNamespace.ps1
@ -109,6 +110,10 @@ function Get-ResourceByType {
If the Azure resource is associated with an AKS Cluster (e.g, AKS Node Pool), this is the parameter to use to pass
the AKS cluster name.
.PARAMETER ResourceId
If testing an Azure resource that is associated with a Resource ID (e.g., Diagnostic Setting)
this is the parameter to use to pass the Resource ID.
.EXAMPLE
Get-AzBPResourceByType -ResourceType ActionGroup -ResourceName "bpactiongroup" -ResourceGroupName "rgbenchpresstest"
@ -172,6 +177,9 @@ function Get-ResourceByType {
[Parameter(Mandatory = $false)]
[string]$ClusterName,
[Parameter(Mandatory = $false)]
[string]$ResourceId,
[Parameter(Mandatory = $false)]
[string]$RoleAssignmentId,
@ -299,6 +307,9 @@ function Get-ResourceByType {
}
return Confirm-DataFactoryLinkedService @params
}
"DiagnosticSetting" {
return Confirm-DiagnosticSetting -ResourceId $ResourceId -Name $ResourceName
}
"EventHub" {
$params = @{
Name = $ResourceName

Просмотреть файл

@ -0,0 +1,23 @@
BeforeAll {
. $PSScriptRoot/../../Public/Confirm-DiagnosticSetting.ps1
. $PSScriptRoot/../../Private/Connect-Account.ps1
Import-Module Az
}
Describe "Confirm-DiagnosticSetting" {
Context "unit tests" -Tag "Unit" {
BeforeEach {
Mock Connect-Account{}
Mock Get-AzDiagnosticSetting{}
}
It "Calls Get-AzDiagnosticSetting" {
Confirm-DiagnosticSetting -ResourceId "testresourceId" -Name "dgName"
Should -Invoke -CommandName "Get-AzDiagnosticSetting" -Times 1
}
}
}
AfterAll {
Remove-Module Az
}

Просмотреть файл

@ -59,6 +59,49 @@ Describe 'Verify Application Insights' {
}
}
Describe 'Verify Diagnostic Setting' {
BeforeAll {
$Script:diagnosticSettingName = 'diagnosticsettingtest'
$Script:resourceId = "path/for/resourceId"
$Script:noDiagnosticSettingName = 'nodiagnosticsettingtest'
}
It "Should contain a Diagnostic Setting named $diagnosticSettingName - Confirm-AzBPResource" {
# arrange
$params = @{
ResourceType = "DiagnosticSetting"
ResourceName = $diagnosticSettingName
ResourceId = $resourceId
}
# act and assert
Confirm-AzBPResource @params | Should -BeSuccessful
}
It "Should contain a Diagnostic Setting named $diagnosticSettingName with Type of aksCluster -
Confirm-AzBPResource" {
# arrange
$params = @{
ResourceType = "DiagnosticSetting"
ResourceName = $diagnosticSettingName
ResourceId = $resourceId
PropertyKey = "Type"
PropertyValue = "Microsoft.Insights/diagnosticSettings"
}
# act and assert
Confirm-AzBPResource @params | Should -BeSuccessful
}
It "Should contain a Diagnostic Setting named $diagnosticSettingName" {
Confirm-AzBPDiagnosticSetting -ResourceId $ResourceId -Name $diagnosticSettingName | Should -BeSuccessful
}
It "Should contain a Diagnostic Setting named $diagnosticSettingName in $rgName" {
Confirm-AzBPDiagnosticSetting -ResourceId $ResourceId -Name $diagnosticSettingName | Should -BeInResourceGroup $rgName
}
}
AfterAll {
Get-Module Az.InfrastructureTesting | Remove-Module
Get-Module BenchPress.Azure | Remove-Module

Просмотреть файл

@ -1,6 +1,7 @@
# How To Run AppInsights.Tests.ps1
`AppInsights.Tests.ps1` contains examples of using the `Confirm-AzBPAppInsights` cmdlet.
`AppInsights.Tests.ps1` contains examples of using the `Confirm-AzBPAppInsights`
and `Confirm-AzBPDiagnosticSetting` cmdlets.
## Pre-Requisites
@ -23,9 +24,11 @@
1. Update `AppInsights.Tests.ps1` variables to point to your expected resources:
- `rg-test` -> `your-resource-group-name`
- `appinsightstest` -> `your-app-insights-name`
- `westus3` -> `your-resource-group-location-name`
- `rg-test` -> `your-resource-group-name`
- `appinsightstest` -> `your-app-insights-name`
- `westus3` -> `your-resource-group-location-name`
- `diagnosticsettingtest` -> `your-diagnostic-setting-name`
- `path/for/resourceId` -> `your-resource-id`
1. If using a local copy of `Az.InfrastructureTesting`, replace `Import-Module Az.InfrastructureTesting` with
`Import-Module "../../bin/BenchPress.Azure.psd1"`. Note that the final `AfterAll` step will properly remove the module

Просмотреть файл

@ -21,3 +21,17 @@ resource applicationInsights 'Microsoft.Insights/components@2020-02-02' = {
WorkspaceResourceId: workspace.id
}
}
resource appInsightsDiagnostics 'Microsoft.Insights/diagnosticSettings@2021-05-01-preview' = {
scope: applicationInsights
name: 'default'
properties: {
workspaceId: workspace.id
metrics: [
{
category: 'AllMetrics'
enabled: true
}
]
}
}