Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit a7e2e17

Browse files
authored
Merge pull request #103 from github/uyumazhakan/add-delete-index-with-query-params
Implements delete index with query parameters
2 parents a99df93 + 6a3677b commit a7e2e17

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

es.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,9 +779,25 @@ func (c *Client) ModifyAliases(actions []AliasAction) error {
779779
//
780780
// Use case: You want to remove an index and all of its data.
781781
func (c *Client) DeleteIndex(indexName string) error {
782+
return c.DeleteIndexWithQueryParameters(indexName, nil)
783+
}
784+
785+
// Delete an index in the cluster with query parameters.
786+
//
787+
// Use case: You want to remove an index and all of its data. You also want to
788+
// specify query parameters such as timeout.
789+
func (c *Client) DeleteIndexWithQueryParameters(indexName string, queryParamMap map[string][]string) error {
790+
queryParams := make([]string, 0, len(queryParamMap))
791+
for key, value := range queryParamMap {
792+
queryParams = append(queryParams, fmt.Sprintf("%s=%s", key,
793+
strings.Join(value, ",")))
794+
}
795+
queryString := strings.Join(queryParams, "&")
796+
797+
agent := c.buildDeleteRequest(fmt.Sprintf("%s?%s", indexName, queryString))
782798
var response acknowledgedResponse
783799

784-
err := handleErrWithStruct(c.buildDeleteRequest(indexName), &response)
800+
err := handleErrWithStruct(agent, &response)
785801

786802
if err != nil {
787803
return err

es_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ import (
1919
// ServerSetup type contains the Method, Path, Body and Response strings, as well as the HTTP Status code.
2020
type ServerSetup struct {
2121
Method, Path, Body, Response string
22+
QueryParams url.Values
2223
HTTPStatus int
2324
extraChecksFn func(t *testing.T, r *http.Request)
2425
}
2526

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

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

43+
if setup.QueryParams != nil {
44+
for key, value := range setup.QueryParams {
45+
if requestQueryParameters.Get(key) != value[0] {
46+
t.Fatalf("request query parameter not matching: %s != %s", requestQueryParameters.Get(key), value[0])
47+
}
48+
}
49+
}
50+
4151
if r.Method == setup.Method && r.URL.EscapedPath() == setup.Path && requestBody == setup.Body {
4252
matched = true
4353
if setup.HTTPStatus == 0 {
@@ -549,6 +559,27 @@ func TestDeleteIndex(t *testing.T) {
549559
}
550560
}
551561

562+
func TestDeleteIndexWithQueryParameters(t *testing.T) {
563+
testSetup := &ServerSetup{
564+
Method: "DELETE",
565+
Path: "/badindex",
566+
QueryParams: map[string][]string{
567+
"timeout": {"1m"},
568+
},
569+
Response: `{"acknowledged": true}`,
570+
}
571+
572+
host, port, ts := setupTestServers(t, []*ServerSetup{testSetup})
573+
defer ts.Close()
574+
client := NewClient(host, port)
575+
576+
err := client.DeleteIndexWithQueryParameters("badindex", map[string][]string{"timeout": {"1m"}})
577+
578+
if err != nil {
579+
t.Errorf("Unexpected error expected nil, got %s", err)
580+
}
581+
}
582+
552583
func TestOpenIndex(t *testing.T) {
553584
testSetup := &ServerSetup{
554585
Method: "POST",

0 commit comments

Comments
 (0)