* Added support for API Management Service, API Management API, API Management Diagnostics, API Management Loggers, and API Management Policies

* Minor fixes.

* Fixing broken unit test.

* Escaping the XML

* Removing unused parameter per code review.

* Fixing examples in the comments per code review.

* Added comments for Confirm-ApiManagmentLogger

* Making ResourceName optional.

* Removing the list of options for ResourceType from Confirm-ResourceByType. It is an enum now, no need to list them.

* Updating from Az-InfrastructureTesting to Az.InfrastructureTesting.

* Updated example tests to use pipelining.
This commit is contained in:
marclerwick 2023-03-30 15:40:34 -07:00 коммит произвёл GitHub
Родитель a8eff62505
Коммит 2d406ff60b
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
38 изменённых файлов: 1071 добавлений и 57 удалений

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

@ -13,6 +13,11 @@
FunctionsToExport = @( FunctionsToExport = @(
"Confirm-ActionGroup", "Confirm-ActionGroup",
"Confirm-AksCluster", "Confirm-AksCluster",
"Confirm-ApiManagement",
"Confirm-ApiManagementApi",
"Confirm-ApiManagementDiagnostic",
"Confirm-ApiManagementLogger",
"Confirm-ApiManagementPolicy",
"Confirm-AppInsights", "Confirm-AppInsights",
"Confirm-AppServicePlan", "Confirm-AppServicePlan",
"Confirm-BicepFile", "Confirm-BicepFile",

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

@ -1,6 +1,11 @@
enum ResourceType{ enum ResourceType{
ActionGroup ActionGroup
AksCluster AksCluster
ApiManagement
ApiManagementApi
ApiManagementDiagnostic
ApiManagementLogger
ApiManagementPolicy
AppInsights AppInsights
AppServicePlan AppServicePlan
ContainerApp ContainerApp

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

@ -0,0 +1,49 @@
# INLINE_SKIP
using module ./../Classes/ConfirmResult.psm1
. $PSScriptRoot/../Private/Connect-Account.ps1
# end INLINE_SKIP
function Confirm-ApiManagement {
<#
.SYNOPSIS
Confirms that an API Management Service exists.
.DESCRIPTION
The Confirm-AzBPApiManagement cmdlet gets an API Management Service using the specified API Management Service
and Resource Group names.
.PARAMETER ResourceGroupName
Specifies the name of the resource group under in which this cmdlet gets the API Management service.
.PARAMETER Name
Specifies the name of API Management service.
.EXAMPLE
Confirm-AzBPApiManagement -ResourceGroupName "rgbenchpresstest" -Name "benchpresstest"
.INPUTS
System.String
.OUTPUTS
ConfirmResult
#>
[CmdletBinding()]
[OutputType([ConfirmResult])]
param (
[Parameter(Mandatory=$true)]
[string]$ResourceGroupName,
[Parameter(Mandatory=$true)]
[string]$Name
)
Begin {
$ConnectResults = Connect-Account
}
Process {
$Resource = Get-AzApiManagement -ResourceGroupName $ResourceGroupName -Name $Name
[ConfirmResult]::new($Resource, $ConnectResults.AuthenticationData)
}
End { }
}

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

@ -0,0 +1,56 @@
# INLINE_SKIP
using module ./../Classes/ConfirmResult.psm1
. $PSScriptRoot/../Private/Connect-Account.ps1
# end INLINE_SKIP
function Confirm-ApiManagementApi {
<#
.SYNOPSIS
Confirms that an API Management API exists.
.DESCRIPTION
The Confirm-AzBPApiManagementApi cmdlet gets an API Management API using the specified API, API Management
Service, and Resource Group names.
.PARAMETER ResourceGroupName
Specifies the name of the resource group under which an API Management service is deployed.
.PARAMETER ServiceName
Specifies the name of the deployed API Management service.
.PARAMETER Name
Specifies the name of the API to get.
.EXAMPLE
Confirm-AzBPApiManagementApi -ResourceGroupName "rgbenchpresstest" -ServiceName "servicetest" -Name "benchpresstest"
.INPUTS
System.String
.OUTPUTS
ConfirmResult
#>
[CmdletBinding()]
[OutputType([ConfirmResult])]
param (
[Parameter(Mandatory=$true)]
[string]$ResourceGroupName,
[Parameter(Mandatory=$true)]
[string]$ServiceName,
[Parameter(Mandatory=$true)]
[string]$Name
)
Begin {
$ConnectResults = Connect-Account
}
Process {
$Resource = New-AzApiManagementContext -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName
| Get-AzApiManagementApi -Name $Name
[ConfirmResult]::new($Resource, $ConnectResults.AuthenticationData)
}
End { }
}

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

@ -0,0 +1,57 @@
# INLINE_SKIP
using module ./../Classes/ConfirmResult.psm1
. $PSScriptRoot/../Private/Connect-Account.ps1
# end INLINE_SKIP
function Confirm-ApiManagementDiagnostic {
<#
.SYNOPSIS
Confirms that an API Management Diagnostic exists.
.DESCRIPTION
The Confirm-AzBPApiManagementDiagnostic cmdlet gets an API Management Diagnostic using the specified API
Diagnostic, API, API Management Service, and Resource Group names.
.PARAMETER ResourceGroupName
Specifies the name of the resource group under which an API Management service is deployed.
.PARAMETER ServiceName
Specifies the name of the deployed API Management service.
.PARAMETER Name
Identifier of existing diagnostic. This will return product-scope policy. This parameters is required.
.EXAMPLE
Confirm-AzBPApiManagementDiagnostic -ResourceGroupName "rgbenchpresstest" -ServiceName "servicetest" `
-Name "benchpresstest"
.INPUTS
System.String
.OUTPUTS
ConfirmResult
#>
[CmdletBinding()]
[OutputType([ConfirmResult])]
param (
[Parameter(Mandatory=$true)]
[string]$ResourceGroupName,
[Parameter(Mandatory=$true)]
[string]$ServiceName,
[Parameter(Mandatory=$true)]
[string]$Name
)
Begin {
$ConnectResults = Connect-Account
}
Process {
$Resource = New-AzApiManagementContext -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName
| Get-AzApiManagementDiagnostic -DiagnosticId $Name
[ConfirmResult]::new($Resource, $ConnectResults.AuthenticationData)
}
End { }
}

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

@ -0,0 +1,58 @@
# INLINE_SKIP
using module ./../Classes/ConfirmResult.psm1
. $PSScriptRoot/../Private/Connect-Account.ps1
# end INLINE_SKIP
function Confirm-ApiManagementLogger {
<#
.SYNOPSIS
Confirms that an API Management Logger exists.
.DESCRIPTION
The Confirm-AzBPApiManagementLogger cmdlet gets an API Management Logger using the specified Logger, API
Management Service, and Resource Group names.
.PARAMETER ResourceGroupName
Specifies the name of the resource group under which an API Management service is deployed.
.PARAMETER ServiceName
Specifies the name of the deployed API Management service.
.PARAMETER Name
Specifies the ID of the specific logger to get.
.EXAMPLE
Confirm-AzBPApiManagementLogger -ResourceGroupName "rgbenchpresstest" -ServiceName "servicetest" `
-Name "benchpresstest"
.INPUTS
System.String
.OUTPUTS
ConfirmResult
#>
[CmdletBinding()]
[OutputType([ConfirmResult])]
param (
[Parameter(Mandatory=$true)]
[string]$ResourceGroupName,
[Parameter(Mandatory=$true)]
[string]$ServiceName,
[Parameter(Mandatory=$true)]
[string]$Name
)
Begin {
$ConnectResults = Connect-Account
}
Process {
# Unlike the other Get-AzApiManagement* cmdlets Get-AzApiManagementLogger does not accept piping of the context
$Context = New-AzApiManagementContext -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName
$Resource = Get-AzApiManagementLogger -Context $Context -LoggerId $Name
[ConfirmResult]::new($Resource, $ConnectResults.AuthenticationData)
}
End { }
}

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

@ -0,0 +1,62 @@
# INLINE_SKIP
using module ./../Classes/ConfirmResult.psm1
. $PSScriptRoot/../Private/Connect-Account.ps1
# end INLINE_SKIP
function Confirm-ApiManagementPolicy {
<#
.SYNOPSIS
Confirms that an API Management Policy exists.
.DESCRIPTION
The Confirm-AzBPApiManagementPolicy cmdlet gets an API Management Policy using the specified API, API Management
Service, and Resource Group names.
.PARAMETER ResourceGroupName
Specifies the name of the resource group under which an API Management service is deployed.
.PARAMETER ServiceName
Specifies the name of the deployed API Management service.
.PARAMETER ApiId
Specifies the identifier of the existing API. This cmdlet returns the API-scope policy.
.EXAMPLE
Confirm-AzBPApiManagementPolicy -ResourceGroupName "rgbenchpresstest" -ServiceName "servicetest" `
-ApiId "benchpresstest"
.INPUTS
System.String
.OUTPUTS
ConfirmResult
#>
[CmdletBinding()]
[OutputType([ConfirmResult])]
param (
[Parameter(Mandatory=$true)]
[string]$ResourceGroupName,
[Parameter(Mandatory=$true)]
[string]$ServiceName,
[Parameter(Mandatory=$true)]
[string]$ApiId
)
Begin {
$ConnectResults = Connect-Account
}
Process {
$policy = New-AzApiManagementContext -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName
| Get-AzApiManagementPolicy -ApiId $ApiId
# Get-AzApiManagementPolicy returns the XML for a policy, not a resource
if ([string]::IsNullOrWhiteSpace($policy)) {
$policy = $null
}
[ConfirmResult]::new($policy, $ConnectResults.AuthenticationData)
}
End { }
}

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

@ -78,20 +78,14 @@ function Confirm-Resource {
[CmdletBinding()] [CmdletBinding()]
[OutputType([ConfirmResult])] [OutputType([ConfirmResult])]
param ( param (
[Parameter(Mandatory = $true)]
[ResourceType]$ResourceType,
[Parameter(Mandatory = $true)] [Parameter(Mandatory = $true)]
[string]$ResourceName, [string]$ResourceName,
[Parameter(Mandatory = $false)] [Parameter(Mandatory = $false)]
[string]$ResourceGroupName, [string]$ResourceGroupName,
[Parameter(Mandatory = $false)] [Parameter(Mandatory = $true)]
[string]$NamespaceName, [ResourceType]$ResourceType,
[Parameter(Mandatory = $false)]
[string]$EventHubName,
[Parameter(Mandatory = $false)] [Parameter(Mandatory = $false)]
[string]$ServerName, [string]$ServerName,
@ -99,12 +93,21 @@ function Confirm-Resource {
[Parameter(Mandatory = $false)] [Parameter(Mandatory = $false)]
[string]$DataFactoryName, [string]$DataFactoryName,
[Parameter(Mandatory = $false)]
[string]$NamespaceName,
[Parameter(Mandatory = $false)]
[string]$EventHubName,
[Parameter(Mandatory = $false)] [Parameter(Mandatory = $false)]
[string]$WorkspaceName, [string]$WorkspaceName,
[Parameter(Mandatory = $false)] [Parameter(Mandatory = $false)]
[string]$AccountName, [string]$AccountName,
[Parameter(Mandatory = $false)]
[string]$ServiceName,
[Parameter(Mandatory = $false)] [Parameter(Mandatory = $false)]
[string]$PropertyKey, [string]$PropertyKey,
@ -114,15 +117,16 @@ function Confirm-Resource {
Begin { } Begin { }
Process { Process {
$ResourceParams = @{ $ResourceParams = @{
ResourceType = $ResourceType
NamespaceName = $NamespaceName
EventHubName = $EventHubName
ResourceName = $ResourceName ResourceName = $ResourceName
ResourceGroupName = $ResourceGroupName ResourceGroupName = $ResourceGroupName
ResourceType = $ResourceType
ServerName = $ServerName ServerName = $ServerName
DataFactoryName = $DataFactoryName DataFactoryName = $DataFactoryName
NamespaceName = $NamespaceName
EventHubName = $EventHubName
WorkspaceName = $WorkspaceName WorkspaceName = $WorkspaceName
AccountName = $AccountName AccountName = $AccountName
ServiceName = $ServiceName
} }
$ConfirmResult = Get-ResourceByType @ResourceParams $ConfirmResult = Get-ResourceByType @ResourceParams

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

@ -43,29 +43,7 @@ function Get-ResourceByType {
The name of the Resource Group The name of the Resource Group
.PARAMETER ResourceType .PARAMETER ResourceType
The type of the Resource (currently support the following: The type of the Resource.
ActionGroup
AksCluster
AppInsights
AppServicePlan
ContainerRegistry
DataFactory
DataFactoryLinkedService
EventHub
EventHubConsumerGroup
EventHubNamespace
KeyVault
ResourceGroup
SqlDatabase
SqlServer
StorageAccount
StorageContainer
StreamAnalyticsCluster
SynapseSparkPool
SynapseSqlPool
SynapseWorkspace
VirtualMachine
WebApp)
.PARAMETER ServerName .PARAMETER ServerName
If testing an Azure SQL Database resource, the name of the server to which the database is assigned. If testing an Azure SQL Database resource, the name of the server to which the database is assigned.
@ -79,7 +57,12 @@ function Get-ResourceByType {
the name of the workspace to which the resource is assigned. the name of the workspace to which the resource is assigned.
.PARAMETER AccountName .PARAMETER AccountName
If the Azure resource has an associated account name (e.g., Cosmos DB SQL Database, Storage Container), If the Azure resource has an associated account name (e.g., Cosmos DB SQL Database, Storage Container) this is
the parameter to use to pass the account name.
.PARAMETER ServiceName
If the Azure resource is associated with a service (e.g, API Management Service) this is the parameter to use to
pass the service name.
.EXAMPLE .EXAMPLE
Get-AzBPResourceByType -ResourceType ActionGroup -ResourceName "bpactiongroup" -ResourceGroupName "rgbenchpresstest" Get-AzBPResourceByType -ResourceType ActionGroup -ResourceName "bpactiongroup" -ResourceGroupName "rgbenchpresstest"
@ -96,7 +79,7 @@ function Get-ResourceByType {
[CmdletBinding()] [CmdletBinding()]
[OutputType([ConfirmResult])] [OutputType([ConfirmResult])]
param ( param (
[Parameter(Mandatory = $true)] [Parameter(Mandatory = $false)]
[string]$ResourceName, [string]$ResourceName,
[Parameter(Mandatory = $false)] [Parameter(Mandatory = $false)]
@ -121,7 +104,10 @@ function Get-ResourceByType {
[string]$WorkspaceName, [string]$WorkspaceName,
[Parameter(Mandatory = $false)] [Parameter(Mandatory = $false)]
[string]$AccountName [string]$AccountName,
[Parameter(Mandatory = $false)]
[string]$ServiceName
) )
Begin { } Begin { }
Process { Process {
@ -132,6 +118,41 @@ function Get-ResourceByType {
"AksCluster" { "AksCluster" {
return Confirm-AksCluster -AKSName $ResourceName -ResourceGroupName $ResourceGroupName return Confirm-AksCluster -AKSName $ResourceName -ResourceGroupName $ResourceGroupName
} }
"ApiManagement" {
return Confirm-ApiManagement -ResourceGroupName $ResourceGroupName -Name $ResourceName
}
"ApiManagementApi" {
$params = @{
ResourceGroupName = $ResourceGroupName
ServiceName = $ServiceName
Name = $ResourceName
}
return Confirm-ApiManagementApi @params
}
"ApiManagementDiagnostic" {
$params = @{
ResourceGroupName = $ResourceGroupName
ServiceName = $ServiceName
Name = $ResourceName
}
return Confirm-ApiManagementDiagnostic @params
}
"ApiManagementLogger" {
$params = @{
ResourceGroupName = $ResourceGroupName
ServiceName = $ServiceName
Name = $ResourceName
}
return Confirm-ApiManagementLogger @params
}
"ApiManagementPolicy" {
$params = @{
ResourceGroupName = $ResourceGroupName
ServiceName = $ServiceName
ApiId = $ResourceName
}
return Confirm-ApiManagementPolicy @params
}
"AppInsights" { "AppInsights" {
return Confirm-AppInsights -ResourceGroupName $ResourceGroupName -Name $ResourceName return Confirm-AppInsights -ResourceGroupName $ResourceGroupName -Name $ResourceName
} }

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

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

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

@ -0,0 +1,26 @@
BeforeAll {
. $PSScriptRoot/../../Public/Confirm-ApiManagementApi.ps1
. $PSScriptRoot/../../Private/Connect-Account.ps1
Import-Module Az
}
Describe "Confirm-ApiManagementApi" {
Context "unit tests" -Tag "Unit" {
BeforeEach {
Mock Connect-Account{}
Mock Get-AzApiManagementApi{}
Mock New-AzApiManagementContext{
New-MockObject -Type 'Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Models.PsApiManagementContext'
}
}
It "Calls Get-AzApiManagementApi" {
Confirm-ApiManagementApi -ResourceGroupName "rgn" -ServiceName "sn" -Name "api"
Should -Invoke -CommandName "Get-AzApiManagementApi" -Times 1
}
}
}
AfterAll {
Remove-Module Az
}

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

@ -0,0 +1,26 @@
BeforeAll {
. $PSScriptRoot/../../Public/Confirm-ApiManagementDiagnostic.ps1
. $PSScriptRoot/../../Private/Connect-Account.ps1
Import-Module Az
}
Describe "Confirm-ApiManagementDiagnostic" {
Context "unit tests" -Tag "Unit" {
BeforeEach {
Mock Connect-Account{}
Mock Get-AzApiManagementDiagnostic{}
Mock New-AzApiManagementContext{
New-MockObject -Type 'Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Models.PsApiManagementContext'
}
}
It "Calls Get-AzApiManagementDiagnostic" {
Confirm-ApiManagementDiagnostic -ResourceGroupName "rgn" -ServiceName "sn" -Name "diag"
Should -Invoke -CommandName "Get-AzApiManagementDiagnostic" -Times 1
}
}
}
AfterAll {
Remove-Module Az
}

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

@ -0,0 +1,26 @@
BeforeAll {
. $PSScriptRoot/../../Public/Confirm-ApiManagementLogger.ps1
. $PSScriptRoot/../../Private/Connect-Account.ps1
Import-Module Az
}
Describe "Confirm-ApiManagementLogger" {
Context "unit tests" -Tag "Unit" {
BeforeEach {
Mock Connect-Account{}
Mock Get-AzApiManagementLogger{}
Mock New-AzApiManagementContext{
New-MockObject -Type 'Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Models.PsApiManagementContext'
}
}
It "Calls Get-AzApiManagementLogger" {
Confirm-ApiManagementLogger -ResourceGroupName "rgn" -ServiceName "sn" -Name "logger"
Should -Invoke -CommandName "Get-AzApiManagementLogger" -Times 1
}
}
}
AfterAll {
Remove-Module Az
}

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

@ -0,0 +1,26 @@
BeforeAll {
. $PSScriptRoot/../../Public/Confirm-ApiManagementPolicy.ps1
. $PSScriptRoot/../../Private/Connect-Account.ps1
Import-Module Az
}
Describe "Confirm-ApiManagementPolicy" {
Context "unit tests" -Tag "Unit" {
BeforeEach {
Mock Connect-Account{}
Mock Get-AzApiManagementPolicy{}
Mock New-AzApiManagementContext{
New-MockObject -Type 'Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Models.PsApiManagementContext'
}
}
It "Calls Get-AzApiManagementPolicy" {
Confirm-ApiManagementPolicy -ResourceGroupName "rgn" -ServiceName "sn" -ApiId "apiid"
Should -Invoke -CommandName "Get-AzApiManagementPolicy" -Times 1
}
}
}
AfterAll {
Remove-Module Az
}

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

@ -88,6 +88,6 @@ Describe 'Verify Action Group' {
} }
AfterAll { AfterAll {
Get-Module Az-InfrastructureTesting | Remove-Module Get-Module Az.InfrastructureTesting | Remove-Module
Get-Module BenchPress.Azure | Remove-Module Get-Module BenchPress.Azure | Remove-Module
} }

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

@ -88,6 +88,6 @@ Describe 'Verify AKS Cluster' {
} }
AfterAll { AfterAll {
Get-Module Az-InfrastructureTesting | Remove-Module Get-Module Az.InfrastructureTesting | Remove-Module
Get-Module BenchPress.Azure | Remove-Module Get-Module BenchPress.Azure | Remove-Module
} }

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

@ -0,0 +1,371 @@
BeforeAll {
Import-Module Az.InfrastructureTesting
$Script:rgName = 'rg-test'
$Script:apiServiceName = 'servicetest'
$Script:apiName = 'apitest'
$Script:location = 'westus3'
}
Describe 'Verify API Management Service' {
BeforeAll {
$Script:noApiServiceName = 'noservice'
}
It 'Should contain an API Management Service with the given name - Confirm-AzBPResource' {
#arrange
$params = @{
ResourceType = "ApiManagement"
ResourceGroupName = $rgName
ResourceName = $apiServiceName
}
#act and assert
(Confirm-AzBPResource @params).Success | Should -Be $true
}
It "Should contain an API Management Service named $apiServiceName - ConfirmAzBPResource" {
#arrange
$params = @{
ResourceType = "ApiManagement"
ResourceGroupName = $rgName
ResourceName = $apiServiceName
PropertyKey = 'Name'
PropertyValue = $apiServiceName
}
#act and assert
(Confirm-AzBPResource @params).Success | Should -Be $true
}
It 'Should contain an API Management Service with the given name' {
(Confirm-AzBPApiManagement -ResourceGroupName $rgName -Name $apiServiceName).Success | Should -Be $true
}
It 'Should not contain an API Management Service with the given name' {
#arrange
$params = @{
ResourceGroupName = $rgName
Name = $noApiServiceName
ErrorAction = "SilentlyContinue"
}
#act and assert
# The '-ErrorAction SilentlyContinue' command suppresses all errors.
# In this test, it will suppress the error message when a resource cannot be found.
# Remove this field to see all errors.
(Confirm-AzBPApiManagement @params).Success | Should -Be $false
}
It "Should contain an API Management Service named $apiServiceName" {
Confirm-AzBPApiManagement -ResourceGroupName $rgName -Name $apiServiceName | Should -BeDeployed
}
It "Should contain an API Management Service named $apiServiceName in $location" {
Confirm-AzBPApiManagement -ResourceGroupName $rgName -Name $apiServiceName | Should -BeInLocation $location
}
It "Should be an API Management Service in a resource group named $rgName" {
Confirm-AzBPApiManagement -ResourceGroupName $rgName -Name $apiServiceName | Should -BeInResourceGroup $rgName
}
}
Describe 'Verify API Management API' {
BeforeAll {
$Script:noApiName = 'noapi'
}
It 'Should contain an API Management API with the given name - Confirm-AzBPResource' {
#arrange
$params = @{
ResourceType = "ApiManagementApi"
ResourceGroupName = $rgName
ServiceName = $apiServiceName
ResourceName = $apiName
}
#act and assert
(Confirm-AzBPResource @params).Success | Should -Be $true
}
It "Should contain an API Management API named $apiName - ConfirmAzBPResource" {
#arrange
$params = @{
ResourceType = "ApiManagementApi"
ResourceGroupName = $rgName
ServiceName = $apiServiceName
ResourceName = $apiName
PropertyKey = 'Name'
PropertyValue = $apiName
}
#act and assert
(Confirm-AzBPResource @params).Success | Should -Be $true
}
It 'Should contain an API Management API with the given name' {
(Confirm-AzBPApiManagementApi -ResourceGroupName $rgName -ServiceName $apiServiceName -Name $apiName).Success
| Should -Be $true
}
It 'Should not contain an API Management API with the given name' {
# arrange
# The '-ErrorAction SilentlyContinue' command suppresses all errors.
# In this test, it will suppress the error message when a resource cannot be found.
# Remove this field to see all errors.
$params = @{
ResourceGroupName = $rgName
Name = $noApiName
ServiceName = $apiServiceName
ErrorAction = "SilentlyContinue"
}
#act and assert
(Confirm-AzBPApiManagementApi @params).Success | Should -Be $false
}
It "Should contain an API Management API named $apiName" {
Confirm-AzBPApiManagementApi -ResourceGroupName $rgName -ServiceName $apiServiceName -Name $apiName
| Should -BeDeployed
}
It "Should be an API Management API in a resource group named $rgName" {
Confirm-AzBPApiManagementApi -ResourceGroupName $rgName -ServiceName $apiServiceName -Name $apiName
| Should -BeInResourceGroup $rgName
}
}
Describe 'Verify API Management Diagnostic' {
BeforeAll {
$Script:diagnosticName = 'diagtest'
$Script:noDiagnosticName = 'nodiag'
}
It 'Should contain an API Management Diagnostic with the given name - Confirm-AzBPResource' {
#arrange
$params = @{
ResourceType = "ApiManagementDiagnostic"
ResourceGroupName = $rgName
ServiceName = $apiServiceName
ResourceName = $diagnosticName
}
#act and assert
(Confirm-AzBPResource @params).Success | Should -Be $true
}
It "Should contain an API Management Diagnostic named $diagnosticName - ConfirmAzBPResource" {
#arrange
$params = @{
ResourceType = "ApiManagementDiagnostic"
ResourceGroupName = $rgName
ServiceName = $apiServiceName
ResourceName = $diagnosticName
PropertyKey = 'DiagnosticId'
PropertyValue = $diagnosticName
}
#act and assert
(Confirm-AzBPResource @params).Success | Should -Be $true
}
It 'Should contain an API Management Diagnostic with the given name' {
#arrange
$params = @{
ResourceGroupName = $rgName
ServiceName = $apiServiceName
Name = $diagnosticName
}
#act and assert
(Confirm-AzBPApiManagementDiagnostic @params).Success | Should -Be $true
}
It 'Should not contain an API Management Diagnostic with the given name' {
#arrange
$params = @{
ResourceGroupName = $rgName
ServiceName = $apiServiceName
Name = $noDiagnosticName
ErrorAction = "SilentlyContinue"
}
#act and assert
# The '-ErrorAction SilentlyContinue' command suppresses all errors.
# In this test, it will suppress the error message when a resource cannot be found.
# Remove this field to see all errors.
(Confirm-AzBPApiManagementDiagnostic @params).Success | Should -Be $false
}
It "Should contain an API Management Diagnostic named $diagnosticName" {
#arrange
$params = @{
ResourceGroupName = $rgName
ServiceName = $apiServiceName
Name = $diagnosticName
}
#act and assert
Confirm-AzBPApiManagementDiagnostic @params | Should -BeDeployed
}
It "Should be an API Management Diagnostic in a resource group named $rgName" {
$params = @{
ResourceGroupName = $rgName
ServiceName = $apiServiceName
Name = $diagnosticName
}
#act and assert
Confirm-AzBPApiManagementDiagnostic @params | Should -BeInResourceGroup $rgName
}
}
Describe 'Verify API Management Logger' {
BeforeAll {
$Script:loggerName = 'loggertest'
$Script:noLoggerName = 'nologger'
}
It 'Should contain an API Management Logger with the given name - Confirm-AzBPResource' {
#arrange
$params = @{
ResourceType = "ApiManagementLogger"
ResourceGroupName = $rgName
ServiceName = $apiServiceName
ResourceName = $loggerName
}
#act and assert
(Confirm-AzBPResource @params).Success | Should -Be $true
}
It "Should contain an API Management Logger named $loggerName - ConfirmAzBPResource" {
#arrange
$params = @{
ResourceType = "ApiManagementLogger"
ResourceGroupName = $rgName
ServiceName = $apiServiceName
ResourceName = $loggerName
PropertyKey = 'LoggerId'
PropertyValue = $loggerName
}
#act and assert
(Confirm-AzBPResource @params).Success | Should -Be $true
}
It 'Should contain an API Management Logger with the given name' {
#arrange
$params = @{
ResourceGroupName = $rgName
ServiceName = $apiServiceName
Name = $loggerName
}
#act and assert
(Confirm-AzBPApiManagementLogger @params).Success | Should -Be $true
}
It 'Should not contain an API Management Logger with the given name' {
#arrange
$params = @{
ResourceGroupName = $rgName
ServiceName = $apiServiceName
Name = $noLoggerName
ErrorAction = "SilentlyContinue"
}
#act and assert
# The '-ErrorAction SilentlyContinue' command suppresses all errors.
# In this test, it will suppress the error message when a resource cannot be found.
# Remove this field to see all errors.
(Confirm-AzBPApiManagementLogger @params).Success | Should -Be $false
}
It "Should contain an API Management Logger named $loggerName" {
#arrange
$params = @{
ResourceGroupName = $rgName
ServiceName = $apiServiceName
Name = $loggerName
}
#act and assert
Confirm-AzBPApiManagementLogger @params | Should -BeDeployed
}
It "Should be an API Management Logger in a resource group named $rgName" {
$params = @{
ResourceGroupName = $rgName
ServiceName = $apiServiceName
Name = $loggerName
}
#act and assert
Confirm-AzBPApiManagementLogger @params | Should -BeInResourceGroup $rgName
}
}
Describe 'Verify API Management Policy' {
BeforeAll {
$Script:noApiId = 'nopolicy'
}
It "Should contain an API Management Policy for the API ID $apiName - Confirm-AzBPResource" {
#arrange
$params = @{
ResourceType = "ApiManagementPolicy"
ResourceGroupName = $rgName
ServiceName = $apiServiceName
ResourceName = $apiName
}
#act and assert
(Confirm-AzBPResource @params).Success | Should -Be $true
}
It "Should contain an API Management Policy for the API ID $apiName" {
#arrange
$params = @{
ResourceGroupName = $rgName
ServiceName = $apiServiceName
ApiId = $apiName
}
#act and assert
(Confirm-AzBPApiManagementPolicy @params).Success | Should -Be $true
}
It "Should not contain an API Management Policy for the API ID $apiName" {
#arrange
$params = @{
ResourceGroupName = $rgName
ServiceName = $apiServiceName
ApiId = $noApiId
ErrorAction = "SilentlyContinue"
}
#act and assert
# The '-ErrorAction SilentlyContinue' command suppresses all errors.
# In this test, it will suppress the error message when a resource cannot be found.
# Remove this field to see all errors.
(Confirm-AzBPApiManagementPolicy @params).Success | Should -Be $false
}
It "Should contain an API Management Policy for the API ID $apiName" {
#arrange
$params = @{
ResourceGroupName = $rgName
ServiceName = $apiServiceName
ApiId = $apiName
}
#act and assert
Confirm-AzBPApiManagementPolicy @params | Should -BeDeployed
}
}
AfterAll {
Get-Module Az.InfrastructureTesting | Remove-Module
Get-Module BenchPress.Azure | Remove-Module
}

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

@ -0,0 +1,85 @@
param serviceName string = 'apim${take(uniqueString(resourceGroup().id), 5)}'
param location string = resourceGroup().location
resource apiManagementService 'Microsoft.ApiManagement/service@2022-08-01' = {
name: serviceName
location: location
sku: {
capacity: 1
name: 'Developer'
}
properties: {
publisherEmail: 'email@contoso.com'
publisherName: 'Contoso'
}
}
param apiName string = 'api${take(uniqueString(resourceGroup().id), 5)}'
resource api 'Microsoft.ApiManagement/service/apis@2022-08-01' = {
name: apiName
parent: apiManagementService
properties: {
displayName: 'api'
path: '/'
protocols: ['https']
}
}
param workspaceName string = 'logworkspace${take(uniqueString(resourceGroup().id), 5)}'
resource workspace 'Microsoft.OperationalInsights/workspaces@2022-10-01' = {
name: workspaceName
location: location
properties: {
sku: {
name: 'PerGB2018'
}
}
}
param appInsightsName string = 'appinsights${take(uniqueString(resourceGroup().id), 5)}'
resource applicationInsights 'Microsoft.Insights/components@2020-02-02' = {
name: appInsightsName
location: location
kind: 'web'
properties: {
Application_Type: 'web'
WorkspaceResourceId: workspace.id
}
}
param loggerName string = 'log${take(uniqueString(resourceGroup().id), 5)}'
resource logger 'Microsoft.ApiManagement/service/loggers@2022-08-01' = {
name: loggerName
parent: apiManagementService
properties: {
credentials: {
instrumentationKey: applicationInsights.properties.InstrumentationKey
}
loggerType: 'applicationInsights'
}
}
param diagnosticName string = 'applicationinsights'
resource diagnostic 'Microsoft.ApiManagement/service/diagnostics@2022-08-01' = {
name: diagnosticName
parent: apiManagementService
properties: {
loggerId: logger.id
}
}
param policyName string = 'policy'
resource apiPolicy 'Microsoft.ApiManagement/service/apis/policies@2022-08-01' = {
name: policyName
parent: api
properties: {
format: 'rawxml'
value: loadTextContent('./ApiManagementPolicy.xml')
}
}

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

@ -0,0 +1,63 @@
<!-- Policy configuration for the API. Explore other sample policies at https://learn.microsoft.com/en-us/azure/api-management/policies/ -->
<policies>
<inbound>
<base />
<!-- This policy is needed to handle preflight requests using the OPTIONS method. Learn more at https://learn.microsoft.com/en-us/azure/api-management/api-management-cross-domain-policies -->
<cors allow-credentials="false">
<allowed-origins>
<origin>*</origin>
</allowed-origins>
<allowed-methods>
<method>PUT</method>
<method>GET</method>
<method>POST</method>
<method>DELETE</method>
<method>PATCH</method>
</allowed-methods>
<allowed-headers>
<header>*</header>
</allowed-headers>
<expose-headers>
<header>*</header>
</expose-headers>
</cors>
</inbound>
<backend>
<limit-concurrency key="@(context.Request.IpAddress)" max-count="3">
<forward-request timeout="120" />
</limit-concurrency>
</backend>
<outbound>
<base />
<!-- Optional policy to to send custom metrics to Application Insights. Learn more at https://learn.microsoft.com/en-us/azure/api-management/api-management-advanced-policies#emit-metrics -->
<choose>
<when condition="@(context.Response.StatusCode &gt;= 200 &amp;&amp; context.Response.StatusCode &lt; 300)">
<emit-metric name="Successful requests" value="1" namespace="apim-metrics">
<dimension name="API" value="@(context.Api.Name)" />
<dimension name="Client IP" value="@(context.Request.IpAddress)" />
<dimension name="Status Code" value="@((String)context.Response.StatusCode.ToString())" />
<dimension name="Status Reason" value="@(context.Response.StatusReason)" />
</emit-metric>
</when>
<when condition="@(context.Response.StatusCode &gt;= 400 &amp;&amp; context.Response.StatusCode &lt; 600)">
<emit-metric name="Failed requests" value="1" namespace="apim-metrics">
<dimension name="API" value="@(context.Api.Name)" />
<dimension name="Client IP" value="@(context.Request.IpAddress)" />
<dimension name="Status Code" value="@(context.Response.StatusCode.ToString())" />
<dimension name="Status Reason" value="@(context.Response.StatusReason)" />
<dimension name="Error Source" value="backend" />
</emit-metric>
</when>
</choose>
</outbound>
<on-error>
<base />
<emit-metric name="Failed requests" value="1" namespace="apim-metrics">
<dimension name="API" value="@(context.Api.Name)" />
<dimension name="Client IP" value="@(context.Request.IpAddress)" />
<dimension name="Status Code" value="500" />
<dimension name="Status Reason" value="@(context.LastError.Reason)" />
<dimension name="Error Source" value="gateway" />
</emit-metric>
</on-error>
</policies>

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

@ -0,0 +1,51 @@
# How To Run ApiManagement.Tests.ps1
`ApiManagement.Tests.ps1` contains examples of using the `Confirm-AzBPApiManagement`, `Confirm-AzBPApiManagementApi`,
`Confirm-AzBPApiManagementDiagnostic`, `Confirm-AzBPApiManagementLogger`, and `Confirm-AzBPApiManagementPolicy`
cmdlets.
## Pre-Requisites
- Follow the [setup instructions](../README.md)
## Steps
1. Navigate to ApiManagement directory:
```Powershell
cd examples\ApiManagement\
```
1. Deploy the Api Management Service to your resource group:
```Powershell
New-AzResourceGroupDeployment -ResourceGroupName "<your-resource-group-name>"`
-TemplateFile ".\ApiManagement.bicep"
```
1. Update `ApiManagement.Tests.ps1` variables to point to your expected resources:
- `rg-test` -> `your-resource-group-name`
- `servicetest` -> `your-api-management-service-name`
- `apitest` -> `your-api-management-api-name`
- `diagtest` -> `your-api-management-diagnostic-name`
- `loggertest` -> `your-api-managemetn-logger-name`
- `westus` -> `your-resource-group-location`
1. If using a local copy of `Az.InfrastructureTesting`, replace `Import-Module Az.InfrastructureTesting` with
`Import-Module "../../bin/BenchPress.Azure.psd1"`.
1. Run `ApiManagement.Tests.ps1`:
```Powershell
Invoke-Pester -Path .\ApiManagement.Tests.ps1
```
1. Success!
```Powershell
Tests completed in 20.02s
Tests Passed: 29, Failed: 0, Skipped: 0 NotRun: 0
```
1. Don't forget to delete any deployed resources that are no longer needed.

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

@ -88,6 +88,6 @@ Describe 'Verify App Insights' {
} }
AfterAll { AfterAll {
Get-Module Az-InfrastructureTesting | Remove-Module Get-Module Az.InfrastructureTesting | Remove-Module
Get-Module BenchPress.Azure | Remove-Module Get-Module BenchPress.Azure | Remove-Module
} }

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

@ -87,6 +87,6 @@ Describe 'Verify App Service Plan' {
} }
AfterAll { AfterAll {
Get-Module Az-InfrastructureTesting | Remove-Module Get-Module Az.InfrastructureTesting | Remove-Module
Get-Module BenchPress.Azure | Remove-Module Get-Module BenchPress.Azure | Remove-Module
} }

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

@ -23,6 +23,6 @@ Describe 'Spin up , Tear down Action Group' {
} }
AfterAll { AfterAll {
Get-Module Az-InfrastructureTesting | Remove-Module Get-Module Az.InfrastructureTesting | Remove-Module
Get-Module BenchPress.Azure | Remove-Module Get-Module BenchPress.Azure | Remove-Module
} }

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

@ -32,7 +32,7 @@ Describe 'Verify Resource Exists' {
} }
AfterAll { AfterAll {
Get-Module Az-InfrastructureTesting | Remove-Module Get-Module Az.InfrastructureTesting | Remove-Module
Get-Module BenchPress.Azure | Remove-Module Get-Module BenchPress.Azure | Remove-Module
} }

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

@ -88,6 +88,6 @@ Describe 'Verify Container Registry' {
} }
AfterAll { AfterAll {
Get-Module Az-InfrastructureTesting | Remove-Module Get-Module Az.InfrastructureTesting | Remove-Module
Get-Module BenchPress.Azure | Remove-Module Get-Module BenchPress.Azure | Remove-Module
} }

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

