简体   繁体   English

使用 az apim api 在 apim 中更新 api

[英]Update api in apim using az apim api

In my azure devops pipeline i have a task AzureCLI@2 that deploys an api to apim.在我的 azure devops 管道中,我有一个任务 AzureCLI@2,它将 api 部署到 apim。 How can i use the same task using the az apim api to update the api?如何使用az apim api使用相同的任务来更新 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.我正在使用在管道中生成的打开的 api 文件来创建,我也想用它来更新 api。

Just use az apim api import again.只需再次使用az apim api import It will replace existing API with new one, preserving policies for operations with the same template.它将用新的 API 替换现有的 API,保留使用相同模板的操作策略。 --path and --api-id parameters must be the same as existing API. --path--api-id参数必须与现有的 API 相同。 Provide url/path to the swagger/openapi and correct specification format.提供 swagger/openapi 的 url/path 和正确的规范格式。 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如果要添加修订而不是更新现有修订,请添加额外--api-revision参数

az apim api update is suited for updating single properties or fields like description, api type etc. az apim api update适用于更新单个属性或字段,如描述、api 类型等。

You can create an API in APIM using this method and delete it using this method.您可以使用方法在 APIM 中创建 API 并使用方法将其删除。

In the azure devops pipeline, we can set the AzureCLI task like this:在 azure devops 管道中,我们可以这样设置 AzureCLI 任务: 在此处输入图像描述

We need to go to the azure portal and find our api ID, and then update the api info.我们需要 go 到 azure 门户网站并找到我们的 api ID,然后更新 Z8A5DA52ED1264727DCA8E 信息。 Here is my test result:这是我的测试结果: 在此处输入图像描述

Please use the below script to create/update API in APIM using az cli.请使用以下脚本使用 az cli 在 APIM 中创建/更新 API。 I am assuming我假设

A. Your api definition is in Open API definition file. A. 您的 api 定义在 Open API 定义文件中。 B. You are identifying apim endpoints using path ($apiPrefix). B. 您正在使用路径 ($apiPrefix) 识别 apim 端点。

$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
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM