Skip to content

Commit

Permalink
opensearchapi: add pointInTimeClient
Browse files Browse the repository at this point in the history
Signed-off-by: Jakob Hahn <[email protected]>
  • Loading branch information
Jakob3xD committed Sep 21, 2023
1 parent 7a964e3 commit 52c63a4
Show file tree
Hide file tree
Showing 8 changed files with 594 additions and 0 deletions.
77 changes: 77 additions & 0 deletions opensearchapi/api_point_in_time-create-params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// 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"
)

// PointInTimeCreateParams represents possible parameters for the PointInTimeCreateReq
type PointInTimeCreateParams struct {
KeepAlive time.Duration
Preference string
Routing string
ExpandWildcards string
AllowPartialPitCreation bool

Pretty bool
Human bool
ErrorTrace bool
}

func (r PointInTimeCreateParams) get() map[string]string {
params := make(map[string]string)

if r.KeepAlive != 0 {
params["keep_alive"] = formatDuration(r.KeepAlive)
}

if r.Preference != "" {
params["preference"] = r.Preference
}

if r.Routing != "" {
params["routing"] = r.Routing
}

if r.ExpandWildcards != "" {
params["expand_wildcards"] = r.ExpandWildcards
}

if r.AllowPartialPitCreation {
params["allow_partial_pit_creation"] = "true"
}

if r.Pretty {
params["pretty"] = "true"
}

if r.Human {
params["human"] = "true"
}

if r.ErrorTrace {
params["error_trace"] = "true"
}

return params
}
76 changes: 76 additions & 0 deletions opensearchapi/api_point_in_time-create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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 (
"net/http"
"strings"

"github.com/opensearch-project/opensearch-go/v2"
)

// PointInTimeCreateReq represents possible options for the index create request
type PointInTimeCreateReq struct {
Indices []string

Header http.Header
Params PointInTimeCreateParams
}

// GetRequest returns the *http.Request that gets executed by the client
func (r PointInTimeCreateReq) GetRequest() (*http.Request, error) {
indices := strings.Join(r.Indices, ",")

var path strings.Builder
path.Grow(len("//_search/point_in_time") + len(indices))
if len(r.Indices) > 0 {
path.WriteString("/")
path.WriteString(indices)
}
path.WriteString("/_search/point_in_time")

return opensearch.BuildRequest(
"POST",
path.String(),
nil,
r.Params.get(),
r.Header,
)
}

// PointInTimeCreateResp represents the returned struct of the index create response
type PointInTimeCreateResp struct {
PitID string `json:"pit_id"`
Shards struct {
Total int `json:"total"`
Successful int `json:"successful"`
Skipped int `json:"skipped"`
Failed int `json:"failed"`
} `json:"_shards"`
CreationTime int64 `json:"creation_time"`
response *opensearch.Response
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r PointInTimeCreateResp) Inspect() Inspect {
return Inspect{Response: r.response}
}
47 changes: 47 additions & 0 deletions opensearchapi/api_point_in_time-delete-params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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

// PointInTimeDeleteParams represents possible parameters for the PointInTimeDeleteReq
type PointInTimeDeleteParams struct {
Pretty bool
Human bool
ErrorTrace bool
}

func (r PointInTimeDeleteParams) get() map[string]string {
params := make(map[string]string)

if r.Pretty {
params["pretty"] = "true"
}

if r.Human {
params["human"] = "true"
}

if r.ErrorTrace {
params["error_trace"] = "true"
}

return params
}
79 changes: 79 additions & 0 deletions opensearchapi/api_point_in_time-delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// 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 (
"bytes"
"encoding/json"
"io"
"net/http"

"github.com/opensearch-project/opensearch-go/v2"
)

// PointInTimeDeleteReq represents possible options for the index create request
type PointInTimeDeleteReq struct {
PitID []string

Header http.Header
Params PointInTimeDeleteParams
}

// GetRequest returns the *http.Request that gets executed by the client
func (r PointInTimeDeleteReq) GetRequest() (*http.Request, error) {
var body io.Reader
if len(r.PitID) > 0 {
bodyStruct := PointInTimeDeleteRequestBody{PitID: r.PitID}
bodyJSON, err := json.Marshal(bodyStruct)
if err != nil {
return nil, err
}
body = bytes.NewBuffer(bodyJSON)
}

return opensearch.BuildRequest(
"DELETE",
"/_search/point_in_time",
body,
r.Params.get(),
r.Header,
)
}

// PointInTimeDeleteRequestBody is used to from the delete request body
type PointInTimeDeleteRequestBody struct {
PitID []string `json:"pit_id"`
}

// PointInTimeDeleteResp represents the returned struct of the index create response
type PointInTimeDeleteResp struct {
Pits []struct {
PitID string `json:"pit_id"`
Successful bool `json:"successful"`
} `json:"pits"`
response *opensearch.Response
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r PointInTimeDeleteResp) Inspect() Inspect {
return Inspect{Response: r.response}
}
47 changes: 47 additions & 0 deletions opensearchapi/api_point_in_time-get-params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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

// PointInTimeGetParams represents possible parameters for the PointInTimeGetReq
type PointInTimeGetParams struct {
Pretty bool
Human bool
ErrorTrace bool
}

func (r PointInTimeGetParams) get() map[string]string {
params := make(map[string]string)

if r.Pretty {
params["pretty"] = "true"
}

if r.Human {
params["human"] = "true"
}

if r.ErrorTrace {
params["error_trace"] = "true"
}

return params
}
60 changes: 60 additions & 0 deletions opensearchapi/api_point_in_time-get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// 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 (
"net/http"

"github.com/opensearch-project/opensearch-go/v2"
)

// PointInTimeGetReq represents possible options for the index create request
type PointInTimeGetReq struct {
Header http.Header
Params PointInTimeGetParams
}

// GetRequest returns the *http.Request that gets executed by the client
func (r PointInTimeGetReq) GetRequest() (*http.Request, error) {
return opensearch.BuildRequest(
"GET",
"/_search/point_in_time/_all",
nil,
r.Params.get(),
r.Header,
)
}

// PointInTimeGetResp represents the returned struct of the index create response
type PointInTimeGetResp struct {
Pits []struct {
PitID string `json:"pit_id"`
CreationTime int `json:"creation_time"`
KeepAlive int64 `json:"keep_alive"`
} `json:"pits"`
response *opensearch.Response
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r PointInTimeGetResp) Inspect() Inspect {
return Inspect{Response: r.response}
}
Loading

0 comments on commit 52c63a4

Please sign in to comment.