@ -631,6 +631,6 @@ Describe 'Comsos DB SQL Database' {
} }
AfterAll { AfterAll {
Get-Module Az-InfrastructureTesting | Remove-Module Get-Module Az.InfrastructureTesting | Remove-Module
Get-Module BenchPress.Azure | Remove-Module Get-Module BenchPress.Azure | Remove-Module
} }

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

@ -159,6 +159,6 @@ Describe 'Verify Data Factory Linked Service' {
} }
AfterAll { AfterAll {
Get-Module Az-InfrastructureTesting | Remove-Module Get-Module Az.InfrastructureTesting | Remove-Module
Get-Module BenchPress.Azure | Remove-Module Get-Module BenchPress.Azure | Remove-Module
} }

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

@ -333,6 +333,6 @@ Describe 'Verify EventHub Consumer Group' {
} }
AfterAll { AfterAll {
Get-Module Az-InfrastructureTesting | Remove-Module Get-Module Az.InfrastructureTesting | Remove-Module
Get-Module BenchPress.Azure | Remove-Module Get-Module BenchPress.Azure | Remove-Module
} }

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

@ -123,6 +123,6 @@ Describe 'Verify KeyVault' {
} }
AfterAll { AfterAll {
Get-Module Az-InfrastructureTesting | Remove-Module Get-Module Az.InfrastructureTesting | Remove-Module
Get-Module BenchPress.Azure | Remove-Module Get-Module BenchPress.Azure | Remove-Module
} }

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

