datastax / terraform-provider-astra

A project that allows DataStax Astra users to manage their full database lifecycle for Astra Serverless databases (built on Apache Cassandra(TM)) using Terraform
https://registry.terraform.io/providers/datastax/astra
Mozilla Public License 2.0
20 stars 23 forks source link

Fix table import #412

Closed emerkle826 closed 1 month ago

emerkle826 commented 1 month ago

Hi there,

Please provide the following details with your issue report.

Terraform Version

Terraform v1.9.8
on linux_amd64
+ provider registry.terraform.io/datastax/astra v2.3.8

Affected Resource(s)

Please list the resources as a list, for example:

Terraform Configuration Files

main.tf

terraform {
  required_providers {
    astra = {
      source  = "datastax/astra"
      version = "2.3.8"
    }
  }
}

module "testing" {
  source = "./modules/testing"
}

resource astra_database "terra74" {
 name = "terra74"
 keyspace = "testks"
 cloud_provider = "aws"
 regions = ["us-east-1"] 
 deletion_protection = false
}

resource "astra_table" "table" {
  table              = "table1"
  keyspace           = astra_database.terra74.keyspace
  database_id        = astra_database.terra74.id
  region             = "us-east-1"
  clustering_columns = "a:b"
  partition_keys     = "c:d"
  column_definitions = module.testing.column_defs
}

modules/testing/variables.tf

variable "column_definitions" {
  type = list(map(string))

  default = [
    {
      Name: "a"
      Static: false
      TypeDefinition: "text"
    },
    {
      Name: "b"
      Static: false
      TypeDefinition: "text"
    },
    {
      Name: "c"
      Static: false
      TypeDefinition: "text"
    },
    {
      Name: "d"
      Static: false
      TypeDefinition: "text"
    },
    {
      Name: "e"
      Static: false
      TypeDefinition: "text"
    },
    {
      Name: "f"
      Static: false
      TypeDefinition: "text"
    }
  ]
}

modules/testing/outputs.tf

output "column_defs" {
  value = var.column_definitions
}

Debug Output

Panic Output

Expected Behavior

  1. Create DB and table
  2. Remove table from terraform state (so table still exists in Astra, but is not part of terrafrom state)
  3. Import table into terraform
  4. Full table definition is imported into terraform. A Terraform show should shwo the table definition like:
    # astra_table.table:
    resource "astra_table" "table" {
    clustering_columns = "a:b"
    column_definitions = [
        {
            "Name"           = "a"
            "Static"         = "false"
            "TypeDefinition" = "text"
        },
        {
            "Name"           = "b"
            "Static"         = "false"
            "TypeDefinition" = "text"
        },
        {
            "Name"           = "c"
            "Static"         = "false"
            "TypeDefinition" = "text"
        },
        {
            "Name"           = "d"
            "Static"         = "false"
            "TypeDefinition" = "text"
        },
        {
            "Name"           = "e"
            "Static"         = "false"
            "TypeDefinition" = "text"
        },
        {
            "Name"           = "f"
            "Static"         = "false"
            "TypeDefinition" = "text"
        },
    ]
    database_id        = "50a08b2f-30c9-4065-8de3-8f1ab31b0151"
    id                 = "50a08b2f-30c9-4065-8de3-8f1ab31b0151/testks/table1"
    keyspace           = "testks"
    partition_keys     = "c:d"
    region             = "us-east-1"
    table              = "table1"
    }

Actual Behavior

When the table is imported in Step 4 above, the table definition is missing partition_keys, clustering_columns and column_definitions:

# astra_table.table:
resource "astra_table" "table" {
    database_id = "50a08b2f-30c9-4065-8de3-8f1ab31b0151"
    id          = "50a08b2f-30c9-4065-8de3-8f1ab31b0151/testks/table1"
    keyspace    = "testks"
    region      = "us-east-1"
    table       = "table1"
}

Steps to Reproduce

  1. terraform init
  2. terraform apply
  3. terraform show (verify full table definition)
  4. terraform rm astra_table.table
  5. terraform show (verify table is no longer in the terraform state)
  6. terraform import astra_table.table 50a08b2f-30c9-4065-8de3-8f1ab31b0151/us-east-1/testks/table1
  7. terraform show (should see full table definition but it is missing data)

Important Factoids

References

n0rm4l-me commented 1 month ago

Awesome, thanks for the quick fix!