Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Provide more graceful handling for APIM default cleanup #225

Merged
merged 3 commits into from
Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,21 @@ $starterResult = Remove-AzApiManagementProduct -Context $apimContext -ProductId
Write-Host "Removing Unlimited product..."
$unlimitedResult = Remove-AzApiManagementProduct -Context $apimContext -ProductId 'unlimited' -DeleteSubscriptions

if ($apiResult -and $starterResult -and $unlimitedResult) {
Write-Host "Successfully removed the 'echo-api' API, 'starter' Product and 'unlimited' Product"
} else {
$message = "Failed to remove API Management defaults"
if (-not $apiResult) {
$message += [System.Environment]::NewLine + "> Failed to remove the 'echo' API"
}
if (-not $starterResult) {
$message += [System.Environment]::NewLine + "> Failed to remove the 'starter' Product"
}
if (-not $unlimitedResult) {
$message += [System.Environment]::NewLine + "> Failed to remove the 'unlimited' Product"
}
$message = $null

if ($null -ne $apiResult) {
$message += [System.Environment]::NewLine + "> Failed to remove the 'echo' API"
}
if ($null -ne $starterResult) {
$message += [System.Environment]::NewLine + "> Failed to remove the 'starter' Product"
}
if ($null -ne $unlimitedResult) {
$message += [System.Environment]::NewLine + "> Failed to remove the 'unlimited' Product"
}
stijnmoreels marked this conversation as resolved.
Show resolved Hide resolved

if($null -eq $message){
Write-Host "Successfully removed the 'echo-api' API, 'starter' Product and 'unlimited' Product"
}else{
Write-Error $message
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

InModuleScope Arcus.Scripting.ApiManagement {
Describe "Arcus Azure API Management unit tests" {
Context "Create Azure API Management backup" {
Context "Back up Azure API Management service" {
It "Creates storage context during API management backup" {
# Arrange
$resourceGroup = "shopping"
Expand Down Expand Up @@ -178,7 +178,7 @@ InModuleScope Arcus.Scripting.ApiManagement {
Assert-MockCalled Backup-AzApiManagement -Times 1
}
}
Context "Create Azure API Management API operation" {
Context "Import Azure API Management operation" {
It "Calls new operation on Azure API Management operation w/o policy" {
# Arrange
$resourceGroup = "shopping"
Expand Down Expand Up @@ -248,8 +248,8 @@ InModuleScope Arcus.Scripting.ApiManagement {
Assert-MockCalled New-AzApiManagementOperation -Times 1
Assert-MockCalled Set-AzApiManagementPolicy -Times 1
}
}
Context "Import Azure API Management product policy" {
}
Context "Import Azure API Management product policy" {
It "Importing policy product sets Azure API Management policy on operation" {
# Arrange
$resourceGroup = "shopping"
Expand Down Expand Up @@ -299,7 +299,7 @@ InModuleScope Arcus.Scripting.ApiManagement {
# Assert
Should -Throw
}
}
}
Context "Remove Azure API Management defaults" {
It "Remove API Management defaults succeed" {
# Arrange
Expand All @@ -310,15 +310,15 @@ InModuleScope Arcus.Scripting.ApiManagement {
Mock Remove-AzApiManagementApi {
$Context | Should -Be $context
$ApiId | Should -Be "echo-api"
return $true } -Verifiable
return $null } -Verifiable
Mock Remove-AzApiManagementProduct {
$Context | Should -Be $context
$DeleteSubscriptions | Should -Be $true
return $true } -Verifiable -ParameterFilter { $ProductId -eq "starter" }
return $null } -Verifiable -ParameterFilter { $ProductId -eq "starter" }
Mock Remove-AzApiManagementProduct {
$Context | Should -Be $context
$DeleteSubscriptions | Should -Be $true
return $true } -Verifiable -ParameterFilter { $ProductId -eq "unlimited" }
return $null } -Verifiable -ParameterFilter { $ProductId -eq "unlimited" }

# Act
Remove-AzApiManagementDefaults -ResourceGroupName $resourceGroup -ServiceName $serviceName
Expand All @@ -342,11 +342,11 @@ InModuleScope Arcus.Scripting.ApiManagement {
Mock Remove-AzApiManagementProduct {
$Context | Should -Be $context
$DeleteSubscriptions | Should -Be $true
return $true } -Verifiable -ParameterFilter { $ProductId -eq "starter" }
return $null } -Verifiable -ParameterFilter { $ProductId -eq "starter" }
Mock Remove-AzApiManagementProduct {
$Context | Should -Be $context
$DeleteSubscriptions | Should -Be $true
return $true } -Verifiable -ParameterFilter { $ProductId -eq "unlimited" }
return $null } -Verifiable -ParameterFilter { $ProductId -eq "unlimited" }

# Act
{ Remove-AzApiManagementDefaults -ResourceGroupName $resourceGroup -ServiceName $serviceName } |
Expand All @@ -367,15 +367,15 @@ InModuleScope Arcus.Scripting.ApiManagement {
Mock Remove-AzApiManagementApi {
$Context | Should -Be $context
$ApiId | Should -Be "echo-api"
return $true } -Verifiable
return $null } -Verifiable
Mock Remove-AzApiManagementProduct {
$Context | Should -Be $context
$DeleteSubscriptions | Should -Be $true
return $false } -Verifiable -ParameterFilter { $ProductId -eq "starter" }
Mock Remove-AzApiManagementProduct {
$Context | Should -Be $context
$DeleteSubscriptions | Should -Be $true
return $true } -Verifiable -ParameterFilter { $ProductId -eq "unlimited" }
return $null } -Verifiable -ParameterFilter { $ProductId -eq "unlimited" }

# Act
{ Remove-AzApiManagementDefaults -ResourceGroupName $resourceGroup -ServiceName $serviceName } |
Expand All @@ -396,11 +396,11 @@ InModuleScope Arcus.Scripting.ApiManagement {
Mock Remove-AzApiManagementApi {
$Context | Should -Be $context
$ApiId | Should -Be "echo-api"
return $true } -Verifiable
return $null } -Verifiable
Mock Remove-AzApiManagementProduct {
$Context | Should -Be $context
$DeleteSubscriptions | Should -Be $true
return $true } -Verifiable -ParameterFilter { $ProductId -eq "starter" }
return $null } -Verifiable -ParameterFilter { $ProductId -eq "starter" }
Mock Remove-AzApiManagementProduct {
$Context | Should -Be $context
$DeleteSubscriptions | Should -Be $true
Expand All @@ -416,7 +416,7 @@ InModuleScope Arcus.Scripting.ApiManagement {
Assert-MockCalled Remove-AzApiManagementProduct -Times 1 -ParameterFilter { $ProductId -eq "starter" }
Assert-MockCalled Remove-AzApiManagementProduct -Times 1 -ParameterFilter { $ProductId -eq "unlimited" }
}
}
}
Context "Import Azure API Management API policy" {
It "Importing policy API sets API Management policy on operation" {
# Arrange
Expand Down Expand Up @@ -469,7 +469,7 @@ InModuleScope Arcus.Scripting.ApiManagement {
Assert-MockCalled New-AzApiManagementContext -Times 1
Assert-MockCalled Set-AzApiManagementPolicy -Times 1
}
}
}
Context "Import Azure API Management operation policy" {
It "Importing policy operation sets Azure API Management policy on operation" {
# Arrange
Expand Down Expand Up @@ -705,34 +705,34 @@ InModuleScope Arcus.Scripting.ApiManagement {
Assert-MockCalled Restore-AzApiManagement -Times 1
}
}
Context "Set Azure API Management subscription key" {
It "Sets subscription keys on an API in Azure API Management" {
# Arrange
$resourceGroup = "shopping"
$serviceName = "shopping-API-management"
$apiId = "shopping-API"
$apiKeyHeaderName = "header-name"
$apiKeyQueryParamName = "query-param-name"
$context = New-Object -TypeName Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Models.PsApiManagementContext

Mock New-AzApiManagementContext {
$ResourceGroupName | Should -Be $resourceGroup
$ServiceName | Should -Be $serviceName
return $context } -Verifiable

Mock Set-AzApiManagementApi {
$Context | Should -be $context
$ApiId | Should -Be $apiId
$SubscriptionKeyHeaderName | Should -Be $apiKeyHeaderName
$SubscriptionKeyQueryParamName | Should -Be $apiKeyQueryParamName } -Verifiable

# Act
Set-AzApiManagementApiSubscriptionKey -ResourceGroupName $resourceGroup -ServiceName $serviceName -ApiId $apiId -HeaderName $apiKeyHeaderName -QueryParamName $apiKeyQueryParamName

# Assert
Assert-VerifiableMock
}
}
Context "Set Azure API Management API subscription key" {
It "Sets subscription keys on an API in Azure API Management" {
# Arrange
$resourceGroup = "shopping"
$serviceName = "shopping-API-management"
$apiId = "shopping-API"
$apiKeyHeaderName = "header-name"
$apiKeyQueryParamName = "query-param-name"
$context = New-Object -TypeName Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Models.PsApiManagementContext

Mock New-AzApiManagementContext {
$ResourceGroupName | Should -Be $resourceGroup
$ServiceName | Should -Be $serviceName
return $context } -Verifiable

Mock Set-AzApiManagementApi {
$Context | Should -be $context
$ApiId | Should -Be $apiId
$SubscriptionKeyHeaderName | Should -Be $apiKeyHeaderName
$SubscriptionKeyQueryParamName | Should -Be $apiKeyQueryParamName } -Verifiable

# Act
Set-AzApiManagementApiSubscriptionKey -ResourceGroupName $resourceGroup -ServiceName $serviceName -ApiId $apiId -HeaderName $apiKeyHeaderName -QueryParamName $apiKeyQueryParamName

# Assert
Assert-VerifiableMock
}
}
Context "Upload Azure API Management certificate" {
It "Uploads private certificate to API Management" {
# Arrange
Expand Down