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.63k forks source link

azurerm_cosmosdb_cassandra_table fails with API error when setting cluster_key #15250

Closed daniel-anova closed 2 years ago

daniel-anova commented 2 years ago

Community Note

Terraform (and AzureRM Provider) Version

Terraform v1.1.5 on linux_amd64

Affected Resource(s)

Terraform Configuration Files

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "example"
  location = "West Europe"
}

resource "azurerm_cosmosdb_account" "example" {
  name                = "tfex-cosmosdb-table-bug"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  offer_type          = "Standard"

  capabilities {
    name = "EnableCassandra"
  }

  consistency_policy {
    consistency_level = "Strong"
  }

  geo_location {
    location          = "West US"
    failover_priority = 0
  }
}

resource "azurerm_cosmosdb_cassandra_keyspace" "example" {
  name                = "tfex-cosmos-cassandra-keyspace"
  resource_group_name = azurerm_cosmosdb_account.example.resource_group_name
  account_name        = azurerm_cosmosdb_account.example.name
  throughput          = 400
}

resource "azurerm_cosmosdb_cassandra_table" "example" {
  name                  = "testtable"
  cassandra_keyspace_id = azurerm_cosmosdb_cassandra_keyspace.example.id

  schema {
    column {
      name = "timestmap"
      type = lower("timestamp") // If not lower it's seen as difference during plan after applied
    }
    partition_key {
      name = "timestamp"

    }

    cluster_key {
      name     = "timstamp"
      order_by = title(lower("desc")) // Validation expects Asc or Desc exacly
    }
  }
}

Debug Output

https://gist.github.com/daniel-anova/015778d85c0a76ac852e0a58acfcc03d

Expected Behaviour

azurerm should create the table successfuly.

Actual Behaviour

Table fails to create with the following error:

Terraform will perform the following actions:

  # azurerm_cosmosdb_cassandra_table.example will be created
  + resource "azurerm_cosmosdb_cassandra_table" "example" {
      + analytical_storage_ttl = -2
      + cassandra_keyspace_id  = "/subscriptions/f75ef996-fbde-480e-896b-22ea5d745e82/resourceGroups/example/providers/Microsoft.DocumentDB/databaseAccounts/tfex-cosmosdb-table-bug/cassandraKeyspaces/tfex-cosmos-cassandra-keyspace"
      + default_ttl            = (known after apply)
      + id                     = (known after apply)
      + name                   = "testtable"
      + throughput             = (known after apply)

      + schema {
          + cluster_key {
              + name     = "timestamp"
              + order_by = "Desc"
            }

          + column {
              + name = "timestamp"
              + type = "timestamp"
            }

          + partition_key {
              + name = "timestamp"
            }
        }
    }

Plan: 1 to add, 0 to change, 0 to destroy.
azurerm_cosmosdb_cassandra_table.example: Creating...
╷
│ Error: creating Cassandra Table: (Table Name "testtable" / Cassandra Keyspace Name "tfex-cosmos-cassandra-keyspace" / Database Account Name "tfex-cosmosdb-table-bug" / Resource Group "example"): documentdb.CassandraResourcesClient#CreateUpdateCassandraTable: Failure sending request: StatusCode=400 -- Original Error: Code="BadRequest" Message="Unable to parse request payload due to the following reason: 'Unknown definition timestamp referenced in PRIMARY KEY'.\r\nActivityId: 43e6037c-f5c1-4e97-b8d8-aed85294fa2d, Microsoft.Azure.Documents.Common/2.14.0"
│ 
│   with azurerm_cosmosdb_cassandra_table.example,
│   on main.tf line 38, in resource "azurerm_cosmosdb_cassandra_table" "example":
│   38: resource "azurerm_cosmosdb_cassandra_table" "example" {
│ 

Commenting out the cluster_key block applying once, uncommenting it and applying again allows terraform to fully apply the table successfully.

Steps to Reproduce

  1. terraform apply
lonegunmanb commented 2 years ago

Hi @daniel-anova , I think it's not a Terraform azure plugin's bug nor an Azure bug.

According to the cosmosdb document:

Apache Cassandra also has a concept of compound keys. A compound primary key consists of more than one column; the first column is the partition key, and any additional columns are the clustering keys

For example, the following cosmosdb SQL:

CREATE TABLE uprofile.user (
   user text,  
   id int, 
   message text, 
   PRIMARY KEY (user, id));

The partition key is user and the cluster keys is [id].

In your case the partition key and cluster key are same:

CREATE TABLE tfex-cosmos-cassandra-keyspace.testtable (
   timestamp timestamp,  
   PRIMARY KEY (timestamp , timestamp ));

If you try to create a table with this SQL via portal you'll get exactly the same error:

In this case, just removing the cluster key should work.

daniel-anova commented 2 years ago

@lonegunmanb thanks for the clarification. I see that second parameter in PRIMARY KEY is the cluster key and not a second column for composite primary keys.

I'll close this issue down, thank you for you time.

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