Skip to content

Commit

Permalink
Feature - remove API Management defaults script (#51)
Browse files Browse the repository at this point in the history
Co-authored-by: Tom Kerkhove <[email protected]>
  • Loading branch information
stijnmoreels and tomkerkhove authored Jul 10, 2020
1 parent 363abaa commit ab30541
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 33 deletions.
50 changes: 33 additions & 17 deletions docs/preview/features/powershell/azure-api-management.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand All @@ -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
Expand All @@ -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
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
.Description
Create an operation on an existing API 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 ResourceGroup
The resource group containing the API Management service.
.Parameter ServiceName
The name of the Azure API Management instance.
.Parameter ApiId
The ID to identify the API running in API Management.
Expand All @@ -33,22 +36,46 @@
The path to the file containing the optional policy of the to-be-created operation on the API.
#>
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"),
[string][Parameter(Mandatory = $true)] $UrlTemplate = $(throw "URL template is required"),
[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.
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<Compile Include="Arcus.Scripting.ApiManagement.psd1" />
<Compile Include="Arcus.Scripting.ApiManagement.psm1" />
<Compile Include="Scripts\Create-AzApiManagementApiOperation.ps1" />
<Compile Include="Scripts\Remove-AzApiManagementDefaults.ps1" />
<Compile Include="Scripts\Import-AzApiManagementApiPolicy.ps1" />
<Compile Include="Scripts\Import-AzApiManagementOperationPolicy.ps1" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -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!"
Binary file modified src/Arcus.Scripting.DevOps/Arcus.Scripting.DevOps.psd1
Binary file not shown.
Loading

0 comments on commit ab30541

Please sign in to comment.