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.52k stars 4.6k forks source link

Error Resource not found when create policy fragment #27356

Open nciross opened 1 week ago

nciross commented 1 week ago

Is there an existing issue for this?

Community Note

i want to add new plicy fragment but i have this error

Terraform Version

1.9.1

AzureRM Provider Version

4.1.0

Affected Resource(s)/Data Source(s)

azurerm_api_management_policy_fragment

Terraform Configuration Files

# ########################################################################################################
# Api management policy fragment(apim_pol_frag) / policy fragment to add correlation id
# #########################################################################################################
resource "azurerm_api_management_policy_fragment" "apim_pol_frag_correlation_id" {
  api_management_id = azurerm_api_management.apim_main.id
  name              = "apim-pol-frag-correlation-id"
  format            = "xml"
  value = <<XML
<fragment>
    <choose>
        <when condition="@(context.Request.Headers.ContainsKey("x-correlationId"))">
            <!-- Header is present, pass the request through -->
        </when>
        <otherwise>
            <!-- Header is not present, generate GUID and assign to header -->
            <set-header name="x-correlationId" exists-action="override">
                <value>@(Guid.NewGuid().ToString())</value>
            </set-header>
            <!-- Pass the request through -->
        </otherwise>
    </choose>
</fragment>
XML
 depends_on = [azurerm_api_management.apim_main]
}

Debug Output/Panic Output

│ Error: creating Policy Fragment (Subscription: "8a63b048-0cd0-4001-800e-efbf985f90ca"
│ Resource Group Name: "rg-apimil-dev-francecentral-001"
│ Service Name: "apimapimildevfrancecentral001"
│ Policy Fragment Name: "apim-pol-frag-correlation-id"): polling after CreateOrUpdate: executing request: unexpected status 404 (404 Not Found) with error: ResourceNotFound: PolicyFragment not found.
│ 
│   with module.api_management.azurerm_api_management_policy_fragment.apim_pol_frag_correlation_id,
│   on api-management/main.tf line 68, in resource "azurerm_api_management_policy_fragment" "apim_pol_frag_correlation_id":
│   68: resource "azurerm_api_management_policy_fragment" "apim_pol_frag_correlation_id" {
│

Expected Behaviour

adding "apim-pol-frag-correlation-id

Actual Behaviour

Resource not found

Steps to Reproduce

No response

Important Factoids

No response

References

No response

secura-dschauland commented 6 days ago

I am also seeing this error when trying to create policy fragment resources in APIM. Terraform version is 1.9.2 AzureRM version is 3.116.0

The files are read during plan and display to the console, but fail with a 404 when applying.

sinbai commented 5 days ago

Hi @nciross thanks for opening this issue. Could you please specify the following format and try again?

format = "rawxml"

matt-lethargic commented 4 days ago

I'm seeing this error also with the configuration below Azurerm: 3.116.0 Terrform: 1.9.5

resource "azurerm_api_management_policy_fragment" "rate_limit_jwt" {
  name              = "rate-limit-jwt"
  api_management_id = azurerm_api_management.hub_apim.id
  format            = "rawxml"

  value = <<XML
<fragemnt>
    <rate-limit-by-key calls="90"
        renewal-period="60"
        counter-key="@(context.Request.Headers.GetValueOrDefault("Authorization","").AsJwt()?.Subject)" />
</fragement>
XML
}
sinbai commented 4 days ago

On my side, the resource could be created successfully by following the steps below. Could you try to reproduce the error code with it?

Step1: Create 'azurerm_api_management_policy_fragment' by the following config.

terraform {
  required_providers {
    azurerm = {
      version = "3.116.0" 
    }
  }
}

provider "azurerm" {

  features {}
}

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

resource "azurerm_api_management" "test" {
  name                = "acctestAM-27356-0914"
  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_policy_fragment" "test" {
  api_management_id = azurerm_api_management.test.id
  name              = azurerm_api_management.test.name
  description       = "Some descriptive text which is updated"
  value             =  <<XML
<fragment>
    <choose>
        <when condition="@(context.Request.Headers.ContainsKey("x-correlationId"))">
            <!-- Header is present, pass the request through -->
        </when>
        <otherwise>
            <!-- Header is not present, generate GUID and assign to header -->
            <set-header name="x-correlationId" exists-action="override">
                <value>@(Guid.NewGuid().ToString())</value>
            </set-header>
            <!-- Pass the request through -->
        </otherwise>
    </choose>
</fragment>
XML
  #format            = "rawxml"
}

Result: image

Step2: Uncomment format = "rawxml" in above config, run terraform apply.

Result: image

secura-dschauland commented 2 days ago

Changing the format from XML to RAWXML seems to have solved this issue for me