Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add import for index mappings #145

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions provider/resource_opensearch_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

"github.com/olivere/elastic/uritemplates"
elastic7 "github.com/olivere/elastic/v7"
)

Expand Down Expand Up @@ -787,5 +788,46 @@ func resourceOpensearchIndexRead(d *schema.ResourceData, meta interface{}) error

indexResourceDataFromSettings(settings, d)

var response *json.RawMessage
var res *elastic7.Response
var mappingsResponse map[string]interface{}
path, err := uritemplates.Expand("/{index}/_mapping", map[string]string{
"index": index,
})
if err != nil {
return err
}
res, err = osClient.PerformRequest(context.TODO(), elastic7.PerformRequestOptions{
Method: "GET",
Path: path,
})
if err != nil {
return err
}
response = &res.Body

err = json.Unmarshal(*response, &mappingsResponse)

if err != nil {
return fmt.Errorf("fail to unmarshal: %v", err)
}

lenMappings := len(mappingsResponse[index].(map[string]interface{})["mappings"].(map[string]interface{}))

if lenMappings == 0 {
return nil
}

jsonString, err := json.Marshal(mappingsResponse[index].(map[string]interface{})["mappings"])
if err != nil {
return fmt.Errorf("fail to marshal: %v", err)
}

err = d.Set("mappings", string(jsonString))

if err != nil {
return err
}

return nil
}
40 changes: 29 additions & 11 deletions provider/resource_opensearch_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ resource "opensearch_index" "test" {
number_of_replicas = 1
}
`

testOpensearchIndexImport = `
resource "opensearch_index" "test1import" {
name = "terraform-test1import"
number_of_shards = 1
number_of_replicas = 1
mappings = jsonencode(
{
"properties" : {
"name" : {
"type" : "text"
}
}
}
)
}
`

testAccOpensearchIndexUpdate1 = `
resource "opensearch_index" "test" {
name = "terraform-test"
Expand Down Expand Up @@ -121,15 +139,15 @@ EOF
resource "opensearch_index" "test_doctype" {
name = "terraform-test"
number_of_replicas = "1"
mappings = <<EOF
{
"properties": {
"name": {
"type": "text"
}
}
}
EOF
mappings = jsonencode(
{
"properties" : {
"name" : {
"type" : "text"
}
}
}
)
}
`
testAccOpensearchIndexUpdateForceDestroy = `
Expand Down Expand Up @@ -436,10 +454,10 @@ func TestAccOpensearchIndex_importBasic(t *testing.T) {
CheckDestroy: checkOpensearchIndexDestroy,
Steps: []resource.TestStep{
{
Config: testAccOpensearchIndex,
Config: testOpensearchIndexImport,
},
{
ResourceName: "opensearch_index.test",
ResourceName: "opensearch_index.test1import",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
Expand Down
Loading