使用本地开放 API 标准文件为 Web 服务创建 ARM 模板



我正在开发一个旧的 Web 服务,在那里我使用自定义工具生成符合 OAS 标准的其余端点文档。使用此 OAS json 文件,我可以通过门户将 API 部署到 Azure API 管理服务,一切正常。但是,我需要自动执行此过程,因此需要使用 ARM 模板将所有 Web 服务部署到 Azure APIM。我一直在研究 https://learn.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"        
}
}
]
}

可以通过 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/

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

{
"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
}
}

我认为不可能通过模板部署 API 配置。

我一直在试图自己解决这个问题,但我很确定你不能在服务中包含你想要的实际 API。

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

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

但是,我自己还没有弄清楚如何做到这一点。

我实际上有一张服务票可以打开以获得帮助。

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

我想出了答案..我所要做的就是编写一个 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;
}
}

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

部署.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!%%xn"
call az deployment group create -n %API_DEPLOYMENT% -g %API_GROUP% --mode Complete -f deploy.json -p openApi="!OPENAPI!"
ENDLOCAL

deploy.json(注意使用替换(

...
{
"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')]"
},
...
},
...

最新更新