我有一个 powershell 脚本,它在我的 API 的 Azure Devops Release Pipepline 中运行,它使用我的 API 中的 swagger 文档自动将更改推送到 APIM。
缺少的步骤是,一旦我进行了修订,我需要使用最新的架构和规范更新开发人员门户。我不能让开发人员在每次发布时都点击发布按钮。
根据这篇文章,我可以获得一个带有用户 ID 的令牌(不确定它想要什么用户 ID,我正在使用服务主体)并发布一个帖子,但我希望像我一样的模块中有一个可用的实用程序用于我的脚本的其余部分。
param([string]$subscriptionId = "", [string]$resourceGroup = "", [string]$apimName = "", [string]$apiId = "", [string]$swaggerJsonPath = "", [string]$apiPath = "", [string]$baseApiUrl = "", [string]$version = "")
$ErrorActionPreference = 'Stop'
# Install-Module -Name Az -AllowClobber -Scope CurrentUser
Import-Module Az
$ctx = Get-AzContext
if ($subscriptionId -eq '')
{
$subscriptionId = $ctx.Subscription.Id
}
#fix version - this will come in as a build number with periods, which are not allowed
#use dashes instead
$version = $version.Replace('.', '-')
Write-Output "Resource Group: ${resourceGroup}"
Write-Output "APIM Name: ${apimName}"
Write-Output "Api Id: ${apiId}"
Write-Output "Swagger Path: ${swaggerJsonPath}"
Write-Output "Api Path: ${apiPath}"
Write-Output "Base Api Url: ${baseApiUrl}"
Write-Output "Version: ${version}"
# Set the context to the subscription Id
Select-AzSubscription -SubscriptionId $subscriptionId
Write-Output "Subscription loaded: ${subscriptionId}"
# load the API management gateway context
$apimContext = New-AzApiManagementContext -ResourceGroupName $resourceGroup -ServiceName $apimName
Write-Output "APIM Context"
Write-Output $apimContext
# create a new revision with the supplied version (this should be the release number)
$apiRevision = New-AzApiManagementApiRevision -Context $apimContext -ApiId $apiId -ApiRevision $version
Write-Output "API Revision"
Write-Output $apiRevision
try
{
Write-Output "Begin API Import from Swagger"
# import the swagger as open id spec - this will fail if the name of the api in swagger does not match the name of the api in the gateway
$importResult = Import-AzApiManagementApi -Context $apimContext -SpecificationUrl $swaggerJsonPath -SpecificationFormat OpenApi -ApiId $apiId -Path $apiPath -ServiceUrl $baseApiUrl -ApiRevision $apiRevision.ApiRevision -ApiVersion $apiRevision.ApiVersion
Write-Output "Api refreshed from swagger [${swaggerJsonPath}]"
Write-Output $importResult
}
catch
{
Write-Output 'Api Import Failure'
Write-Output $_.Exception
Write-Output 'Beginning Cleanup'
#clean up the revision we made
Remove-AzApiManagementApiRevision -Context $apimContext -ApiId $apiId -ApiRevision $version
Write-Output "Revision ${$apiRevision.ApiRevision} removed"
exit 10
}
# set the revision as current (release it to the public)
$apiRelease = New-AzApiManagementApiRelease -Context $apimContext -ApiId $apiId -ApiRevision $version
Write-Output "API Release"
Write-Output $apiRelease
# TODO: Publish dev portal
如果没有已经提供的模块,我可以使用我的服务主体获得一个 sas 令牌吗?