Scripts/Create-AzApiManagementApiOperation.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
param(
   [Parameter(Mandatory = $true)][string] $ResourceGroupName = $(throw "Resource group is required"),
   [Parameter(Mandatory = $true)][string] $ServiceName = $(throw "API management service name is required"),
   [Parameter(Mandatory = $true)][string] $ApiId = $(throw "API ID is required"),
   [Parameter(Mandatory = $true)][string] $OperationId = $(throw "Operation ID is required"),
   [Parameter(Mandatory = $true)][string] $Method = $(throw "Method is required"),
   [Parameter(Mandatory = $true)][string] $UrlTemplate = $(throw "URL template is required"),
   [Parameter(Mandatory = $false)][string] $OperationName = $OperationId,
   [Parameter(Mandatory = $false)][string] $Description = "",
   [Parameter(Mandatory = $false)][string] $PolicyFilePath = ""
)

$apimContext = New-AzApiManagementContext -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName

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 instance was added."

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
}