From ab305418cfede0d1921eb5ecd1be109564d7cf60 Mon Sep 17 00:00:00 2001 From: stijnmoreels <9039753+stijnmoreels@users.noreply.github.com> Date: Fri, 10 Jul 2020 09:06:39 +0200 Subject: [PATCH] Feature - remove API Management defaults script (#51) Co-authored-by: Tom Kerkhove --- .../powershell/azure-api-management.md | 50 ++++--- .../Arcus.Scripting.ApiManagement.psd1 | Bin 2806 -> 5676 bytes .../Arcus.Scripting.ApiManagement.psm1 | 47 +++++-- .../Arcus.Scripting.ApiManagement.pssproj | 1 + .../Remove-AzApiManagementDefaults.ps1 | 35 +++++ .../Arcus.Scripting.DevOps.psd1 | Bin 5348 -> 5370 bytes .../Arcus.Scripting.ApiManagement.tests.ps1 | 123 +++++++++++++++++- 7 files changed, 223 insertions(+), 33 deletions(-) create mode 100644 src/Arcus.Scripting.ApiManagement/Scripts/Remove-AzApiManagementDefaults.ps1 diff --git a/docs/preview/features/powershell/azure-api-management.md b/docs/preview/features/powershell/azure-api-management.md index 248ca9fd..fb7c3e24 100644 --- a/docs/preview/features/powershell/azure-api-management.md +++ b/docs/preview/features/powershell/azure-api-management.md @@ -20,10 +20,10 @@ Create an operation on an existing API in Azure API Management. | Parameter | Mandatory | Description | | ------------------- | --------- | -------------------------------------------------------------------------------------------------------- | | `ResourceGroupName` | yes | The resource group containing the API Management service | -| `ServiceName` | yes | The name of the API Management service located in Azure | -| `ApiId` | yes | The ID to identify the API running in API Management | +| `ServiceName` | yes | The name of the Azure API Management instance located in Azure | +| `ApiId` | yes | The ID to identify the API running in Azure API Management | | `OperationId` | yes | The ID to identify the to-be-created operation on the API | -| `Method` | yes | The method of the to-be-created operation on the API | +| `Method` | yes | The method of the to-be-created operation on the API | | `UrlTemplate` | yes | The URL-template, or endpoint-URL, of the to-be-created operation on the API | | `OperationName` | no | The optional descriptive name to give to the to-be-created operation on the API (default: `OperationId`) | | `Description` | no | The optional explanation to describe the to-be-created operation on the API | @@ -45,16 +45,32 @@ PS> Create-AzApiManagementApiOperation -ResourceGroup $ResourceGroup -ServiceNam # New API operation '$OperationName' on API Management service was added. ``` -## Import policy to an API in the API Management service +## Remove all Azure API Management defaults from the service + +Remove all default API's and products from the Azure API Management service ('echo-api' API, 'starter' & 'unlimited' products), including the subscriptions. + +| Parameter | Mandatory | Description | +| --------------- | --------- | -------------------------------------------------------- | +| `ResourceGroup` | yes | The resource group containing the Azure API Management service | +| `ServiceName` | yes | The name of the Azure API Management instance located in Azure | + +```powershell +PS> Remove-AzApiManagementDefaults -ResourceGroup $ResourceGroup -ServiceName $ServiceName +# Removing Echo Api... +# Removing Starter product... +# Removing Unlimited product... +``` + +## Import policy to an API in the Azure API Management service Imports a base-policy from a file to an API in Azure API Management. -| Parameter | Mandatory | Description | -| ------------------- | --------- | -------------------------------------------------------------------------------------------------------- | -| `ResourceGroupName` | yes | The resource group containing the API Management service | -| `ServiceName` | yes | The name of the API Management service located in Azure | -| `ApiId` | yes | The ID to identify the API running in API Management | -| `PolicyFilePath` | yes | The path to the file containing the to-be-imported policy | +| Parameter | Mandatory | Description | +| ---------------- | --------- | --------------------------------------------------------- | +| `ResourceGroup` | yes | The resource group containing the Azure API Management service | +| `ServiceName` | yes | The name of the Azure API Management instance | +| `ApiId` | yes | The ID to identify the API running in Azure API Management | +| `PolicyFilePath` | yes | The path to the file containing the to-be-imported policy | ```powershell PS> Import-AzApiManagementApiPolicy -ResourceGroup $ResourceGroup -ServiceName $ServiceName -ApiId $ApiId -PolicyFilePath $PolicyFilePath @@ -64,13 +80,13 @@ PS> Import-AzApiManagementApiPolicy -ResourceGroup $ResourceGroup -ServiceName $ ## Import policy to an operation in the API Management service Imports a policy from a file to an API operation in Azure API Management. -| Parameter | Mandatory | Description | -| ------------------- | --------- | -------------------------------------------------------------------------------------------------------- | -| `ResourceGroupName` | yes | The resource group containing the API Management service | -| `ServiceName` | yes | The name of the API Management service located in Azure | -| `ApiId` | yes | The ID to identify the API running in API Management | -| `OperationId` | yes | The ID to identify the operation for which to import the policy | -| `PolicyFilePath` | yes | The path to the file containing the to-be-imported policy | +| Parameter | Mandatory | Description | +| ---------------- | --------- | --------------------------------------------------------------- | +| `ResourceGroup` | yes | The resource group containing the Azure API Management instance | +| `ServiceName` | yes | The name of the Azure API Management service instance | +| `ApiId` | yes | The ID to identify the API running in Azure API Management | +| `OperationId` | yes | The ID to identify the operation for which to import the policy | +| `PolicyFilePath` | yes | The path to the file containing the to-be-imported policy | ```powershell PS> Import-AzApiManagementOperationPolicy -ResourceGroup $ResourceGroup -ServiceName $ServiceName -ApiId $ApiId -OperationId $OperationId -PolicyFilePath $PolicyFilePath diff --git a/src/Arcus.Scripting.ApiManagement/Arcus.Scripting.ApiManagement.psd1 b/src/Arcus.Scripting.ApiManagement/Arcus.Scripting.ApiManagement.psd1 index bcc54941c82204e0e1a7b39f7b38da09031081a0..1286263c37975d23c8f7d06189a475bec9270543 100644 GIT binary patch literal 5676 zcmc(jT~8ZF6o$`rrTzy?zA&wdlTb>VNR?tiNRUE;NSgN6*oIog*fl03t@78~zRw&F zGqYa6e5pdLcXwvayyxpZXZGKJn)bDQWld|?u8pm4XLf7@J&mkuCpNT0J)PQ2Zw@s+ z4I}xymS(2s)?8YY`BJM-^gorfnST3Pe_)^OnSMKZ+tYrhORO173Tu9_N4929!}YQ5 z9_!r`U7zW?rt6w*1j)OZOt3t+zr&k|>z#I`$oN5*JZ0P??vkt;r^ggU0PC7f<9A#COV{XdMdqUK+ zKXe6C+sc_=6l*1_9oy2r#DT}hOh|Yp`u^1KK(@vQLq(74M^y4Aw{F6UwlD1$bGeRd zT`M*eJx`_WkM_N->TV$W40N}y`1wg=tGa^VvF^Hh?#^edYh_Ps*7c@D-Vr)5cwSs( zKQNWEt3ECzZ%XD!UL*(6)OYwOL?=SH+t9@8KHk+%9^W1NOPJ6O&G!_o zFfjfubGg)ZS18Ggk=Bm10-H=_pL(r-4YP@tbIoaJ7w%Z`PCh2Dus1Pv6lQ`2L}cAC z+%02`<|UHLyswYD6qTjFmP1bFd2PBs)UW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kids$X8P3G`0VM?Gd)4S97Z;Om`Rvx>ac&xieE; z8KM6Ry1)NWL?&Uil function Create-AzApiManagementApiOperation { - param( + param( [string][Parameter(Mandatory = $true)] $ResourceGroup = $(throw "Resource group is required"), - [string][Parameter(Mandatory = $true)] $ServiceName = $(throw "API management service name is required"), + [string][Parameter(Mandatory = $true)] $ServiceName = $(throw "Service name is required"), [string][Parameter(Mandatory = $true)] $ApiId = $(throw "API ID is required"), [string][Parameter(Mandatory = $true)] $OperationId = $(throw "Operation ID is required"), [string][Parameter(Mandatory = $true)] $Method = $(throw "Method is required"), @@ -43,12 +46,36 @@ function Create-AzApiManagementApiOperation { [string][Parameter(Mandatory = $false)] $OperationName = $OperationId, [string][Parameter(Mandatory = $false)] $Description = "", [string][Parameter(Mandatory = $false)] $PolicyFilePath = "" - ) - . $PSScriptRoot\Scripts\Create-AzApiManagementApiOperation.ps1 -ResourceGroup $ResourceGroup -ServiceName $ServiceName -ApiId $ApiId -OperationId $OperationId -Method $Method -UrlTemplate $UrlTemplate -OperationName $OperationName -Description $Description -PolicyFilePath $PolicyFilePath + ) + . $PSScriptRoot\Scripts\Create-AzApiManagementApiOperation.ps1 -ResourceGroup $ResourceGroup -ServiceName $ServiceName -ApiId $ApiId -OperationId $OperationId -Method $Method -UrlTemplate $UrlTemplate -OperationName $OperationName -Description $Description -PolicyFilePath $PolicyFilePath } Export-ModuleMember -Function Create-AzApiManagementApiOperation +<# + .Synopsis + Remove all defaults from the API Management service. + + .Description + Remove all default API's and products from an Azure API Management instance ('echo-api' API, 'starter' & 'unlimited' products), including the subscriptions. + + .Parameter ResourceGroup + The resource group containing the Azure API Management instance. + + .Parameter ServiceName + The name of the Azure API Management instance. +#> +function Remove-AzApiManagementDefaults { + param( + [string][Parameter(Mandatory = $true)] $ResourceGroup = $(throw "Resource group is required"), + [string][Parameter(Mandatory = $true)] $ServiceName = $(throw "Service name is required") + ) + +. $PSScriptRoot\Scripts\Remove-AzApiManagementDefaults.ps1 -ResourceGroup $ResourceGroup -ServiceName $ServiceName +} + +Export-ModuleMember -Function Remove-AzApiManagementDefaults + <# .Synopsis Import a policy to an API in Azure API Management. @@ -83,17 +110,11 @@ Export-ModuleMember -Function Import-AzApiManagementApiPolicy <# .Synopsis - Imports a policy to an operation in Azure API Management. + Imports a policy to an operation in Azure API Management. .Description Imports a policy from a file to an API operation in Azure API Management. - .Parameter ResourceGroup - The resource group containing the API Management service. - - .Parameter ServiceName - The name of the API Management service located in Azure. - .Parameter ApiId The ID to identify the API running in API Management. diff --git a/src/Arcus.Scripting.ApiManagement/Arcus.Scripting.ApiManagement.pssproj b/src/Arcus.Scripting.ApiManagement/Arcus.Scripting.ApiManagement.pssproj index 2d3b3fcd..c875b847 100644 --- a/src/Arcus.Scripting.ApiManagement/Arcus.Scripting.ApiManagement.pssproj +++ b/src/Arcus.Scripting.ApiManagement/Arcus.Scripting.ApiManagement.pssproj @@ -32,6 +32,7 @@ + diff --git a/src/Arcus.Scripting.ApiManagement/Scripts/Remove-AzApiManagementDefaults.ps1 b/src/Arcus.Scripting.ApiManagement/Scripts/Remove-AzApiManagementDefaults.ps1 new file mode 100644 index 00000000..77d59475 --- /dev/null +++ b/src/Arcus.Scripting.ApiManagement/Scripts/Remove-AzApiManagementDefaults.ps1 @@ -0,0 +1,35 @@ +param( + [string][parameter(Mandatory = $true)] $ResourceGroup, + [string][parameter(Mandatory = $true)] $ServiceName +) + +Write-Host "Start removing Azure API Management defaults..." +$apimContext = New-AzApiManagementContext -ResourceGroupName $ResourceGroup -ServiceName $ServiceName + +Write-Host "Removing Echo Api..." +$apiResult = Remove-AzApiManagementApi -Context $apimContext -ApiId 'echo-api' + +Write-Host "Removing Starter product..." +$starterResult = Remove-AzApiManagementProduct -Context $apimContext -ProductId 'starter' -DeleteSubscriptions + +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" + } + + Write-Error $message +} + +Write-Host "Finished removing Azure API Management defaults!" diff --git a/src/Arcus.Scripting.DevOps/Arcus.Scripting.DevOps.psd1 b/src/Arcus.Scripting.DevOps/Arcus.Scripting.DevOps.psd1 index b293956a97152d6e37841f37ad23f765a8795729..99050fc3f90d2c290b8dd8141930e2ebd6bb396a 100644 GIT binary patch delta 62 zcmaE&`Ac)dDSkr-1`P&vhG2$Nh7tx{21kY}1{WZ^jKQCwfT5Tnj3JSsh#?b5CNbmy O#nl-!H$Ub7&jJAE01p2E delta 40 tcmeyR`9yQWDSl~n26u*3h7tx{1~-OMhCGI3AUl&GAIMVQ{G0zj3jp!+3w8hi diff --git a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.ApiManagement.tests.ps1 b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.ApiManagement.tests.ps1 index 948f3e87..66f3fe4a 100644 --- a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.ApiManagement.tests.ps1 +++ b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.ApiManagement.tests.ps1 @@ -35,7 +35,7 @@ Describe "Arcus" { Assert-MockCalled New-AzApiManagementOperation -Times 1 Assert-MockCalled Set-AzApiManagementPolicy -Times 0 } - It "Calls new operation on API management operation w/ policy" { + It "Calls new operation on API Management operation w/ policy" { # Arrange $resourceGroup = "shopping" $serviceName = "shopping-API-management" @@ -73,6 +73,121 @@ Describe "Arcus" { Assert-MockCalled New-AzApiManagementOperation -Times 1 Assert-MockCalled Set-AzApiManagementPolicy -Times 1 } + It "Remove API Management defaults succeed" { + # Arrange + $resourceGroup = "shopping" + $serviceName = "shopping-API-management" + $context = New-Object -TypeName Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Models.PsApiManagementContext + + Mock Remove-AzApiManagementApi { + $Context | Should -Be $context + $ApiId | Should -Be "echo-api" + return $true } -Verifiable + Mock Remove-AzApiManagementProduct { + $Context | Should -Be $context + $DeleteSubscriptions | Should -Be $true + return $true } -Verifiable -ParameterFilter { $ProductId -eq "starter" } + Mock Remove-AzApiManagementProduct { + $Context | Should -Be $context + $DeleteSubscriptions | Should -Be $true + return $true } -Verifiable -ParameterFilter { $ProductId -eq "unlimited" } + + # Act + Remove-AzApiManagementDefaults -ResourceGroup $resourceGroup -ServiceName $serviceName + + # Assert + Assert-VerifiableMock + Assert-MockCalled Remove-AzApiManagementApi -Times 1 + Assert-MockCalled Remove-AzApiManagementProduct -Times 1 -ParameterFilter { $ProductId -eq "starter" } + Assert-MockCalled Remove-AzApiManagementProduct -Times 1 -ParameterFilter { $ProductId -eq "unlimited" } + } + It "Remove API Management defaults when echo-api API failed to remove, throws" { + # Arrange + $resourceGroup = "shopping" + $serviceName = "shopping-API-management" + $context = New-Object -TypeName Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Models.PsApiManagementContext + + Mock Remove-AzApiManagementApi { + $Context | Should -Be $context + $ApiId | Should -Be "echo-api" + return $false } -Verifiable + Mock Remove-AzApiManagementProduct { + $Context | Should -Be $context + $DeleteSubscriptions | Should -Be $true + return $true } -Verifiable -ParameterFilter { $ProductId -eq "starter" } + Mock Remove-AzApiManagementProduct { + $Context | Should -Be $context + $DeleteSubscriptions | Should -Be $true + return $true } -Verifiable -ParameterFilter { $ProductId -eq "unlimited" } + + # Act + { Remove-AzApiManagementDefaults -ResourceGroup $resourceGroup -ServiceName $serviceName } | + Should -Throw + + # Assert + Assert-VerifiableMock + Assert-MockCalled Remove-AzApiManagementApi -Times 1 + Assert-MockCalled Remove-AzApiManagementProduct -Times 1 -ParameterFilter { $ProductId -eq "starter" } + Assert-MockCalled Remove-AzApiManagementProduct -Times 1 -ParameterFilter { $ProductId -eq "unlimited" } + } + It "Remove API Management defaults when starter product failed to remove, throws" { + # Arrange + $resourceGroup = "shopping" + $serviceName = "shopping-API-management" + $context = New-Object -TypeName Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Models.PsApiManagementContext + + Mock Remove-AzApiManagementApi { + $Context | Should -Be $context + $ApiId | Should -Be "echo-api" + return $true } -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" } + + # Act + { Remove-AzApiManagementDefaults -ResourceGroup $resourceGroup -ServiceName $serviceName } | + Should -Throw + + # Assert + Assert-VerifiableMock + Assert-MockCalled Remove-AzApiManagementApi -Times 1 + Assert-MockCalled Remove-AzApiManagementProduct -Times 1 -ParameterFilter { $ProductId -eq "starter" } + Assert-MockCalled Remove-AzApiManagementProduct -Times 1 -ParameterFilter { $ProductId -eq "unlimited" } + } + It "Remove API Management defaults when unlimited product failed to remove, throws" { + # Arrange + $resourceGroup = "shopping" + $serviceName = "shopping-API-management" + $context = New-Object -TypeName Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Models.PsApiManagementContext + + Mock Remove-AzApiManagementApi { + $Context | Should -Be $context + $ApiId | Should -Be "echo-api" + return $true } -Verifiable + Mock Remove-AzApiManagementProduct { + $Context | Should -Be $context + $DeleteSubscriptions | Should -Be $true + return $true } -Verifiable -ParameterFilter { $ProductId -eq "starter" } + Mock Remove-AzApiManagementProduct { + $Context | Should -Be $context + $DeleteSubscriptions | Should -Be $true + return $false } -Verifiable -ParameterFilter { $ProductId -eq "unlimited" } + + # Act + { Remove-AzApiManagementDefaults -ResourceGroup $resourceGroup -ServiceName $serviceName } | + Should -Throw + + # Assert + Assert-VerifiableMock + Assert-MockCalled Remove-AzApiManagementApi -Times 1 + Assert-MockCalled Remove-AzApiManagementProduct -Times 1 -ParameterFilter { $ProductId -eq "starter" } + Assert-MockCalled Remove-AzApiManagementProduct -Times 1 -ParameterFilter { $ProductId -eq "unlimited" } + } It "Importing policy API sets API Management policy on operation" { # Arrange $resourceGroup = "shopping" @@ -121,6 +236,8 @@ Describe "Arcus" { # Assert Assert-VerifiableMock + Assert-MockCalled New-AzApiManagementContext -Times 1 + Assert-MockCalled Set-AzApiManagementPolicy -Times 1 } It "Importing policy operation sets API Management policy on operation" { # Arrange @@ -128,7 +245,6 @@ Describe "Arcus" { $serviceName = "shopping-API-management" $apiId = "shopping-API" $operationId = "orders" - $policyFilePath = "/file-path/operation-policy" $context = New-Object -TypeName Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Models.PsApiManagementContext @@ -143,8 +259,9 @@ Describe "Arcus" { $PolicyFilePath | Should -Be $policyFilePath return $true } -Verifiable + # Act Import-AzApiManagementOperationPolicy -ResourceGroup $resourceGroup -ServiceName $serviceName -ApiId $apiId -OperationId $operationId -PolicyFilePath $policyFilePath - + # Assert Assert-VerifiableMock }