Implements delete index with query parameters

This commit is contained in:
Hakan Uyumaz 2022-11-23 16:52:48 +03:00
Родитель 2b93a65189
Коммит c5cb8356d9
2 изменённых файлов: 51 добавлений и 1 удалений

21
es.go
Просмотреть файл

@ -767,9 +767,28 @@ 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, len(queryParamMap))
for key, value := range queryParamMap {
queryParams = append(queryParams, fmt.Sprintf("%s=%s", key,
strings.Join(value, ",")))
}
queryString := strings.Join(queryParams, "&")
var urlBuilder strings.Builder
urlBuilder.WriteString(fmt.Sprintf("%s?%s", indexName, queryString))
agent := c.buildDeleteRequest(urlBuilder.String())
var response acknowledgedResponse
err := handleErrWithStruct(c.buildDeleteRequest(indexName), &response)
err := handleErrWithStruct(agent, &response)
if err != nil {
return err

Просмотреть файл

@ -19,6 +19,7 @@ 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)
}
@ -26,6 +27,7 @@ type ServerSetup struct {
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
@ -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 {
@ -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",