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

查看:79
本文介绍了如何从使用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是

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.

  • Delete an API
  • Delete a product

Azure CLI中有一个模块,可让您

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 将这些命令嵌入terraform中带有空资源.

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天全站免登陆