首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >使用本地Open标准文件为web服务创建ARM模板

使用本地Open标准文件为web服务创建ARM模板
EN

Stack Overflow用户
提问于 2018-08-27 08:14:42
回答 6查看 2K关注 0票数 0

我正在开发一个旧的web服务,在该服务中,我使用自定义工具生成符合美洲国家组织标准的其他端点文档。使用这个美洲国家组织的json文件,我可以通过门户将API部署到Azure管理服务中,这一切都很好。但是,我需要自动化这个过程,因此需要使用ARM模板将所有web服务部署到Azure APIM。我一直在研究提供的https://learn.microsoft.com/en-us/azure/templates/microsoft.apimanagement/service/apis示例,但似乎无法理解如何在github中使用本地OAS.json文件或文件。

代码语言:javascript
代码运行次数:0
运行
复制
{
  "$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"        
      }
    }
  ]
}
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2019-06-26 01:27:49

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

代码语言:javascript
代码运行次数:0
运行
复制
"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函数是这样的:

代码语言:javascript
代码运行次数:0
运行
复制
#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;
    }
}
票数 0
EN

Stack Overflow用户

发布于 2018-09-04 12:12:15

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

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

票数 2
EN

Stack Overflow用户

发布于 2019-01-31 10:19:40

您可以使用类型为Azure Resource ManagerMicrosoft.ApiManagement/service/apis模板部署API,要使用Open / swagger定义,需要指定模板contentValuecontentFormat参数

代码语言:javascript
代码运行次数:0
运行
复制
{
  "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
    }
 }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52035173

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档