将功能定义定义为API管理,作为CI流程的一部分

发布于 2025-02-12 07:19:19 字数 533 浏览 0 评论 0原文

我们希望在每部部署Azure函数的情况下将函数定义导入函数定义的过程自动化。在Azure门户中,有一种非常简单的方法可以通过UI导入函数定义,但是它们似乎并不是任何API/CLI/POWERSHELL库来自动化此过程。

我们设法创建了一个解决方法,该解决方法涉及使用C#保持函数应用程序的最新定义。 OpenAPI库,然后使用az apim导入 cli命令作为我们部署管道的一部分,但感觉就像额外的工作保持OpenAPI定义的最新定义,并且在我们功能的每个端点上都准确,并且最可取要自动将门户网站在引擎盖下导入函数应用程序时所做的事情(这不需要OpenAPI定义在源代码中保持最新状态)。任何帮助将不胜感激。

我们正在为CI/CD管道和释放使用Azure DevOps。

We're looking to automate the process of importing a function definition into our CD process with every deployment of our azure functions. Within the azure portal, there is a very straightforward way to import a function definition through the UI, but their doesn't appear to be any api/cli/powershell library to automate this process.

Azure Portal Function app import

We have managed to create a workaround which involves keeping our function app definitions up to date using the C# OpenApi library and then using the az apim import cli command as part of our deployment pipeline but it feels like extra work keeping the OpenApi definition up to date and accurate on every endpoint of our function and it would be preferable to automate what the portal is doing under the hood when you import a function app (this does not require an openapi definition being kept up to date in source code). Any help would be greatly appreciated.

We are using Azure Devops for our CI/CD pipelines and releases.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万劫不复 2025-02-19 07:19:19

您有一些选择。您没有指定您是否已经有一些现有的基础架构CI工具,但是我最熟悉在Terraform中这样做。

有关如何将Terraform设置到管道中的教程。

一旦您将Terraform整合到了Terraform之后,就可以轻松将API定义添加到您的APIM实例中。并将其链接到您的功能后端。

文章及其一些代码样本:

resource "azurerm_api_management_backend" "example" {
  name                = "example-backend"
  resource_group_name = data.azurerm_resource_group.example.name
  api_management_name = data.azurerm_api_management.example.name
  protocol            = "http"
  url                 = "https://${azurerm_function_app.example.name}.azurewebsites.net/api/"

  credentials {
    header = {
      "x-functions-key" = "${data.azurerm_function_app_host_keys.example.default_function_key}"
    }
  }
}

resource "azurerm_api_management_api" "example" {
  name                = "example-api"
  resource_group_name = data.azurerm_resource_group.example.name
  api_management_name = data.azurerm_api_management.example.name
  revision            = "1"
  display_name        = "Example API"
  path                = "example"
  protocols           = ["https"]

  import {
    content_format = "openapi"
    content_value  = file("${path.module}/FuncOpenAPI3.yml")
  }
}

resource "azurerm_api_management_api_policy" "example" {
  api_name            = azurerm_api_management_api.example.name
  api_management_name = azurerm_api_management_api.example.api_management_name
  resource_group_name = azurerm_api_management_api.example.resource_group_name

  xml_content = <<XML
<policies>
  <inbound>
    <base/>
    <set-backend-service backend-id="example-backend" />
  </inbound>
</policies>
XML
}

现在,正如您在本文中看到的那样,它谈到了导入OpenAPI文件以定义实际API。没有一个很好的方法可以自动执行此操作,但是有一种使用Swagger的内置的Tofile工具:

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Command="dotnet tool restore" />
    <Exec Command="dotnet swagger tofile --output swagger.json $(OutputPath)\$(AssemblyName).dll v1 " />
</Target>

当您在管道中运行构建时,将生成此文件,然后您可以在基础架构阶段将其引用OpenAPI文件自动在APIM中生成该API。

You've got a few options for this. You didn't specify if you already have some existing infrastructure CI tools, but I'm most familiar with doing this in terraform.

Tutorial on how to setup terraform into your pipeline.

Once you have terraform integrated you can easily add API definitions into your APIM instance. And link that up to your function backend.

Article and some code samples from it:

resource "azurerm_api_management_backend" "example" {
  name                = "example-backend"
  resource_group_name = data.azurerm_resource_group.example.name
  api_management_name = data.azurerm_api_management.example.name
  protocol            = "http"
  url                 = "https://${azurerm_function_app.example.name}.azurewebsites.net/api/"

  credentials {
    header = {
      "x-functions-key" = "${data.azurerm_function_app_host_keys.example.default_function_key}"
    }
  }
}

resource "azurerm_api_management_api" "example" {
  name                = "example-api"
  resource_group_name = data.azurerm_resource_group.example.name
  api_management_name = data.azurerm_api_management.example.name
  revision            = "1"
  display_name        = "Example API"
  path                = "example"
  protocols           = ["https"]

  import {
    content_format = "openapi"
    content_value  = file("${path.module}/FuncOpenAPI3.yml")
  }
}

resource "azurerm_api_management_api_policy" "example" {
  api_name            = azurerm_api_management_api.example.name
  api_management_name = azurerm_api_management_api.example.api_management_name
  resource_group_name = azurerm_api_management_api.example.resource_group_name

  xml_content = <<XML
<policies>
  <inbound>
    <base/>
    <set-backend-service backend-id="example-backend" />
  </inbound>
</policies>
XML
}

Now as you can see in this article it talks about importing an OpenApi file to define the actual api. There isn't a great way to do this automatically, but there is a way using swagger's built in tofile tool:

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Command="dotnet tool restore" />
    <Exec Command="dotnet swagger tofile --output swagger.json $(OutputPath)\$(AssemblyName).dll v1 " />
</Target>

When you run your build in your pipeline this file will be generated and then you can reference it in your infrastructure stage to use that OpenAPI file to automatically generate that API in APIM.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文