hashicorp / terraform-provider-azurerm

Terraform provider for Azure Resource Manager
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
Mozilla Public License 2.0
4.61k stars 4.65k forks source link

Microsoft.ApiManagement Error: unexpected segment "policies/policy" in 3.92.0 on Import #25190

Closed ron-foreman closed 8 months ago

ron-foreman commented 8 months ago

Is there an existing issue for this?

Community Note

Terraform Version

1.7.0

AzureRM Provider Version

3.92.0

Affected Resource(s)/Data Source(s)

azurerm_api_management_api_policy

Terraform Configuration Files

resource "azurerm_api_management_api_policy" "api_policies" {
  count               = length(var.apis_arguments)
  api_name            = var.apis_arguments[count.index].name
  resource_group_name = data.azurerm_api_management.apim.resource_group_name
  api_management_name = data.azurerm_api_management.apim.name

  xml_content = file(abspath(var.apis_arguments[count.index].policy_content))

 depends_on = [
    azurerm_api_management_api.apis,
    azurerm_api_management_named_value.named_values
  ]
}

Debug Output/Panic Output

╷
│ Error: parsing "/subscriptions/.../resourceGroups/.../providers/Microsoft.ApiManagement/service/.../apis/.../policies/papi/apis/.../policies/policy")
│
│
╵

Expected Behaviour

module.apim.azurerm_api_management_api_policy.apim_api_policy: Import prepared!

Prepared azurerm_api_management_api_operation for import

Import successful!

The resources that were imported are shown above. These resources are now in your Terraform state and will henceforth be managed by Terraform.

Actual Behaviour

╷ │ Error: parsing "/subscriptions/.../resourceGroups/.../providers/Microsoft.ApiManagement/service/.../apis/.../policies/papi/apis/.../policies/policy") │ │ ╵

Steps to Reproduce

terraform import azurerm_api_management_api_policy.apim_api_policy

Important Factoids

No response

References

This error keeps coming up

Issue 23095

Issue 23036

sinbai commented 8 months ago

Hi @ron-foreman thanks for opening this issue. Unfortunately, I could not reproduce the issue with tf config and repro steps below. Please let me know if I missed anything crucial.

Step1: Create a azurerm_api_management_api with the following tf config.

terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "3.92.0"
    }
  }
}

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "test" {
  name     = "acctestRG-0311"
  location = "eastus"
}

resource "azurerm_api_management" "test" {
  name                = "acctestAM-0311"
  location            = azurerm_resource_group.test.location
  resource_group_name = azurerm_resource_group.test.name
  publisher_name      = "pub1"
  publisher_email     = "pub1@email.com"
  sku_name            = "Consumption_0"
}

resource "azurerm_api_management_api" "test" {
  name                = "acctestapi-0311"
  resource_group_name = azurerm_resource_group.test.name
  api_management_name = azurerm_api_management.test.name
  display_name        = "api1"
  path                = "api1"
  protocols           = ["https"]
  revision            = "1"
}

resource "azurerm_api_management_api_policy" "api_policies" {
  api_name            =  azurerm_api_management_api.test.name
  resource_group_name = azurerm_api_management.test.resource_group_name
  api_management_name =azurerm_api_management.test.name

 xml_content = <<XML
<policies>
  <inbound>
    <find-and-replace from="xyz" to="abc" />
  </inbound>
</policies>
XML
}

Step2: Import the azurerm_api_management_api_policy created by the above config with command terraform import azurerm_api_management_api_policy.api_policies /subscriptions/xxx/resourceGroups/acctestRG-0311/providers/Microsoft.ApiManagement/service/acctestAM-0311/apis/acctestapi-0311

Actual: image

It is worth mentioning that per the Terraform documentation, the command to import azurerm_api_management_api_policy does not include /policies/policy.

github-actions[bot] commented 7 months ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.