generated from arcus-azure/arcus.github.template
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature - create new API operation on API Management service script (#50
) * Feature - create new API operation on API Management service script * pr-sug: add '-DisableNameChecking' during importing module * pr-sug: fix typo in api management * pr-fix: use concrete type as return type * Update Arcus.Scripting.ApiManagement.tests.ps1 * Update Arcus.Scripting.ApiManagement.tests.ps1 * Update Arcus.Scripting.ApiManagement.tests.ps1 * Update docs/preview/features/powershell/azure-api-management.md Co-authored-by: Maxim Braekman <[email protected]> * Update src/Arcus.Scripting.ApiManagement/Arcus.Scripting.ApiManagement.psm1 Co-authored-by: Maxim Braekman <[email protected]> * Update docs/preview/features/powershell/azure-api-management.md Co-authored-by: Maxim Braekman <[email protected]> * Update src/Arcus.Scripting.ApiManagement/Arcus.Scripting.ApiManagement.psm1 Co-authored-by: Maxim Braekman <[email protected]> * pr-sug: add with using module * pr-sug: update with context assertion Co-authored-by: Maxim Braekman <[email protected]>
- Loading branch information
1 parent
7653312
commit 0c8fb0c
Showing
8 changed files
with
206 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
--- | ||
title: "Scripts related to interacting with Azure API Management" | ||
layout: default | ||
--- | ||
|
||
# Azure API Management | ||
|
||
## Installation | ||
|
||
To have access to the following features, you have to import the module: | ||
|
||
```powershell | ||
PS> Import-Module -Name Arcus.Scripting.ApiManagement | ||
``` | ||
|
||
## Create a new API operation on the API Management service | ||
|
||
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 | | ||
| `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 | | ||
| `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 | | ||
| `PolicyFilePath` | no | The path to the file containing the optional policy of the to-be-created operation on the API | | ||
|
||
**Example** | ||
|
||
Creates a new API operation on the API Management service with using the default base operation policy. | ||
|
||
```powershell | ||
PS> Create-AzApiManagementApiOperation -ResourceGroup $ResourceGroup -ServiceName $ServiceName -ApiId $ApiId -OperationId $OperationId -Method $Method -UrlTemplate $UrlTemplate | ||
# New API operation '$OperationName' on API Management service was added. | ||
``` | ||
|
||
Creates a new API operation on the API Management service with using a specific operation policy. | ||
|
||
```powershell | ||
PS> Create-AzApiManagementApiOperation -ResourceGroup $ResourceGroup -ServiceName $ServiceName -ApiId $ApiId -OperationId $OperationId -Method $Method -UrlTemplate $UrlTemplate -OperationName $OperationName -Description $Description -PolicyFilePath $PolicyFilePath | ||
# New API operation '$OperationName' on API Management service was added. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+46 Bytes
(100%)
src/Arcus.Scripting.ApiManagement/Arcus.Scripting.ApiManagement.psd1
Binary file not shown.
50 changes: 47 additions & 3 deletions
50
src/Arcus.Scripting.ApiManagement/Arcus.Scripting.ApiManagement.psm1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,50 @@ | ||
<# | ||
My Function | ||
.Synopsis | ||
Create an operation on an API in Azure API Management. | ||
.Description | ||
Create an operation on an existing API in Azure API Management. | ||
.Parameter ServiceName | ||
The name of the API Management service located in Azure. | ||
.Parameter ResourceGroupName | ||
The resource group containing the API Management service. | ||
.Parameter ApiId | ||
The ID to identify the API running in API Management. | ||
.Parameter OperationId | ||
The ID to identify the to-be-created operation on the API. | ||
.Parameter Method | ||
The method of the to-be-created operation on the API. | ||
.Parameter UrlTemplate | ||
The URL-template, or endpoint-URL, of the to-be-created operation on the API. | ||
.Parameter OperationName | ||
The optional descriptive name to give to the to-be-created operation on the API. | ||
.Parameter Description | ||
The optional explanation to describe the to-be-created operation on the API. | ||
.Parameter PolicyFilePath | ||
The path to the file containing the optional policy of the to-be-created operation on the API. | ||
#> | ||
function Get-Function { | ||
function Create-AzApiManagementApiOperation { | ||
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)] $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 | ||
} | ||
|
||
} | ||
Export-ModuleMember -Function Create-AzApiManagementApiOperation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/Arcus.Scripting.ApiManagement/Scripts/Create-AzApiManagementApiOperation.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
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)] $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 = "" | ||
) | ||
|
||
# Retrieve the context of APIM | ||
$apimContext = New-AzApiManagementContext -ResourceGroupName $ResourceGroup -ServiceName $ServiceName | ||
|
||
# Create a new operation on the previously created API | ||
New-AzApiManagementOperation -Context $apimContext -ApiId $ApiId -OperationId $OperationId -Name $OperationName -Method $Method -UrlTemplate $UrlTemplate -Description $Description | ||
Write-Host "New API operation '$OperationName' on API Management service was added." | ||
|
||
# Check if a policy-file has been specified, if not - the base policy is assigned by default | ||
if($OperationId -eq "" -or $PolicyFilePath -eq "") | ||
{ | ||
Write-Host "No policy has been defined." | ||
} | ||
else | ||
{ | ||
Write-Host "Updating policy of the operation '$OperationId' in API '$ApiId'" | ||
Set-AzApiManagementPolicy -Context $apimContext -ApiId $ApiId -OperationId $OperationId -PolicyFilePath $PolicyFilePath | ||
} |
78 changes: 78 additions & 0 deletions
78
src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.ApiManagement.tests.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using module Az | ||
Import-Module -Name $PSScriptRoot\..\Arcus.Scripting.ApiManagement -DisableNameChecking | ||
|
||
Describe "Arcus" { | ||
Context "ApiManagement" { | ||
InModuleScope Arcus.Scripting.ApiManagement { | ||
It "Calls new operation on API Management operation w/o policy" { | ||
# Arrange | ||
$resourceGroup = "shopping" | ||
$serviceName = "shopping-API-management" | ||
$apiId = "shopping-API" | ||
$operationId = "orders" | ||
$method = "POST" | ||
$urlTemplate = "https://{host}.com/{path}{query}" | ||
$context = New-Object -TypeName Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Models.PsApiManagementContext | ||
|
||
Mock New-AzApiManagementContext { | ||
$ResourceGroup | Should -Be $resourceGroup | ||
$ServiceName | Should -Be $serviceName | ||
return $context } -Verifiable | ||
Mock New-AzApiManagementOperation { | ||
$Context | Should -Be $context | ||
$ApiId | Should -Be $apiId | ||
$OperationId | Should -Be $operationId | ||
$Method | Should -Be $method | ||
$UrlTemplate | Should -Be $urlTemplate } -Verifiable | ||
Mock Set-AzApiManagementPolicy { } | ||
|
||
# Act | ||
Create-AzApiManagementApiOperation -ResourceGroup $resourceGroup -ServiceName $serviceName -ApiId $apiId -OperationId $operationId -Method $method -UrlTemplate $urlTemplate | ||
|
||
# Assert | ||
Assert-VerifiableMock | ||
Assert-MockCalled New-AzApiManagementContext -Times 1 | ||
Assert-MockCalled New-AzApiManagementOperation -Times 1 | ||
Assert-MockCalled Set-AzApiManagementPolicy -Times 0 | ||
} | ||
It "Calls new operation on API management operation w/ policy" { | ||
# Arrange | ||
$resourceGroup = "shopping" | ||
$serviceName = "shopping-API-management" | ||
$apiId = "shopping-API" | ||
$operationId = "orders" | ||
$method = "POST" | ||
$urlTemplate = "https://{host}.com/{path}{query}" | ||
$operationName = "POSTing orders" | ||
$description = "API that can process posted orders" | ||
$policyFilePath = "/file-path/operation-policy" | ||
$context = New-Object -TypeName Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Models.PsApiManagementContext | ||
|
||
Mock New-AzApiManagementContext { | ||
$ResourceGroup | Should -Be $resourceGroup | ||
$ServiceName | Should -Be $serviceName | ||
return $context } -Verifiable | ||
Mock New-AzApiManagementOperation { | ||
$Context | Should -Be $context | ||
$ApiId | Should -Be $apiId | ||
$OperationId | Should -Be $operationId | ||
$Method | Should -Be $method | ||
$UrlTemplate | Should -Be $urlTemplate | ||
$Description | Should -Be $description } -Verifiable | ||
Mock Set-AzApiManagementPolicy { | ||
$ApiId | Should -Be $apiId | ||
$OperationId | Should -Be $operationId | ||
$PolicyFilePath | Should -Be $policyFilePath } -Verifiable | ||
|
||
# Act | ||
Create-AzApiManagementApiOperation -ResourceGroup $resourceGroup -ServiceName $serviceName -ApiId $apiId -OperationId $operationId -Method $method -UrlTemplate $urlTemplate -OperationName $operationName -Description $Description -PolicyFilePath $policyFilePath | ||
|
||
# Assert | ||
Assert-VerifiableMock | ||
Assert-MockCalled New-AzApiManagementContext -Times 1 | ||
Assert-MockCalled New-AzApiManagementOperation -Times 1 | ||
Assert-MockCalled Set-AzApiManagementPolicy -Times 1 | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters