我想知道如何通过 ARM 部署将第二个 api 附加到 Azure api 管理中已注册的 api 中?
如果我在 Microsoft.ApiManagement/service/apis 资源中使用相同的 name 属性值。它会覆盖整个 api,而不是附加它。我在arm参考文档中找不到属性来指定我想要附加api而不是覆盖它:https://learn.microsoft.com/en-us/azure/templates/microsoft.apimanagement/2019- 01-01/服务/api
我想通过arm实现相同的结果,就像我可以通过Azure门户导入菜单完成的那样 文档中也对此进行了描述:https://learn.microsoft.com/en-us/azure/api-management/add-api-manually#append-other-apis
目前这显然是不可能的。 “追加”逻辑在 UI 中实现,但它仅依赖于公开可用的 ARM 调用。您可以检查它对 ARM 进行的调用,将一个 API 附加到另一个 API,并尝试“手动”重现它。
当我等待 MS 实施此积压项目时:
https://feedback.azure.com/d365community/idea/3164bc08-0125-ec11-b6e6-000d3a4f0858
我在我们的项目中进行了以下修复,将 Open API jason 文件附加到现有的 jason 文件并将输出保存在压缩的 jason 文件中,因为 loadTextContent 上有 1MB 文件限制,API 管理上载此文件有 4MB 文件限制使生成的 jason 文件小很多。
这是powershell代码:
追加OpenApiOperationsAndSchemas.ps1:
param(
[string]$sourceSwaggerFile,
[string]$swaggerFileToAppend,
[string]$outputFile
)
Write-Output "========================================================================================================================"
Write-Output "Appending operations and schemas from OpenAPI file: $swaggerFileToAppend"
Write-Output "to existing OpenAPI file: $sourceSwaggerFile"
Write-Output "and saving the output in: $outputFile"
# Load the original Swagger file and the Swagger file to append
$sourceSwaggerContent = Get-Content -Path $sourceSwaggerFile | ConvertFrom-Json
$contentToAppend = Get-Content -Path $swaggerFileToAppend | ConvertFrom-Json
foreach( $pathProperty in $contentToAppend.paths.psobject.properties )
{
$propertyName = $pathProperty.name
$propertyValue = $pathProperty.value
Write-Output "Adding Operation: $propertyName"
# Add or update the path in $sourceSwaggerContent
if (-not $sourceSwaggerContent.paths.psobject.properties.name.Contains($propertyName)) {
# If the path does not exist, add it
$sourceSwaggerContent.paths | Add-Member -MemberType NoteProperty -Name $propertyName -Value $propertyValue
} else {
# If the path already exists, update it
$sourceSwaggerContent.paths.$propertyName = $propertyValue
}
}
foreach( $schemaProperty in $contentToAppend.components.schemas.psobject.properties)
{
$propertyName = $schemaProperty.name
$propertyValue = $schemaProperty.value
Write-Output "Adding Schema: $propertyName"
# Add or update the schema in $sourceSwaggerContent
if (-not $sourceSwaggerContent.components.schemas.psobject.properties.name.Contains($propertyName)) {
# If the schema does not exist, add it
$sourceSwaggerContent.components.schemas | Add-Member -MemberType NoteProperty -Name $propertyName -Value $propertyValue
} else {
# If the schema already exists, update it
$sourceSwaggerContent.components.schemas.$propertyName = $propertyValue
}
}
# Save the modified Swagger JSON back to outputFile, but use -Compress so we stay under the 1048576 characters max size.
# See https://github.com/Azure/bicep/issues/4293 for more info.
$sourceSwaggerContent | ConvertTo-Json -Compress -Depth 100 | Set-Content -Path $outputFile
Write-Output "========================================================================================================================"
在 swagger 生成 jason 文件后,我立即从项目 postbuild 项目文件中调用它,如下所示:
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="dotnet new tool-manifest --force" />
<Exec Command="dotnet tool install --local Swashbuckle.AspNetCore.Cli --version 6.4.0" />
<MakeDir Directories="$(ProjectDir)..\artifacts" />
<Exec Command="dotnet tool run swagger tofile --output $(ProjectDir)..\artifacts\swagger-v9.json $(OutputPath)$(AssemblyName).dll internal-v9" />
<Exec Command="dotnet tool run swagger tofile --output $(ProjectDir)..\artifacts\swagger-public-v9.json $(OutputPath)$(AssemblyName).dll public-v9" />
<Exec Command="dotnet tool run swagger tofile --output $(ProjectDir)..\artifacts\swagger-v10.json $(OutputPath)$(AssemblyName).dll internal-v10" />
<Exec Command="dotnet tool run swagger tofile --output $(ProjectDir)..\artifacts\swagger-public-v10.json $(OutputPath)$(AssemblyName).dll public-v10" />
<Exec Command="xcopy /Y $(ProjectDir)Common\OpenApi\CustomSwaggerJson\swagger-ignore-v10.json $(ProjectDir)..\artifacts\" />
<Exec Command="powershell -ExecutionPolicy Bypass -File $(ProjectDir)Common\OpenApi\AppendOpenApiOperationsAndSchemas.ps1 -sourceSwaggerFile $(ProjectDir)..\artifacts\swagger-v10.json -swaggerFileToAppend $(ProjectDir)..\artifacts\swagger-ignore-v10.json -outputFile $(ProjectDir)..\artifacts\swagger-appended-v10.json"/>
</Target>