diff --git a/opensearchapi/api_aliases-params.go b/opensearchapi/api_aliases-params.go new file mode 100644 index 000000000..d8c17cc0b --- /dev/null +++ b/opensearchapi/api_aliases-params.go @@ -0,0 +1,65 @@ +// SPDX-License-Identifier: Apache-2.0 +// +// The OpenSearch Contributors require contributions made to +// this file be licensed under the Apache-2.0 license or a +// compatible open source license. +// +// Modifications Copyright OpenSearch Contributors. See +// GitHub history for details. + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package opensearchapi + +import "time" + +// AliasesParams represents possible parameters for the AliasesReq +type AliasesParams struct { + MasterTimeout time.Duration + ClusterManagerTimeout time.Duration + Timeout time.Duration + + Pretty bool + Human bool + ErrorTrace bool +} + +func (r AliasesParams) get() map[string]string { + params := make(map[string]string) + + if r.MasterTimeout != 0 { + params["master_timeout"] = formatDuration(r.MasterTimeout) + } + + if r.ClusterManagerTimeout != 0 { + params["cluster_manager_timeout"] = formatDuration(r.ClusterManagerTimeout) + } + + if r.Timeout != 0 { + params["timeout"] = formatDuration(r.Timeout) + } + + if r.Pretty { + params["pretty"] = "true" + } + + if r.Human { + params["human"] = "true" + } + + if r.ErrorTrace { + params["error_trace"] = "true" + } + + return params +} diff --git a/opensearchapi/api_aliases.go b/opensearchapi/api_aliases.go new file mode 100644 index 000000000..cedb03607 --- /dev/null +++ b/opensearchapi/api_aliases.go @@ -0,0 +1,73 @@ +// SPDX-License-Identifier: Apache-2.0 +// +// The OpenSearch Contributors require contributions made to +// this file be licensed under the Apache-2.0 license or a +// compatible open source license. +// +// Modifications Copyright OpenSearch Contributors. See +// GitHub history for details. + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package opensearchapi + +import ( + "context" + "io" + "net/http" + + "github.com/opensearch-project/opensearch-go/v2" +) + +// Aliases executes an /_aliases request with the required AliasesReq +func (c Client) Aliases(ctx context.Context, req AliasesReq) (*AliasesResp, error) { + var ( + data AliasesResp + err error + ) + if data.response, err = c.do(ctx, req, &data); err != nil { + return &data, err + } + + return &data, nil +} + +// AliasesReq represents possible options for the / request +type AliasesReq struct { + Body io.Reader + + Header http.Header + Params AliasesParams +} + +// GetRequest returns the *http.Request that gets executed by the client +func (r AliasesReq) GetRequest() (*http.Request, error) { + return opensearch.BuildRequest( + "POST", + "/_aliases", + r.Body, + r.Params.get(), + r.Header, + ) +} + +// AliasesResp represents the returned struct of the / response +type AliasesResp struct { + Acknowledged bool `json:"acknowledged"` + response *opensearch.Response +} + +// Inspect returns the Inspect type containing the raw *opensearch.Reponse +func (r AliasesResp) Inspect() Inspect { + return Inspect{Response: r.response} +} diff --git a/opensearchapi/api_aliases_test.go b/opensearchapi/api_aliases_test.go new file mode 100644 index 000000000..3c0731432 --- /dev/null +++ b/opensearchapi/api_aliases_test.go @@ -0,0 +1,72 @@ +// SPDX-License-Identifier: Apache-2.0 +// +// The OpenSearch Contributors require contributions made to +// this file be licensed under the Apache-2.0 license or a +// compatible open source license. +// +// Modifications Copyright OpenSearch Contributors. See +// GitHub history for details. + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//go:build integration + +package opensearchapi_test + +import ( + "strings" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/opensearch-project/opensearch-go/v2/opensearchapi" + osapitest "github.com/opensearch-project/opensearch-go/v2/opensearchapi/internal/test" +) + +func TestAliases(t *testing.T) { + t.Run("Aliases", func(t *testing.T) { + client, err := opensearchapi.NewDefaultClient() + require.Nil(t, err) + + index := "test-aliases" + t.Cleanup(func() { + client.Indices.Delete(nil, opensearchapi.IndicesDeleteReq{Indices: []string{index}}) + }) + + _, err = client.Indices.Create(nil, opensearchapi.IndicesCreateReq{Index: index}) + require.Nil(t, err) + + t.Run("with request", func(t *testing.T) { + resp, err := client.Aliases( + nil, + opensearchapi.AliasesReq{ + Body: strings.NewReader(`{"actions":[{"add":{"index":"test-aliases","alias":"logs"}},{"remove":{"index":"test-aliases","alias":"logs"}}]}`), + }, + ) + require.Nil(t, err) + require.NotEmpty(t, resp) + require.NotEmpty(t, resp.Inspect().Response) + osapitest.CompareRawJSONwithParsedJSON(t, resp, resp.Inspect().Response) + }) + + t.Run("inspect", func(t *testing.T) { + failingClient, err := osapitest.CreateFailingClient() + require.Nil(t, err) + + res, err := failingClient.Aliases(nil, opensearchapi.AliasesReq{}) + require.NotNil(t, err) + require.NotNil(t, res) + osapitest.VerifyInspect(t, res.Inspect()) + }) + }) +}