opensearch-project / terraform-provider-opensearch

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

[BUG] opensearch_index resource does not seem to store imported mappings properly #71

Open david-alpert-nl opened 1 year ago

david-alpert-nl commented 1 year ago

What is the bug?

The opensearch_index resource does not seem to store or manage the mappings property.

How can one reproduce the bug?

  1. Define an opensearch provider with valid connection (e.g. url, credentials, etc) to an existing OpenSearch server with a valid index containing mappings.
  2. Define an opensearch_index resource with a mapping
  3. Import the remote resource into terraform
  4. Run a plan

What is the expected behavior?

What is the actual behavior?

What is your host/environment?

Do you have any additional context?

david-alpert-nl commented 1 year ago

from what I can tell I think this resourceOpensearchIndexRead(...) method is missing the mappings on import as it never explicitly gets them from the opensearch client: https://github.com/opensearch-project/terraform-provider-opensearch/blob/08ca809a1111164e3d8a2fa2d3bfd1ea11700a29/provider/resource_opensearch_index.go#L701-L765

the osClient.IndexGetSettings call uses the /{index}/_settings endpoint which does not return mappings, for example:

{
  "my-index": {
    "settings": {
      "index": {
        "creation_date": "1692630469965",
        "number_of_shards": "5",
        "number_of_replicas": "1",
        "uuid": "M6a4_T9QSJ-EkydkCouCAg",
        "version": {
          "created": "136267827"
        },
        "provided_name": "my-index"
      }
    }
  }
}

I think the provider read method also needs to call the osClient.GetMapping(index) method which uses the/{index}/_mapping/ endpoint to import the existing mapping.

{
  "my-index": {
    "mappings": {
      "properties": {
           ...
      }
    }
  }
}

If I am on the right track I am happy to submit a pull request.

david-alpert-nl commented 1 year ago

I wonder now if this is an issue with API differences?

it appears that OpenSearch 2.9 exposes a more complete response at the GET /{index} endpoint that includes both mappings and settings, perhaps shaped more like this provider code expects 🤔

jordarlu commented 1 year ago

@phillbaker , @prudhvigodithi , would you pls help take a look and comment ?

jordarlu commented 1 year ago

@david-alpert-nl , thanks for reporting the issue and share the finding ,,, would you mind also create a Pull Request for the team here to review and discuss ? thanks again !

prudhvigodithi commented 1 year ago

Thanks @david-alpert-nl, to the provider Read operation we should be adding a condition osClient.GetMapping(index) method, please open a pull request when you are ready, Thanks Adding @phillbaker @bbarani @peterzhuamazon Thank you