如何从使用Terraform创建的APIM中删除演示产品?

时间:2019-08-16 20:11:51

标签: terraform azure-api-management

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

#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”的产品。

是否可以阻止Terraform首先创建它们?

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

我在terraform之后的下一步是对ansible进行一些资源配置,因此我可以在那里使用解决方案。

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

2 个答案:

答案 0 :(得分:1)

似乎不可能阻止terraform首先创建它们,因为terraform使用created by the underlying SDK

不可能像doesn't yet support API Management那样直接使用Azure CLI。

但是,REST API确实支持它。

Azure CLI中有一个模块,可让您以跨平台的方式call the REST API

例如

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这样的解决方案相比,它具有优势,因为它可以为您处理身份验证。

另一个要点是,{subscriptionId}会自动用正确的值代替您(假设您使用正确的帐户登录),而您不必自己查找该值。

然后可以使用local-execnull-resource将这些命令嵌入terraform中。

# 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"]
}

答案 1 :(得分:0)

使用az v2.12添加以下内容:

resource "null_resource" "clean_api_echo" {
  provisioner "local-exec" {
    command     = "az apim api delete --api-id echo-api --subscription ${var.subscription_id} -g ${azurerm_resource_group.rg.name} -n ${local.apim_name} -y"
    interpreter = ["PowerShell", "-Command"]
  }
  
  depends_on = [
    module.api_manager_api
  ]
}

resource "null_resource" "clean_product_starter" {
  provisioner "local-exec" {
    command     = "az apim product delete --product-id starter --subscription ${var.subscription_id} -g ${azurerm_resource_group.rg.name} -n ${local.apim_name} --delete-subscriptions true -y"
    interpreter = ["PowerShell", "-Command"]
  }

  depends_on = [
    module.api_manager_api
  ]
}

resource "null_resource" "clean_product_unlimited" {
  provisioner "local-exec" {
    command     = "az apim product delete --product-id unlimited --subscription ${var.subscription_id} -g ${azurerm_resource_group.rg.name} -n ${local.apim_name} --delete-subscriptions true -y"
    interpreter = ["PowerShell", "-Command"]
  }

  depends_on = [
    module.api_manager_api
  ]
}

注意:根据您的变量名进行修改。