Azure API 管理 - 更新 Swagger 架构
Azure API Management - Update Swagger Schema
我已经导入了我的 swagger 架构,管理服务已经为我的 API 构建了所有文档。我现在已经进行了更改并重新部署了这些更改。我是否已从 API 管理中删除 API 并重新导入,或者是否有办法 'update' 现有的?
没关系,原来你只是告诉导入它是一个现有的 API 并且它会更新。我担心我最终会收到一条错误消息,即 'operation already existed'.
好的,伙计们,我将履行我对人类的责任,并向你们展示真正的方法。 "real" 我的意思是,让我们面对现实吧,现实世界中没有人会一直点击门户来刷新他们 API 的更改。每个人都想要的是将这个烦人的手动任务自动化。
所以我写了这个我们目前在生产中使用的 Powershell 脚本。这肯定会让你到达那里。
先决条件:您需要服务主体才能自动登录。 I used this guide to do that.
param(
[String]$pass,
[String]$swaggerUrl,
[String]$apiId,
[String]$apiName,
[String]$apiServiceUrl
)
Try
{
$azureAccountName = "[YOUR AZURE AD APPLICATION ID FOR THE SERVICE PRINCIPAL]"
$azurePassword = ConvertTo-SecureString $pass -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential($azureAccountName, $azurePassword)
Add-AzureRmAccount -Credential $psCred -TenantId "[YOUR AZURE TENANT ID]" -ServicePrincipal
$azcontext = New-AzureRmApiManagementContext -ResourceGroupName "[YOUR RESOURCE GROUP NAME]" -ServiceName "[THE NAME OF YOUR API MANAGEMENT INSTANCE]"
Import-AzureRmApiManagementApi -Context $azcontext -SpecificationFormat "Swagger" -SpecificationUrl $swaggerUrl -ApiId $apiId
Set-AzureRmApiManagementApi -Context $azcontext -ApiId $apiId -ServiceUrl $apiServiceUrl -Protocols @("https") -Name $apiName
}
Catch
{
Write-Host "FAILURE! An error occurred!!! Aborting script..."
exit
}
显然您需要替换上面括号中的字符串。参数说明:
"pass":您的服务主体的密码
"swaggerUrl" : 应用程序 swagger 的路径 json 文档
"apiId" :从您的 API 管理实例获取此值,如果您检查现有 API
,它将显示在门户的仪表板中
"apiName" : 随便起什么名字
"apiServiceUrl":你的 API 的 Azure App Service Url(或者你的 API 所在的任何地方)
作为参考,因为我遇到了同样的挑战,您可以选择使用 ARM 模板,并创建一个 CI(使用 VSTS、git,等等)并部署 ARM 模板.优点是,如果由于某种原因您需要删除 API 管理服务并重新创建它,也可以使用 ARM 模板。如果您需要对某些特定配置或 api 规范进行更改,那么您可以执行并部署它,它会更新您的更改。
我目前正在使用下面的这个脚本,我可以在本地 运行 修改版本,或者可以在 CI/CD 自动更新解决方案中更新
Login-AzureRmAccount
$subscriptionId =
( Get-AzureRmSubscription |
Out-GridView `
-Title "Select an Azure Subscription ..." `
-PassThru
).SubscriptionId
$subscriptionId
Select-AzureRmSubscription -SubscriptionId $subscriptionId
$apiMSName = "<input>"
$swaggerUrl = "<input>"
$apiId = "<input>"
$apiName = "<input>"
$apiServiceUrl = "<input>"
$resourceGroupName = "<input>"
$azcontext = New-AzureRmApiManagementContext -ResourceGroupName
$resourceGroupName -ServiceName $apiMSName
Import-AzureRmApiManagementApi -Context $azcontext -SpecificationFormat "Swagger" -SpecificationUrl $swaggerUrl -ApiId $apiId
Set-AzureRmApiManagementApi -Context $azcontext -ApiId $apiId -ServiceUrl $apiServiceUrl -Protocols @("https") -Name $apiName
我已经导入了我的 swagger 架构,管理服务已经为我的 API 构建了所有文档。我现在已经进行了更改并重新部署了这些更改。我是否已从 API 管理中删除 API 并重新导入,或者是否有办法 'update' 现有的?
没关系,原来你只是告诉导入它是一个现有的 API 并且它会更新。我担心我最终会收到一条错误消息,即 'operation already existed'.
好的,伙计们,我将履行我对人类的责任,并向你们展示真正的方法。 "real" 我的意思是,让我们面对现实吧,现实世界中没有人会一直点击门户来刷新他们 API 的更改。每个人都想要的是将这个烦人的手动任务自动化。
所以我写了这个我们目前在生产中使用的 Powershell 脚本。这肯定会让你到达那里。
先决条件:您需要服务主体才能自动登录。 I used this guide to do that.
param(
[String]$pass,
[String]$swaggerUrl,
[String]$apiId,
[String]$apiName,
[String]$apiServiceUrl
)
Try
{
$azureAccountName = "[YOUR AZURE AD APPLICATION ID FOR THE SERVICE PRINCIPAL]"
$azurePassword = ConvertTo-SecureString $pass -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential($azureAccountName, $azurePassword)
Add-AzureRmAccount -Credential $psCred -TenantId "[YOUR AZURE TENANT ID]" -ServicePrincipal
$azcontext = New-AzureRmApiManagementContext -ResourceGroupName "[YOUR RESOURCE GROUP NAME]" -ServiceName "[THE NAME OF YOUR API MANAGEMENT INSTANCE]"
Import-AzureRmApiManagementApi -Context $azcontext -SpecificationFormat "Swagger" -SpecificationUrl $swaggerUrl -ApiId $apiId
Set-AzureRmApiManagementApi -Context $azcontext -ApiId $apiId -ServiceUrl $apiServiceUrl -Protocols @("https") -Name $apiName
}
Catch
{
Write-Host "FAILURE! An error occurred!!! Aborting script..."
exit
}
显然您需要替换上面括号中的字符串。参数说明:
"pass":您的服务主体的密码
"swaggerUrl" : 应用程序 swagger 的路径 json 文档
"apiId" :从您的 API 管理实例获取此值,如果您检查现有 API
,它将显示在门户的仪表板中"apiName" : 随便起什么名字
"apiServiceUrl":你的 API 的 Azure App Service Url(或者你的 API 所在的任何地方)
作为参考,因为我遇到了同样的挑战,您可以选择使用 ARM 模板,并创建一个 CI(使用 VSTS、git,等等)并部署 ARM 模板.优点是,如果由于某种原因您需要删除 API 管理服务并重新创建它,也可以使用 ARM 模板。如果您需要对某些特定配置或 api 规范进行更改,那么您可以执行并部署它,它会更新您的更改。
我目前正在使用下面的这个脚本,我可以在本地 运行 修改版本,或者可以在 CI/CD 自动更新解决方案中更新
Login-AzureRmAccount
$subscriptionId =
( Get-AzureRmSubscription |
Out-GridView `
-Title "Select an Azure Subscription ..." `
-PassThru
).SubscriptionId
$subscriptionId
Select-AzureRmSubscription -SubscriptionId $subscriptionId
$apiMSName = "<input>"
$swaggerUrl = "<input>"
$apiId = "<input>"
$apiName = "<input>"
$apiServiceUrl = "<input>"
$resourceGroupName = "<input>"
$azcontext = New-AzureRmApiManagementContext -ResourceGroupName
$resourceGroupName -ServiceName $apiMSName
Import-AzureRmApiManagementApi -Context $azcontext -SpecificationFormat "Swagger" -SpecificationUrl $swaggerUrl -ApiId $apiId
Set-AzureRmApiManagementApi -Context $azcontext -ApiId $apiId -ServiceUrl $apiServiceUrl -Protocols @("https") -Name $apiName