4

我已经导入了我的招摇模式,并且管理服务已经为我的 API 构建了所有文档。我现在进行了更改并重新部署了更改。我是否已从 API 管理中删除 API 并重新导入,还是有办法“更新”现有的?

4

4 回答 4

11

好的,伙计们,我将尽我对人类的职责,向你们展示做到这一点的真正方法。我所说的“真实”是指,让我们面对现实吧,现实世界中没有人会继续点击门户以刷新对其 API 的更改。每个人都想要的是自动化这个烦人的手动任务。

所以我写了这个我们目前在生产中使用的 Powershell 脚本。这肯定会让你到达那里。

先决条件:您需要一个能够自动登录的服务主体。我使用本指南来做到这一点。

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 应用服务 URL(或 API 所在的任何位置)

于 2017-11-02T06:04:13.203 回答
4

没关系,原来你只是告诉导入它是一个现有的 API,它会更新。我担心我最终会收到一条错误消息,指出“操作已经存在”。

于 2016-03-04T15:30:00.433 回答
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
于 2018-08-24T20:31:37.280 回答
0

作为参考,由于我遇到了同样的挑战,您可以选择使用 ARM 模板,并创建一个 CI(使用 VSTS、git 等)并部署 ARM 模板。优点是,如果由于某种原因需要删除 API 管理服务并重新创建它,也可以使用 ARM 模板。如果您需要对某些特定配置或 api 规范进行更改,那么您可以这样做并部署它,它会更新您的更改。

于 2018-08-22T10:25:37.717 回答