Skip to content

Commit

Permalink
opensearchapi: add template 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 7f659eb commit e7e326a
Show file tree
Hide file tree
Showing 10 changed files with 783 additions and 0 deletions.
73 changes: 73 additions & 0 deletions opensearchapi/api_template-create-params.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 (
"strconv"
"time"
)

// TemplateCreateParams represents possible parameters for the TemplateCreateReq
type TemplateCreateParams struct {
Create *bool
MasterTimeout time.Duration
ClusterManagerTimeout time.Duration
Order *int

Pretty bool
Human bool
ErrorTrace bool
}

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

if r.Create != nil {
params["create"] = strconv.FormatBool(*r.Create)
}

if r.MasterTimeout != 0 {
params["master_timeout"] = formatDuration(r.MasterTimeout)
}

if r.ClusterManagerTimeout != 0 {
params["cluster_manager_timeout"] = formatDuration(r.ClusterManagerTimeout)
}

if r.Order != nil {
params["order"] = strconv.FormatInt(int64(*r.Order), 10)
}

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

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

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

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

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

// TemplateCreateReq represents possible options for the index create request
type TemplateCreateReq struct {
Template string

Body io.Reader

Header http.Header
Params TemplateCreateParams
}

// GetRequest returns the *http.Request that gets executed by the client
func (r TemplateCreateReq) GetRequest() (*http.Request, error) {
return opensearch.BuildRequest(
"PUT",
fmt.Sprintf("/_template/%s", r.Template),
r.Body,
r.Params.get(),
r.Header,
)
}

// TemplateCreateResp represents the returned struct of the index create response
type TemplateCreateResp struct {
Acknowledged bool `json:"acknowledged"`
response *opensearch.Response
}

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

// TemplateDeleteParams represents possible parameters for the TemplateDeleteReq
type TemplateDeleteParams struct {
MasterTimeout time.Duration
ClusterManagerTimeout time.Duration
Timeout time.Duration

Pretty bool
Human bool
ErrorTrace bool
}

func (r TemplateDeleteParams) 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
}
59 changes: 59 additions & 0 deletions opensearchapi/api_template-delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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 (
"fmt"
"net/http"

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

// TemplateDeleteReq represents possible options for the index create request
type TemplateDeleteReq struct {
Template string

Header http.Header
Params TemplateDeleteParams
}

// GetRequest returns the *http.Request that gets executed by the client
func (r TemplateDeleteReq) GetRequest() (*http.Request, error) {
return opensearch.BuildRequest(
"DELETE",
fmt.Sprintf("/_template/%s", r.Template),
nil,
r.Params.get(),
r.Header,
)
}

// TemplateDeleteResp represents the returned struct of the index create response
type TemplateDeleteResp struct {
Acknowledged bool `json:"acknowledged"`
response *opensearch.Response
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r TemplateDeleteResp) Inspect() Inspect {
return Inspect{Response: r.response}
}
73 changes: 73 additions & 0 deletions opensearchapi/api_template-exists-params.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 (
"strconv"
"time"
)

// TemplateExistsParams represents possible parameters for the TemplateExistsReq
type TemplateExistsParams struct {
FlatSettings *bool
Local *bool
MasterTimeout time.Duration
ClusterManagerTimeout time.Duration

Pretty bool
Human bool
ErrorTrace bool
}

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

if r.FlatSettings != nil {
params["flat_settings"] = strconv.FormatBool(*r.FlatSettings)
}

if r.Local != nil {
params["local"] = strconv.FormatBool(*r.Local)
}

if r.MasterTimeout != 0 {
params["master_timeout"] = formatDuration(r.MasterTimeout)
}

if r.ClusterManagerTimeout != 0 {
params["cluster_manager_timeout"] = formatDuration(r.ClusterManagerTimeout)
}

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

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

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

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

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

// TemplateExistsReq represents possible options for the index create request
type TemplateExistsReq struct {
Template string

Header http.Header
Params TemplateExistsParams
}

// GetRequest returns the *http.Request that gets executed by the client
func (r TemplateExistsReq) GetRequest() (*http.Request, error) {
return opensearch.BuildRequest(
"HEAD",
fmt.Sprintf("/_template/%s", r.Template),
nil,
r.Params.get(),
r.Header,
)
}
Loading

0 comments on commit e7e326a

Please sign in to comment.