Azure API管理 - 更新Swagger架构

时间:2016-03-04 15:27:20

标签: api azure-api-management

我已经导入了我的swagger架构,管理服务已经为我的API构建了所有文档。我现在已经进行了更改并重新部署了更改。我是否已从API管理中删除API并重新导入,或者是否有办法更新'现有的一个?

4 个答案:

答案 0 :(得分:7)

好吧,伙计们,我将尽职尽责,向你展示真正做到这一点的方法。通过“真实”我的意思是,让我们面对现实,现实世界中的任何人都不会继续点击门户网站来刷新其API的更改。每个人都想要的是自动化这个烦人的手动任务。

所以我写了这个Powershell脚本,我们目前正在生产中使用它。这肯定会让你到那儿。

PREREQUISITE:您需要服务主体才能自动登录。 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 Management实例获取此值,如果您检查现有API

,它将显示在门户网站的信息中心中

“apiName”:无论你想命名它什么

“apiServiceUrl”:您的API的Azure App Service Url(或您的API所在的位置)

答案 1 :(得分:4)

没关系,事实证明你只是告诉导入它的现有API并且它会更新。我担心我最终会得到一条错误信息,即“操作已经存在”。

答案 2 :(得分:1)

我目前在下面使用此脚本,我可以在本地运行修改后的版本,也可以在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

答案 3 :(得分:0)

仅供参考,由于我遇到同样的挑战,因此您可以选择使用ARM模板,并创建一个CI(使用VSTS,git或其他工具)并部署ARM模板。好处是,如果由于某种原因需要删除API管理服务并重新创建它,则使用ARM模板也可以实现。如果您需要对某些特定配置或api规范进行更改,则可以进行更改并进行部署,它将更新您的更改。