使用Powershell,如何向azure api管理API添加版本?
我正在使用New-AzureRmApiManagementApi
创建API,并使用AzureRM.ApiManagement 5.1.2版
答案 0 :(得分:1)
要添加模块版本,可以使用命令Install-Module -Name AzureRM.ApiManagement -RequiredVersion 6.0.0
有关更多信息,请检查此链接PowerShell gallery
答案 1 :(得分:0)
我已经通过PowerShell中的Azure REST API在APIM下实现了Azure API管理版本集。
参考Api-versioning-with-swagger-azure-api-management-services
Microsoft Azure PowerShell文档没有任何提示,到目前为止,我在Internet上都找不到任何提示。尽管可以使用Import-AzureRmApiManagementApi和New-AzureRmApiManagementApiVersionSet Powershell cmdlet,但使用带有cmdlet的Swagger Open API格式的APIM中的APIM似乎是不可能的。
在寻找解决方案时,我从Ronald Wildenberg中发现了这篇文章,他在Powershell脚本中使用直接REST API调用,而不是cmdlet,这就是他如何在Azure API Manager中创建版本化的API。
param(
[string]$tenantId = "",
[string]$subscriptionId = "",
[string]$resourceGroupName = "",
[string]$apimServiceName = "",
[string]$clientId = "",
[string]$clientSecret = "",
[string]$apiName = "",
[string]$backendUrl = "",
[bool] $apiContainsMultipleVersions = $true
)
class ApiManagerConnection {
[ValidateNotNullOrEmpty()][string]$TenantId
[ValidateNotNullOrEmpty()][string]$SubscriptionId
[ValidateNotNullOrEmpty()][string]$ResourceGroupName
[ValidateNotNullOrEmpty()][string]$ApiManagerServiceName
[ValidateNotNullOrEmpty()][string]$AccessToken
}
function Get-AccessToken ($clientId, $clientSecret, $tenantId) {
$body = "grant_type=client_credentials&client_id=$clientId&client_secret=$clientSecret&resource=https://management.core.windows.net/"
$result = Invoke-RestMethod -Method Post -Uri "https://login.microsoftonline.com/$tenantId/oauth2/token" -Body $body -ContentType 'application/x-www-form-urlencoded'
$result.access_token
}
function Read-SwaggerHtml ($backendUrl) {
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest "$backendUrl/swagger"
}
function Get-AllSwaggerFilePaths ($swaggerHtml) {
$paths = @()
Select-String '/swagger/v\d/swagger.json' -input $swaggerHtml -AllMatches | ForEach-Object {
$paths = $_.matches -split ' '
}
$paths
}
function New-ApiVersionSet ([ApiManagerConnection] $connection, $apiName) {
$createOrUpdateApiVersionSetBody = @{
name = $apiName
versioningScheme = "Segment"
}
Invoke-RestMethod `
-Method Put `
-Uri ("https://management.azure.com/subscriptions/" + $connection.SubscriptionId +
"/resourceGroups/" + $connection.ResourceGroupName +
"/providers/Microsoft.ApiManagement/service/" + $connection.ApiManagerServiceName +
"/api-version-sets/" + $apiName + "VersionSet" +
"?api-version=2018-01-01") `
-Headers @{ Authorization = ("Bearer " + $connection.AccessToken)
"Content-Type" = "application/json"
} `
-Body ($createOrUpdateApiVersionSetBody | ConvertTo-Json -Compress -Depth 3)
}
function New-VersionedApi ([ApiManagerConnection] $connection, $apiVersionSet, $version, $backendUrl, $apiId) {
$createOrUpdateApiBody = @{
properties = @{
description = "Temp API"
apiVersion = $version
apiVersionSetId = $apiVersionSet.id
displayName = "Temp API"
path = "temp-api"
protocols = , "https"
serviceUrl = $backendUrl
}
}
Invoke-RestMethod `
-Method Put `
-Uri ("https://management.azure.com/subscriptions/" + $connection.SubscriptionId +
"/resourceGroups/" + $connection.ResourceGroupName +
"/providers/Microsoft.ApiManagement/service/" + $connection.ApiManagerServiceName +
"/apis/" + $apiId +
"?api-version=2018-01-01") `
-Headers @{ Authorization = ("Bearer " + $connection.AccessToken)
"Content-Type" = "application/json"
} `
-Body ($createOrUpdateApiBody | ConvertTo-Json -Compress -Depth 3)
}
function Update-VersionedApi ([ApiManagerConnection] $connection, $api, $apiVersionSet, $version, $serviceUrl, $apiId) {
$createOrUpdateApiBody = @{
properties = @{
description = $api.Description
apiVersion = $version
apiVersionSetId = $apiVersionSet.id
displayName = $api.Name
path = $api.Path
protocols = , "https"
serviceUrl = $serviceUrl
}
}
Invoke-RestMethod `
-Method Put `
-Uri ("https://management.azure.com/subscriptions/" + $connection.SubscriptionId +
"/resourceGroups/" + $connection.ResourceGroupName +
"/providers/Microsoft.ApiManagement/service/" + $connection.ApiManagerServiceName +
"/apis/" + $apiId +
"?api-version=2018-01-01") `
-Headers @{ Authorization = ("Bearer " + $connection.AccessToken)
"Content-Type" = "application/json"
} `
-Body ($createOrUpdateApiBody | ConvertTo-Json -Compress -Depth 3)
}
function Read-SwaggerVersionJson ($swaggerJsonUrl, $version) {
Invoke-WebRequest $swaggerJsonUrl -OutFile "swagger$version.json"
}
function Update-SwaggerPaths ($apiSwaggerSuffix, $version, $multiVersion) {
if ($multiVersion) {
(Get-Content "swagger$version.json").replace($apiSwaggerSuffix, '') | Set-Content "swagger$version.json"
}
}
function Remove-DeletedApiVersions ($apiMgmtContext, $apiName, $multiVersion) {
if ($multiVersion) {
$existingApis = Get-AzureRmApiManagementApi -Context $apiMgmtContext | Where-Object ApiId -Match "^$apiName-.*"
foreach ($existingApi in $existingApis) {
if (!$apisSeen.Contains($existingApi.ApiId)) {
Write-Host ("Deleting " + $existingApi.ApiId)
Remove-AzureRmApiManagementApi -Context $apiMgmtContext -ApiId $existingApi.ApiId
}
}
}
}
$accessToken = Get-AccessToken -clientId $clientId -clientSecret $clientSecret -tenantId $tenantId
$apiManagerConnection = [ApiManagerConnection]@{
TenantId = $tenantId
SubscriptionId = $subscriptionId
ResourceGroupName = $resourceGroupName
ApiManagerServiceName = $apimServiceName
AccessToken = $accessToken
}
$swaggerHtml = Read-SwaggerHtml -backendUrl $backendUrl
Write-Host ("Swagger HTML file is downloaded...")
$swaggerPaths = Get-AllSwaggerFilePaths -swaggerHtml $swaggerHtml
Write-Host ("Swagger JSON paths are discovered...")
Write-Host ("")
foreach ($swaggerPath in $swaggerPaths) {
$swaggerJsonUrl = $backendUrl + $swaggerPath
$swaggerPath -match 'v\d' | Out-Null
$version = $matches[0]
$apiId = "$apiName-" + $version.ToUpper()
$apisSeen += $apiId
if ($apiContainsMultipleVersions) {
$apiSwaggerSuffix = "/api/$version"
$apiManagerSuffix = "/api"
}
else {
$apiSwaggerSuffix = ""
$apiManagerSuffix = ""
}
$serviceUrl = "$backendUrl$apiSwaggerSuffix"
Write-Host ("*** Starting to process '" + $swaggerPath + "' ***")
Read-SwaggerVersionJson -swaggerJsonUrl $swaggerJsonUrl -version $version
Write-Host ("Swagger JSON file is downloaded...")
Update-SwaggerPaths -apiSwaggerSuffix $apiSwaggerSuffix -version $version -multiVersion $apiContainsMultipleVersions
Write-Host ("Swagger JSON file is updated...")
$apiVersionSet = New-ApiVersionSet -connection $apiManagerConnection -apiName $apiName
Write-Host ("Api version set is created...")
$api = New-VersionedApi -connection $apiManagerConnection -apiVersionSet $apiVersionSet -version $version -backendUrl $backendUrl -apiId $apiId
Write-Host ("A versioned Api is created based on the version set...")
$ApiMgmtContext = New-AzureRmApiManagementContext -ResourceGroupName $apiManagerConnection.ResourceGroupName -ServiceName $apiManagerConnection.ApiManagerServiceName
$api = Import-AzureRmApiManagementApi -Context $ApiMgmtContext -SpecificationFormat "Swagger" -SpecificationPath "swagger$version.json" -Path "$apiName$apiManagerSuffix" -ApiId $apiId
Write-Host ("A Swagger definition is imported into the versioned Api...")
$api = Update-VersionedApi -connection $apiManagerConnection -api $api -apiVersionSet $apiVersionSet -version $version -serviceUrl $serviceUrl -apiId $apiId
Write-Host ("Versioned Api is updated to its final state...")
Write-Host ("*** Finished processing '" + $swaggerPath + "' ***")
}
Remove-DeletedApiVersions -apiMgmtContext $ApiMgmtContext -apiName $apiName -multiVersion $apiContainsMultipleVersions