@ -93,6 +93,6 @@ Describe 'Verify Operational Insights Workspace Exists' {
} }
AfterAll { AfterAll {
Get-Module Az-InfrastructureTesting | Remove-Module Get-Module Az.InfrastructureTesting | Remove-Module
Get-Module BenchPress.Azure | Remove-Module Get-Module BenchPress.Azure | Remove-Module
} }

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

@ -78,6 +78,6 @@ Describe 'Verify Resource Group Exists' {
} }
AfterAll { AfterAll {
Get-Module Az-InfrastructureTesting | Remove-Module Get-Module Az.InfrastructureTesting | Remove-Module
Get-Module BenchPress.Azure | Remove-Module Get-Module BenchPress.Azure | Remove-Module
} }

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

@ -92,6 +92,6 @@ Describe 'Verify Sql Database' {
} }
AfterAll { AfterAll {
Get-Module Az-InfrastructureTesting | Remove-Module Get-Module Az.InfrastructureTesting | Remove-Module
Get-Module BenchPress.Azure | Remove-Module Get-Module BenchPress.Azure | Remove-Module
} }

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

@ -89,6 +89,6 @@ Describe 'Verify Sql Server' {
} }
AfterAll { AfterAll {
Get-Module Az-InfrastructureTesting | Remove-Module Get-Module Az.InfrastructureTesting | Remove-Module
Get-Module BenchPress.Azure | Remove-Module Get-Module BenchPress.Azure | Remove-Module
} }

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

