如何从使用 Terraform 创建的 APIM 中删除演示产品? [英] How Can I Remove Demo Products From APIM Created With Terraform?

查看:20
本文介绍了如何从使用 Terraform 创建的 APIM 中删除演示产品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建我的 API 管理实例并使用 Terraform 导入 Swagger API,如下所示:

I create my API Management Instance and import the Swagger API with Terraform like this:

#Create the API Management layer
resource "azurerm_api_management" "apim" {
  name                = "${var.prefix}-apim"
  resource_group_name = var.resource_group_name
  location            = var.resource_group_location

  sku {
    name     = "Developer"
    capacity = 1
  }
}

resource "azurerm_api_management_api" "swagger" {
  name                = "ensurex-transaction-api"
  resource_group_name = var.resource_group_name
  api_management_name = azurerm_api_management.apim.name
  revision            = "1"
  display_name        = "My API"
  path                = "api"
  protocols           = ["https"]

  import {
    content_format = "swagger-json"
    #TODO: Put this in a better place during build/tests
    content_value = file("../../web/out/test/swagger.json")
  }
}

但是,当我打开开发者页面时,有一个名为Echo API"的 API 和名为Starter"和Unlimited"的产品.

However, when I open the developer page there is an api called "Echo API" and products called "Starter" and "Unlimited".

是否有可能一开始就阻止 Terraform 创建这些?

Is it possible to prevent Terraform from creating these in the first place?

或者是否可以在 Terraform 脚本中添加一些内容以在创建后删除它们?

Or is it possible to add something to the Terraform script to delete them after they have been created?

在 terraform 之后,我的下一步是使用 ansible 对资源进行一些配置,所以我可以接受在那里执行此操作的解决方案.

My next step after terraform is some configuration of the resources with ansible so I am OK with a solution that does it there.

但是,我不想使用 Powershell 或将 terraform 替换为 ARM 模板.

However, I don't want to use Powershell or replace terraform with an ARM template.

推荐答案

似乎不可能阻止 terraform 首先创建这些,因为它们是 由底层 SDK 创建 terraform 使用的.

It doesn't appear to be possible to prevent terraform from creating these in the first place as they are created by the underlying SDK that terraform uses.

无法直接使用 Azure CLI,因为它还不支持API 管理.

It is not possible to directly use the Azure CLI as it doesn't yet support API Management.

但是,REST API 确实支持它.

However, the REST API does support it.

Azure CLI 中有一个模块可让您跨平台调用REST API.

There is a module in the Azure CLI that lets you call the REST API in cross platform way.

例如

az rest -m delete -u "https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/my-resource-group-name/providers/Microsoft.ApiManagement/service/my-apim-name/apis/echo-api?api-version=2019-01-01"

这比 curl 等解决方案具有优势,因为它会为您处理身份验证.

This has advantages over solutions like curl as it is handling the authentication for you.

另一个关键点是 {subscriptionId} 会自动替换为正确的值(假设您使用正确的帐户登录)并且您不必查找该值自己.

Another key point is that the {subscriptionId} is automatically substituted for you with the correct value (assuming you are logged in with the correct account) and you don't have to look up the value yourself.

然后可以使用 local-exec 带有 null-resource.

These commands can then be embedded in terraform using the local-exec with a null-resource.

# Create a resource group
resource "azurerm_resource_group" "resource-group" {
  name     = "${var.prefix}_rg"
  location = var.resource_group_location

  tags = var.tags
}
resource "azurerm_api_management" "apim" {
  name                = "${var.prefix}-apim"
  resource_group_name = azurerm_resource_group.resource-group.name
  location            = var.resource_group_location

  sku {
    name     = "Developer"
    capacity = 1
  }
}

resource "null_resource" "clean-apim-api" {
  provisioner "local-exec" {
    command = "az rest -m delete -u "https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/${azurerm_resource_group.resource-group.name}/providers/Microsoft.ApiManagement/service/${azurerm_api_management.apim.name}/apis/echo-api?api-version=2019-01-01""
  }
  depends_on = ["azurerm_api_management.apim"]
}

resource "null_resource" "clean-apim-product-starter" {
  provisioner "local-exec" {
    command = "az rest -m delete -u "https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/${azurerm_resource_group.resource-group.name}/providers/Microsoft.ApiManagement/service/${azurerm_api_management.apim.name}/products/Starter?api-version=2019-01-01""
  }
  depends_on = ["azurerm_api_management.apim"]
}

resource "null_resource" "clean-apim-product-unlimited" {
  provisioner "local-exec" {
    command = "az rest -m delete -u "https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/${azurerm_resource_group.resource-group.name}/providers/Microsoft.ApiManagement/service/${azurerm_api_management.apim.name}/products/Unlimited?api-version=2019-01-01""
  }
  depends_on = ["azurerm_api_management.apim"]
}

这篇关于如何从使用 Terraform 创建的 APIM 中删除演示产品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