Skip to content

Commit

Permalink
opensearchapi: add aliases to root client
Browse files Browse the repository at this point in the history
Signed-off-by: Jakob Hahn <[email protected]>
  • Loading branch information
Jakob3xD committed Sep 20, 2023
1 parent e98f8aa commit 98ee45f
Show file tree
Hide file tree
Showing 3 changed files with 210 additions and 0 deletions.
65 changes: 65 additions & 0 deletions opensearchapi/api_aliases-params.go
Original file line number Diff line number Diff line change
@@ -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
}
73 changes: 73 additions & 0 deletions opensearchapi/api_aliases.go
Original file line number Diff line number Diff line change
@@ -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}
}
72 changes: 72 additions & 0 deletions opensearchapi/api_aliases_test.go
Original file line number Diff line number Diff line change
@@ -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())
})
})
}

0 comments on commit 98ee45f

Please sign in to comment.