问题描述
我有一个 powershell 脚本,它在我的 Azure Devops 发布管道中为我的 API 运行,它使用我的 API 中的 swagger 文档自动将更改推送到 APIM。
缺少的步骤是,一旦我进行了修改,我需要使用最新的架构和规范更新开发人员门户。我不能让开发人员每次发布时都出去点击发布按钮。
根据 this post,我可以获得一个带有用户 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 令牌吗?
解决方法
azure powershell 中没有内置命令来发布开发门户,您可以选择使用服务主体获取 sas 令牌并通过 powershell 调用 api。
首先,确保天蓝色门户中的设置 Enable Management REST API
为 Yes
。
然后使用下面的命令。
$resourceGroup = "xxxxxx"
$apimName = "xxxxxx"
$apimContext = New-AzApiManagementContext -ResourceGroupName $resourceGroup -ServiceName $apimName
$Access = Get-AzApiManagementTenantAccess -Context $apimContext
$token = (New-AzApiManagementUserToken -Context $apimContext -UserId $Access.Id).UserToken
$header = @{
'Authorization' = 'SharedAccessSignature ' + $token
}
Invoke-RestMethod -Method Post -Uri "https://$apimName.developer.azure-api.net/publish" -Headers $header
一段时间后,在门户中检查它,它工作正常。