opensearch-project / terraform-provider-opensearch

https://registry.terraform.io/providers/opensearch-project/opensearch
Apache License 2.0
73 stars 56 forks source link

[BUG] Import of index resource doesn't include the aliases #108

Open sharathganga opened 10 months ago

sharathganga commented 10 months ago

What is the bug?

When I attempt to import the opensearch_index resource with aliases configured, it doesn't get imported into the state.

How can one reproduce the bug?

Provided an index that was created with aliases

{
  "ci-metrics-000001" : {
    "aliases" : {
      "ci-metrics" : {
        "is_write_index" : true
      }
    },
    "mappings" : { },
    "settings" : {
      "index" : {
        "number_of_shards" : "3",
        "plugins" : {
          "index_state_management" : {
            "rollover_alias" : "ci-metrics"
          }
        },
        "provided_name" : "ci-metrics-000001",
        "creation_date" : "1691569233793",
        "number_of_replicas" : "1",
        "uuid" : "Vm8occETQKqPtTpR5zyFeQ",
        "version" : {
          "created" : "135248027"
        }
      }
    }
  }
}

then run the terraform import command

terraform import opensearch_index.index[\"ci-metrics\"] ci-metrics-000001

but this is how the resource looks in the state

$ terraform state show opensearch_index.index[\"ci-metrics\"]
# opensearch_index.index["ci-metrics"]:
resource "opensearch_index" "index" {
    id                 = "ci-metrics-000001"
    name               = "ci-metrics-000001"
    number_of_replicas = "1"
    number_of_shards   = "3"
    rollover_alias     = "ci-metrics"
}

Finally when I run a terraform plan, it tells me that the index will replaced to add aliases

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create
-/+ destroy and then create replacement

Terraform will perform the following actions:

  # opensearch_index.index["ci-metrics"] must be replaced
-/+ resource "opensearch_index" "index" {
      + aliases            = jsonencode(
            {
              + ci-metrics = {
                  + is_write_index = true
                }
            } # forces replacement
        )
      + force_destroy      = false
      ~ id                 = "ci-metrics-000001" -> (known after apply)
        name               = "ci-metrics-000001"
      - number_of_replicas = "1" -> null
      ~ number_of_shards   = "3" -> (known after apply)
      ~ rollover_alias     = "ci-metrics" -> (known after apply)
    }

What is the expected behavior?

Terraform should import the aliases field and/or any other missing fields

What is your host/environment?

$ uname -a Linux PBL244 5.15.90.1-microsoft-standard-WSL2 #1 SMP Fri Jan 27 02:56:13 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

$ terraform version Terraform v1.5.3 on linux_amd64

Your version of Terraform is out of date! The latest version is 1.6.2. You can update by downloading from https://www.terraform.io/downloads.html

I'm using AWS Opensearch 1.3 and provider version 2.0.0.

peterzhuamazon commented 10 months ago

Hi @rblcoder @premkirank @SkollRyu @afrodidact @tranngocsongtruc could you take a look and add your thoughts?

cc: @prudhvigodithi

Thanks.

c0rmichels commented 10 months ago

I see the same behaviour provider version 2.0.0, opensearch version 2.2.1

prudhvigodithi commented 10 months ago

@rblcoder can you please take a look and add your thoughts ?

rblcoder commented 10 months ago

rollover_alias is a computed attribute. https://discuss.hashicorp.com/t/computed-attributes-and-plan-modifiers/45830/14#:~:text=The%20framework%20will%20automatically%20mark,prevent%20Terraform%20data%20consistency%20errors. says The framework will automatically mark Computed attributes with null configuration as unknown (known after apply) during the plan, which is intentional to prevent Terraform data consistency errors.

rblcoder commented 9 months ago

Also please try setting number_of_shards and number_of_replicas as follows that is as separate key/value pairs instead of within settings, documentation https://registry.terraform.io/providers/jamesanto/opensearch/latest/docs/resources/index -

resource "opensearch_index" "index" {
  name = "test-logs-000001"
  number_of_shards = 3
  number_of_replicas = 1
sharathganga commented 9 months ago

@rblcoder number_of_shards and number_of_replicas on the index are being managed from opensearch_index_template resource