Update api in apim using az apim api

Viewed 2495

In my azure devops pipeline i have a task AzureCLI@2 that deploys an api to apim. How can i use the same task using the az apim api to update the api? I know that there is an operation to do this but the documentantion don't show how to and i didn't find any example. I'm using an open api file generated in the pipeline to create and i would like to use it to update the api either.

4 Answers

Just use az apim api import again. It will replace existing API with new one, preserving policies for operations with the same template. --path and --api-id parameters must be the same as existing API. Provide url/path to the swagger/openapi and correct specification format. Example:

az apim api import --path "/myapi" --api-id "myapiid" --resource-group "test-rg" --service-name "test-apim" --specification-url "https://petstore.swagger.io/v2/swagger.json" --specification-format Swagger

If you want to add revision instead of updating existing one, add additional --api-revision parameter

az apim api update is suited for updating single properties or fields like description, api type etc.

You can create an API in APIM using this method and delete it using this method.

Please use the below script to create/update API in APIM using az cli. I am assuming

A. Your api definition is in Open API definition file. B. You are identifying apim endpoints using path ($apiPrefix).

$apiPrefix="my-path"
$rgName="RG_NAME"
$apimS="APIM_SVC_NAME"
$apiDefinitionLocalPath="PATH_TO_APIM_DEFINITION_JSON"
$allAPIs= az apim api list --resource-group $rgName --service-name $apimS
$allAPIsJson= $allAPIs | ConvertFrom-Json
$apiWithMatchingPath=$allAPIsJson | Where-Object {$_.path -eq $apiPrefix }
if($apiWithMatchingPath){
    #it only requires the ID. using $apiWithMatchingPath.id will return the 
    #fully qualified ID... which is not required.
    az apim api import --api-id $apiWithMatchingPath.name --resource-group `
    $rgName --service-name $apimS --path $apiPrefix --api-type http --protocols` 
     https --specification-format OpenApi --specification-path `
    $apiDefinitionLocalPath
    }else{
      #IMPORT definition w/o name
      az apim api import --resource-group $rgName --service-name $apimS --path `
      $apiPrefix --api-type http --protocols https --specification-format `
      OpenApi --specification-path $apiDefinitionLocalPath
 }

In the azure devops pipeline, we can set the AzureCLI task like this: enter image description here

We need to go to the azure portal and find our api ID, and then update the api info. Here is my test result: enter image description here

Related