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

Http 400 code BadRequest error while trying to update tags on azurerm_notification_hub #23043

Open rem-aj opened 1 year ago

rem-aj commented 1 year ago

Is there an existing issue for this?

Community Note

Terraform Version

1.0.8

AzureRM Provider Version

3.39.0

Affected Resource(s)/Data Source(s)

azurerm_notification_hub_namespace

Terraform Configuration Files

resource "azurerm_notification_hub_namespace" "hub_ns" {
  name = format(
    "%spsc-%snotif-hub-ns",
    var.region_codes[var.location],
    substr(var.environment, 0, 1),
  )
  resource_group_name = var.resource_group
  location            = var.location
  namespace_type      = var.hubnotification_namespacetype
  sku_name            = var.hubnotification_sku
  tags = merge(
    var.common_tags,
    {
      "Role" = "Notification Hub Namespace"
    },
  )
  # https://github.com/hashicorp/terraform-provider-azurerm/issues/14541 and
  # https://github.com/Azure/azure-rest-api-specs/issues/17277
  # Azure API is not returning the namespace_type, so terraform keeps wanting to add it.
  lifecycle {
    ignore_changes = [
      namespace_type
    ]
  }
}

The corresponding plan looks like this

~ update in-place

Terraform will perform the following actions:

  # module.namespacehub.azurerm_notification_hub.staging_psc will be updated in-place
  ~ resource "azurerm_notification_hub" "staging_psc" {
        id                  = "/subscriptions/XXXXXXXXXX/resourceGroups/UE2pscDcommon/providers/Microsoft.NotificationHubs/namespaces/ue2psc-dnotif-hub-ns/notificationHubs/ue2psc-dnotif-hub"
        name                = "ue2psc-dnotif-hub"
      ~ tags                = {
          + "businesscontact" = "XXXXXX"
          + "engcontact"      = "XXXXXX"
          + "project"         = "XXXXXX"
            # (4 unchanged elements hidden)
        }
        # (3 unchanged attributes hidden)

      - timeouts {}

        # (2 unchanged blocks hidden)
    }

Debug Output/Panic Output

module.namespacehub.azurerm_notification_hub_namespace.hub_ns: Modifying... 
╷
│ Error: creating/updating Namespace (Subscription: "XXXXXXXX"
│ Resource Group Name: "UE2pscDcommon"
│ Namespace Name: "ue2psc-dnotif-hub-ns"): namespaces.NamespacesClient#CreateOrUpdate: Failure responding to request: StatusCode=400 -- Original Error: autorest/azure: Service returned an error. Status=400 Code="BadRequest" Message="The JSON value could not be converted to System.Nullable`1[Microsoft.NotificationHubs.Contracts.Arm.NamespaceType]. Path: $.properties.namespaceType | LineNumber: 0 | BytePositionInLine: 69."
│ 
│   with module.namespacehub.azurerm_notification_hub_namespace.hub_ns,
│   on ../modules/namespace_hub/main.tf line 1, in resource "azurerm_notification_hub_namespace" "hub_ns":
│    1: resource "azurerm_notification_hub_namespace" "hub_ns" {
│

Expected Behaviour

The code change was one line invoking a terraform module to add a new set of tags to to common_tags variable

common_tags = merge(var.common_tags, local.project_tags_essentials)

All should have happened is that the 2 extra value tags should have been merged with common_tags variable and the notification would then contain a couple of extra tags.

Actual Behaviour

module.namespacehub.azurerm_notification_hub_namespace.hub_ns: Modifying... ╷ │ Error: creating/updating Namespace (Subscription: "XXXXXXXX" │ Resource Group Name: "UE2pscDcommon" │ Namespace Name: "ue2psc-dnotif-hub-ns"): namespaces.NamespacesClient#CreateOrUpdate: Failure responding to request: StatusCode=400 -- Original Error: autorest/azure: Service returned an error. Status=400 Code="BadRequest" Message="The JSON value could not be converted to System.Nullable`1[Microsoft.NotificationHubs.Contracts.Arm.NamespaceType]. Path: $.properties.namespaceType | LineNumber: 0 | BytePositionInLine: 69." │ │ with module.namespacehub.azurerm_notification_hub_namespace.hub_ns, │ on ../modules/namespace_hub/main.tf line 1, in resource "azurerm_notification_hub_namespace" "hub_ns": │ 1: resource "azurerm_notification_hub_namespace" "hub_ns" { │

Steps to Reproduce

Create a notification hub with a set of tags, try to append extra tags to those existing tags.

Important Factoids

No response

References

No response

sinbai commented 1 year ago

Hi @rem-aj thanks for opening this issue. Could you please provide the value of namespace_type while updating the tags of azurerm_notification_hub? Also, Since I could not reproduce this issue using the following tf config and steps, is it possible to provide the repro steps and terraform log to help reproduce/troubleshoot ?

  1. Create a notification hub with the following tf config.
    
    provider "azurerm" {
    features {}
    }

terraform { required_providers { azurerm = { source = "hashicorp/azurerm" version = "=3.39.0" } } } resource "azurerm_resource_group" "test" { name = "acctestRGpol-0823" location = "eastus" }

resource "azurerm_notification_hub_namespace" "test" { name = "acctestnhn-0823" resource_group_name = azurerm_resource_group.test.name location = azurerm_resource_group.test.location namespace_type = "NotificationHub" sku_name = "Free" }

resource "azurerm_notification_hub" "test" { name = "acctestnh-0823" namespace_name = azurerm_notification_hub_namespace.test.name resource_group_name = azurerm_resource_group.test.name location = azurerm_resource_group.test.location

tags = { env = "Test" } }


2. Update tags of  `azurerm_notification_hub` with follows, then run `terraform apply`

tags = { env = "Test" engcontact = "test2" }



Result:

![image](https://github.com/hashicorp/terraform-provider-azurerm/assets/39109137/76c492c9-d336-4aa2-b30e-78b3a24d6f86)
rem-aj commented 1 year ago

Hi @sinbai here's the value of the namespace type.

"NotificationHub"

By the way this value was already there, it wasn't added as part of this change, this change was simply a tag update. I provided the error logs earlier, are you looking for something different?

Thanks!

sinbai commented 1 year ago

Hi @rem-aj thanks for the reply. Per the provided error logs, It seems that the value of namespace_type that is actually passed to the API is not "NotificationHub". So, is it possible to provide the Terraform Log to help with troubleshooting? Also, could you reproduce this issue with the tf configuration and steps I provided above?

rem-aj commented 1 year ago

Hi @sinbai if you observe the comments in the code, the namespace attribute is actually set to be ignored using lifecycle flag due to the github issues mentioned there which I will post again below. These issues have not been resolved, so by using ignore lifecycle we are able to workaround those specific issues.

https://github.com/hashicorp/terraform-provider-azurerm/issues/14541 and

https://github.com/Azure/azure-rest-api-specs/issues/17277