【问题标题】:Use Powershell Az Module to Publish Revision to Developer Portal使用 Powershell Az 模块将修订版发布到 Developer Portal
【发布时间】:2021-07-10 13:26:12
【问题描述】:

我有一个 powershell 脚本,它在我的 API 的 Azure Devops Release Pipepline 中运行,它使用我 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 令牌吗?

【问题讨论】:

    标签: powershell azure-api-management azure-service-principal powershell-az-module


    【解决方案1】:

    azure powershell 中没有用于发布开发门户的内置命令,您的选择是使用您的服务主体获取 sas 令牌并通过 powershell 调用 api。

    首先,确保在 azure 门户中设置 Enable Management REST APIYes

    然后使用下面的命令。

    $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
    

    过了一会儿,在传送门里检查一下,它工作正常。

    【讨论】:

    • 尝试您的 URL,我收到一个错误:Invoke-RestMethod:底层连接已关闭:无法为 SSL/TLS 安全通道建立信任关系。
    • 然后我在启用管理 API 的屏幕中尝试了管理 API url:https://${apimName}.management.azure-api.net/publish 这导致放置错误查询字符串中的 api 版本。所以,然后我添加了 QS 值,现在它要我使用完全限定的路径 /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}
    • 好吧,我想通了。我们的网关上有一个自定义域。我们必须使用的网址是 ourcustomdevportaldoman.com/publish
    猜你喜欢
    • 1970-01-01
    • 2021-02-13
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    • 1970-01-01
    • 2010-11-15
    • 1970-01-01
    • 2021-07-05
    相关资源
    最近更新 更多