Skip to content

Commit

Permalink
Merge pull request #103 from github/uyumazhakan/add-delete-index-with…
Browse files Browse the repository at this point in the history
…-query-params

Implements delete index with query parameters
  • Loading branch information
UyumazHakan committed Dec 7, 2022
2 parents a99df93 + 6a3677b commit a7e2e17
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
18 changes: 17 additions & 1 deletion es.go
Original file line number Diff line number Diff line change
Expand Up @@ -779,9 +779,25 @@ func (c *Client) ModifyAliases(actions []AliasAction) error {
//
// Use case: You want to remove an index and all of its data.
func (c *Client) DeleteIndex(indexName string) error {
return c.DeleteIndexWithQueryParameters(indexName, nil)
}

// Delete an index in the cluster with query parameters.
//
// Use case: You want to remove an index and all of its data. You also want to
// specify query parameters such as timeout.
func (c *Client) DeleteIndexWithQueryParameters(indexName string, queryParamMap map[string][]string) error {
queryParams := make([]string, 0, len(queryParamMap))
for key, value := range queryParamMap {
queryParams = append(queryParams, fmt.Sprintf("%s=%s", key,
strings.Join(value, ",")))
}
queryString := strings.Join(queryParams, "&")

agent := c.buildDeleteRequest(fmt.Sprintf("%s?%s", indexName, queryString))
var response acknowledgedResponse

err := handleErrWithStruct(c.buildDeleteRequest(indexName), &response)
err := handleErrWithStruct(agent, &response)

if err != nil {
return err
Expand Down
31 changes: 31 additions & 0 deletions es_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ import (
// ServerSetup type contains the Method, Path, Body and Response strings, as well as the HTTP Status code.
type ServerSetup struct {
Method, Path, Body, Response string
QueryParams url.Values
HTTPStatus int
extraChecksFn func(t *testing.T, r *http.Request)
}

func buildTestServer(t *testing.T, setups []*ServerSetup, tls bool) *httptest.Server {
handlerFunc := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestBytes, _ := ioutil.ReadAll(r.Body)
requestQueryParameters := r.URL.Query()
requestBody := string(requestBytes)

matched := false
Expand All @@ -38,6 +40,14 @@ func buildTestServer(t *testing.T, setups []*ServerSetup, tls bool) *httptest.Se
t.Fatalf("request body not matching: %s != %s", requestBody, setup.Body)
}

if setup.QueryParams != nil {
for key, value := range setup.QueryParams {
if requestQueryParameters.Get(key) != value[0] {
t.Fatalf("request query parameter not matching: %s != %s", requestQueryParameters.Get(key), value[0])
}
}
}

if r.Method == setup.Method && r.URL.EscapedPath() == setup.Path && requestBody == setup.Body {
matched = true
if setup.HTTPStatus == 0 {
Expand Down Expand Up @@ -549,6 +559,27 @@ func TestDeleteIndex(t *testing.T) {
}
}

func TestDeleteIndexWithQueryParameters(t *testing.T) {
testSetup := &ServerSetup{
Method: "DELETE",
Path: "/badindex",
QueryParams: map[string][]string{
"timeout": {"1m"},
},
Response: `{"acknowledged": true}`,
}

host, port, ts := setupTestServers(t, []*ServerSetup{testSetup})
defer ts.Close()
client := NewClient(host, port)

err := client.DeleteIndexWithQueryParameters("badindex", map[string][]string{"timeout": {"1m"}})

if err != nil {
t.Errorf("Unexpected error expected nil, got %s", err)
}
}

func TestOpenIndex(t *testing.T) {
testSetup := &ServerSetup{
Method: "POST",
Expand Down

0 comments on commit a7e2e17

Please sign in to comment.