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.59k stars 4.62k forks source link

azurerm_api_management_redis_cache -"cache_location" should be checked against "Default". currently checks for "default". #24092

Closed ivanthelad closed 7 months ago

ivanthelad commented 10 months ago

Is there an existing issue for this?

Community Note

Terraform Version

1.5.7

AzureRM Provider Version

3.77.0

Affected Resource(s)/Data Source(s)

azurerm_api_management_redis_cache

Terraform Configuration Files

resource "random_pet" "rg_name" {
  prefix = var.resource_group_name_prefix
}

resource "azurerm_resource_group" "rg" {
  name     = random_pet.rg_name.id
  location = var.resource_group_location
}

resource "random_string" "azurerm_api_management_name" {
  length  = 13
  lower   = true
  numeric = false
  special = false
  upper   = false
}

resource "azurerm_api_management" "api" {
  name                = "apiservice${random_string.azurerm_api_management_name.result}"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
  publisher_email     = var.publisher_email
  publisher_name      = var.publisher_name
  sku_name            = "${var.sku}_${var.sku_count}"
}

resource "azurerm_api_management_redis_cache" "example" {
  name              = "rediscacheinst${random_string.azurerm_api_management_name.result}"
  api_management_id = azurerm_api_management.api.id
  connection_string = azurerm_redis_cache.example.primary_connection_string
  description       = "Redis cache instances"
  redis_cache_id    = azurerm_redis_cache.example.id
  cache_location    = "Default"
}

resource "azurerm_redis_cache" "example" {
  name                = "rediscache${random_string.azurerm_api_management_name.result}"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
  capacity            = 1
  family              = "C"
  sku_name            = "Basic"
  enable_non_ssl_port = false
  minimum_tls_version = "1.2"

  redis_configuration {
  }
}

Debug Output/Panic Output

Plan: 5 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + api_management_service_name = (known after apply)
  + resource_group_name         = (known after apply)
╷
│ Error: "default" was not found in the list of supported Azure Locations: "australiacentral,australiacentral2,australiaeast,australiasoutheast,brazilsouth,brazilsoutheast,brazilus,canadacentral,canadaeast,centralindia,centralus,centraluseuap,eastasia,eastus,eastus2,eastus2euap,francecentral,francesouth,germanynorth,germanywestcentral,israelcentral,italynorth,japaneast,japanwest,jioindiacentral,jioindiawest,koreacentral,koreasouth,malaysiasouth,northcentralus,northeurope,norwayeast,norwaywest,polandcentral,qatarcentral,southafricanorth,southafricawest,southcentralus,southeastasia,southindia,swedencentral,swedensouth,switzerlandnorth,switzerlandwest,uaecentral,uaenorth,uksouth,ukwest,westcentralus,westeurope,westindia,westus,westus2,westus3,austriaeast,chilecentral,eastusslv,israelnorthwest,malaysiawest,mexicocentral,newzealandnorth,southeastasiafoundational,spaincentral,taiwannorth,taiwannorthwest"
│
│   with azurerm_api_management_redis_cache.example,
│   on main.tf line 33, in resource "azurerm_api_management_redis_cache" "example":
│   33:   cache_location    = "Default"

Expected Behaviour

That cache_location with value 'Default' should be considered case sensitive. azurerm_api_management_redis_cache should not accept 'default' as a value.

Actual Behaviour

That cache_location with value 'Default' value fails comparison check against "default" and is instead passed to be validated as a real region. This will result in failure and forces users to configure 'default' instead. when 'default' is used APIM cannot reference it

The consequence of this behaviour the terraform provider, using the ARM API creates an External cache entry named with cacheLocation called 'default'. APIM relies on the name "Default" and results in the the cache not been found by the local gateway. The current implementation basically means you cannot provision a default cache via terraform and always need create a cach named "Default" via the ARM API

note: The ARM apim should be more strict regarding its validation so it does not accept any string as a name. it should be Default or a valid region

Steps to Reproduce

terraform plan against the above code snippet

Important Factoids

No response

References

to fix this the following line https://github.com/hashicorp/terraform-provider-azurerm/blob/main/internal/services/apimanagement/validate/redis_cache_location.go#L19 should be changed to

`package validate

import ( "fmt" "github.com/hashicorp/go-azure-helpers/resourcemanager/location" "strings" )

func RedisCacheLocation(input interface{}, key string) (warnings []string, errors []error) { v, ok := input.(string) if !ok { errors = append(errors, fmt.Errorf("expected %q to be a string", key)) return }

if v == "Default" { return warnings, errors }

return location.EnhancedValidate(v, key) } `

Additionally, the original issue was https://github.com/hashicorp/terraform-provider-azurerm/issues/23675
This issue allowed fix basically allows user to provision a default cache regardless if its Default or default and always provisions.a cache location called 'default'. after further tests, the TF provider should perform a strict comparison on "Default"

sinbai commented 10 months ago

Hi @ivanthelad thanks for opening this. I would like to clarify that Terraform provider implements CRUD for Azure resources by calling the Azure Rest API. According to the description of useFromLocation in the latest API , it's value should be either 'default' or valid Azure region identifier. Also, when default is configured, the API returns success and can be displayed normally on the Azure Portal.

For the above reasons, I recommend opening an issue on the Azure Rest API repo to confirm whether this is an API documentation issue.

In addition, if you could provide public documentation or fully explain that its value must be Default, it would be of great help to us in solving this issue.

rcskosir commented 7 months ago

Thanks for taking the time to open this issue. It looks like the behavior you requested is not supported by the underlying Azure API so I am going to label this issue as such and close it for now. If you create a request on Azure/azure-rest-api-specs, feel free to add the link here. When it gets added, we can reopen this request or you can create a new one.

github-actions[bot] commented 6 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.