@ -178,6 +178,6 @@ Describe 'Verify Storage Container' {
} }
AfterAll { AfterAll {
Get-Module Az-InfrastructureTesting | Remove-Module Get-Module Az.InfrastructureTesting | Remove-Module
Get-Module BenchPress.Azure | Remove-Module Get-Module BenchPress.Azure | Remove-Module
} }

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

@ -409,6 +409,6 @@ Describe 'Stream Analytics Jobs' {
} }
AfterAll { AfterAll {
Get-Module Az-InfrastructureTesting | Remove-Module Get-Module Az.InfrastructureTesting | Remove-Module
Get-Module BenchPress.Azure | Remove-Module Get-Module BenchPress.Azure | Remove-Module
} }

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

@ -288,6 +288,6 @@ Describe 'Verify Synapse Spark/SQL Pool' {
} }
AfterAll { AfterAll {
Get-Module Az-InfrastructureTesting | Remove-Module Get-Module Az.InfrastructureTesting | Remove-Module
Get-Module BenchPress.Azure | Remove-Module Get-Module BenchPress.Azure | Remove-Module
} }

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

@ -95,6 +95,6 @@ Describe 'Verify Virtual Machine' {
} }
AfterAll { AfterAll {
Get-Module Az-InfrastructureTesting | Remove-Module Get-Module Az.InfrastructureTesting | Remove-Module
Get-Module BenchPress.Azure | Remove-Module Get-Module BenchPress.Azure | Remove-Module
} }

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

@ -88,6 +88,6 @@ Describe 'Verify Web App Exists' {
} }
AfterAll { AfterAll {
Get-Module Az-InfrastructureTesting | Remove-Module Get-Module Az.InfrastructureTesting | Remove-Module
Get-Module BenchPress.Azure | Remove-Module Get-Module BenchPress.Azure | Remove-Module
} }