Jason H
Jason H

Reputation: 5156

Azure API Management - Update Swagger Schema

I have Imported my swagger schema and the management service has built out all the documentation for my API. I have now made changes and re-deployed the changes. Do I have remove the API from the API Management and re-import or is there a way to 'update' the existing one?

Upvotes: 4

Views: 6650

Answers (4)

Judy007
Judy007

Reputation: 5870

I am currently using this script below, a modified version I can run locally or can be updated in CI/CD solution for automated updates

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

Upvotes: 1

Hugo Barona
Hugo Barona

Reputation: 1398

Just as reference, since i had the same challenge, you have the option to use ARM templates, and create a CI (using VSTS, git, whatever) and deploy the ARM template. The advantage is, if for some reason you need to delete your API Management service and create it again, it will also be possible with the ARM template. If you need to do the changes to some specific configuration, or to the api specification, then you can do it, and deploy it, and it will update your changes.

Upvotes: 0

starmandeluxe
starmandeluxe

Reputation: 2548

Ok guys I'm going to do my duty to humanity and show you the REAL way to do this. By "real" I mean, let's face it, nobody in the real world is going to keep clicking the portal to refresh changes to their API. What everyone wants is to automate this annoying manual task.

So I wrote this Powershell script which we are currently using in production. This will get you there for sure.

PREREQUISITE: You need a service principal to be able to automate the login. 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 
}

Obviously you'll need to replace the bracketed strings above. An explanation of the parameters:

"pass" : Your service principal's password

"swaggerUrl" : The path to your application's swagger json document

"apiId" : Get this value from your API Management instance, it will be shown in the portal's dashboard if you check that existing API

"apiName" : Whatever you want to name it

"apiServiceUrl" : The Azure App Service Url of your API (or wherever your API is)

Upvotes: 11

Jason H
Jason H

Reputation: 5156

Nevermind, turns out you just tell the import that its an existing API and it will update. I was concerned I was going to end up with an error message that the 'operation already existed'.

Upvotes: 4

Related Questions