使用本地Open API Standard文件创建Web服务的ARM模板

时间:2018-08-27 08:14:43

标签: json azure azure-api-management

我正在使用旧的Web服务,在该服务中,我使用自定义工具生成了符合OAS标准的其余端点文档。使用此OAS json文件,我可以通过门户将API部署到Azure API Managements服务,并且一切正常。但是,我需要使此过程自动化,因此需要使用ARM模板将所有Web服务部署到Azure APIM。我一直在研究https://docs.microsoft.com/en-us/azure/templates/microsoft.apimanagement/service/apis提供的示例,但似乎无法为如何使用本地OAS.json文件或github中的文件而烦恼。

{
  "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    }
  },
  "variables": {
    "apiManagementServiceName": "price-capture"
  },
  "resources": [    
    {      
      "apiVersion": "2018-01-01",
      "type": "Microsoft.ApiManagement/service/apis",
      "name": "[variables('apiManagementServiceName')]",
      "properties": {
        "displayName": "Service display Name",
        "apiRevision": "1",
        "description": "API description",
      //need help since it's not a swagger url 
      //wondering if there is a way to ref a local file like the option 
      //provided in the portal when we register api's manually.
        "serviceUrl": "----", 
        "path": "----",                            
        "protocols": [    
            "https"
        ],            
        "isCurrent": true,
        "apiVersion": "v1",
        "apiVersionDescription": "apiVersionDescription"        
      }
    }
  ]
}

6 个答案:

答案 0 :(得分:1)

您可以通过ARM模板在API Management上部署和配置整个API,但不能使用本地文件来提供OpenApi / Swagger。 在您的情况下,需要公开访问OpenApi / Swagger,以便资源管理器可以读取它,因此,如果可以自由访问Github URL,它应该可以工作。 我通常将OpenApi / Swagger存储到一个存储帐户,然后使用SAS令牌从ARM模板访问它。

您可以查看此博客,以获取有关在APIM中自动进行API部署的详细信息: https://blog.eldert.net/api-management-ci-cd-using-arm-templates-linked-template/

答案 1 :(得分:0)

我认为无法通过模板部署API配置。

我一直在尝试自己解决这个问题,但是我很确定您不能在服务中包含所需的实际API。

据我所知,您不能使用GIT存储库来执行此操作,因为它需要在门户网站中手动创建的身份验证

我认为唯一可以使用ARM模板自动化的是实际的API管理服务,然后您需要使用Azure API在其上添加和配置API。

但是,我还没有自己弄清楚该怎么做。

我实际上有一张服务单可以获取帮助。

答案 2 :(得分:0)

您可以使用Azure Resource Manager类型的Microsoft.ApiManagement/service/apis模板来部署API,并且要使用Open API / swagger定义,您需要指定contentValuecontentFormat template

的参数
{
  "name": "awesome-api-management/petstore",
  "type": "Microsoft.ApiManagement/service/apis",
  "apiVersion": "2018-06-01-preview",
  "properties": {
    "path": "petstore"
    "contentValue": "petstore swagger file contents here", // or it's URL
    "contentFormat": "swagger-json", // or swagger-link-json if externally available
    }
 }

答案 3 :(得分:0)

API略有更改,因此可以正常工作:

首先需要将yaml文件(calculatorApiFile)上传到Blob存储,但这可以作为部署管道的一部分完成

 {
            "type": "Microsoft.ApiManagement/service/apis",
            "apiVersion": "2019-01-01",
            "name": "[concat(parameters('service_name'), '/b12b1d5ab8204cg6b695e3e861fdd709')]",
            "dependsOn": [
                "[resourceId('Microsoft.ApiManagement/service', parameters('service_name'))]"
            ],
            "properties": {
                "displayName": "Calculator",
                "apiRevision": "1",
                "description": "A simple Calculator ",                   
                "path": "calc",
                "value": "[concat(parameters('containerUri'), parameters('calculatorApiFile'), parameters('containerSasToken'))]",
                "format": "openapi-link",
                "protocols": [
                    "https"
                ],
                "isCurrent": true
            }
        }  

答案 4 :(得分:0)

我想出了答案..我所要做的就是编写一个azure函数,该函数从私有github存储库中获取oas.yaml文件。

"variables":{
    "swagger_json":"[concat(parameters('url_of_azurefunctionwithaccesskey'),'&&githuburi='parameter('raw_url'),'&githubaccesstoken=',parameter('personalaccesstoken')]" 
},
"resources": [
        {
            "type": "Microsoft.ApiManagement/service/apis",
            "name": "[concat(parameters('apimName') ,'/' ,parameters('serviceName'))]",
            "apiVersion": "2018-06-01-preview",
            "properties": {
                "apiRevision": "[parameters('apiRevision')]",
                "path": "pricecapture",
                "contentValue": "[variables('swagger_json')]",
                "contentFormat": "openapi-link"
            }
        }]

我必须编写的Azure函数是这样的:

#r "Newtonsoft.Json"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using System.IO;
using System.Text;

public static async Task<HttpResponseMessage> Run(HttpRequest req, ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    var gitHubUri = req.Query["githuburi"];

    var gitHubAccessToken = req.Query["githubaccesstoken"];
    var encoding = Encoding.ASCII;
    if (string.IsNullOrEmpty(gitHubUri))
    {

        var errorcontent = new StringContent("please pass the raw file content URI (raw.githubusercontent.com) in the request URI string", Encoding.ASCII);
        return new HttpResponseMessage
        {
            StatusCode = HttpStatusCode.BadRequest,
            Content = errorcontent
        };
    }
    else if (string.IsNullOrEmpty(gitHubAccessToken))
    {
        var errorcontent = new StringContent("please pass the GitHub personal access token in the request URI string", Encoding.ASCII);
        return new HttpResponseMessage
        {
            StatusCode = HttpStatusCode.BadRequest,
            Content = errorcontent
        };
    }
    else
    {
        var strAuthHeader = "token " + gitHubAccessToken;
        var client = new HttpClient();
        client.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3.raw");
        client.DefaultRequestHeaders.Add("Authorization", strAuthHeader);
        var response = await client.GetAsync(gitHubUri);
        return response;
    }
}

答案 5 :(得分:0)

如果您将 YAML 加载到一个变量中,该变量可以传递给 ARM 模板并作为值传递:

deploy.bat:

SETLOCAL EnableDelayedExpansion
set API_DEPLOYMENT=<deployment name>
set API_GROUP=<deployment group>
set API=<api file path.yml>
set OPENAPI=
for /f "delims=" %%x in ('type %API%') do set "OPENAPI=!OPENAPI!%%x\n"
call az deployment group create -n %API_DEPLOYMENT% -g %API_GROUP% --mode Complete -f deploy.json -p openApi="!OPENAPI!"
ENDLOCAL

deploy.json(注意replace的使用)

...
        {
            "type": "Microsoft.ApiManagement/service/apis",
            "apiVersion": "2020-12-01",
            "name": "[variables('apiName')]",
            "properties": {
                "path": "[variables('service')]",
                "apiType": "http",
                "displayName": "[variables('apiDisplayName')]",
                "format": "openapi",
                "value": "[replace(parameters('openApi'), '\\n', '\n')]"
            },
            ...
        },
...