elastic / terraform-provider-elasticstack

Terraform provider for Elastic Stack
https://registry.terraform.io/providers/elastic/elasticstack/latest/docs
Apache License 2.0
164 stars 85 forks source link

[Bug] Cannot create index with number_of_replicas=0 #238

Closed paivin-dn closed 5 days ago

paivin-dn commented 1 year ago

Describe the bug I've tried to create an index with the parameter number_of_replicas=0, but in an elasticsearch instance, I've got an index with number_of_replicas=1.

To Reproduce Steps to reproduce the behavior:

  1. Define index in a terraform code:
    resource "elasticstack_elasticsearch_index" "example" {
    name               = "example"
    number_of_shards   = 1
    number_of_replicas = 0
    }
  2. Run the terraform plan & terraform plan commands
  3. Check index settings and make sure index.number_of_replicas = 1.

Expected behavior The created index should have index.number_of_replicas = 0 instead of index.number_of_replicas = 0

Versions (please complete the following information):

k-yomo commented 1 year ago

The root cause would be that if raw, ok := d.GetOk(tfFieldKey); ok { will only be true when the value is not zero value and zero value can't be set at create time. It seems that's known issue and can be fixed by migrating to new sdk(terraform-provider-framework) since they have IsNull method to check if not set, or is explicitly set to null. https://github.com/elastic/terraform-provider-elasticstack/blob/c5e409539eeec2ed46d38eb749ff8d760a9d87d3/internal/utils/utils.go#L148-L163

Workaround would be

  1. create with value > 0 and update number_of_replicas to 0 after.
    resource "elasticstack_elasticsearch_index" "example" {
    name               = "example"
    number_of_shards   = 1
    number_of_replicas = 1
    }
  2. use settings field (it's now deprecated though)

    resource "elasticstack_elasticsearch_index" "example" {
    name               = "example"
    number_of_shards   = 1
    
    settings {
    setting {
      name  = "number_of_replicas"
      value = "0"
    }
    }
    }
paivin-dn commented 1 year ago

The root cause would be that if raw, ok := d.GetOk(tfFieldKey); ok { will only be true when the value is not zero value and zero value can't be set at create time. It seems that's known issue and can be fixed by migrating to new sdk(terraform-provider-framework) since they have IsNull method to check if not set, or is explicitly set to null.

https://github.com/elastic/terraform-provider-elasticstack/blob/c5e409539eeec2ed46d38eb749ff8d760a9d87d3/internal/utils/utils.go#L148-L163

Workaround would be

  1. create with value > 0 and update number_of_replicas to 0 after.
resource "elasticstack_elasticsearch_index" "example" {
  name               = "example"
  number_of_shards   = 1
  number_of_replicas = 1
}
  1. use settings field (it's now deprecated though)
resource "elasticstack_elasticsearch_index" "example" {
  name               = "example"
  number_of_shards   = 1

  settings {
    setting {
      name  = "number_of_replicas"
      value = "0"
    }
  }
}

Thank you for your reply!

james-world commented 1 year ago

I ran into this recently - not sure why this bug is closed since it isn't fixed?

olfek commented 1 year ago

I'm seeing this problem with the new version of everything

tobio commented 1 year ago

Requires https://github.com/elastic/terraform-provider-elasticstack/issues/214