Update api in apim using az apim api
Asked Answered
H

4

6

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.

Hailstone answered 23/4, 2021 at 13:26 Comment(1)
What about this issue, does the answer will help you? If yes, you could Accept it as an Answer, so it could help other community members who get the same issues and we could archive this thread, thanks.Gongorism
E
10

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.

Eustazio answered 2/5, 2021 at 21:28 Comment(3)
Are you sure that az apim api import update existing one ? I am trying but it is not reflecting.Ehudd
Confirmed this works if you modify the openapi structure.Christen
I got "Message: API with specified name '<NAME>' already exists"Discretional
C
0

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

Cockleboat answered 23/4, 2021 at 16:4 Comment(1)
I'm already creating the API with the method IMPORT that is the right to create an api with the open api specification. I can't DELETE the API to recreate it after. I want to use this method to update the API.with the new open api specification generated by my pipeline but i dont't know how.Hailstone
S
0

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
 }
Such answered 2/6, 2021 at 16:3 Comment(0)
G
-1

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

Gongorism answered 26/4, 2021 at 5:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.