Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 431
Jason H Avatar asked Dec 05 '22 01:12

Jason H


1 Answers

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)

like image 135
starmandeluxe Avatar answered Dec 31 '22 13:12

starmandeluxe