github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.info.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  //
     3  // The OpenSearch Contributors require contributions made to
     4  // this file be licensed under the Apache-2.0 license or a
     5  // compatible open source license.
     6  //
     7  // Modifications Copyright OpenSearch Contributors. See
     8  // GitHub history for details.
     9  
    10  // Licensed to Elasticsearch B.V. under one or more contributor
    11  // license agreements. See the NOTICE file distributed with
    12  // this work for additional information regarding copyright
    13  // ownership. Elasticsearch B.V. licenses this file to you under
    14  // the Apache License, Version 2.0 (the "License"); you may
    15  // not use this file except in compliance with the License.
    16  // You may obtain a copy of the License at
    17  //
    18  //    http://www.apache.org/licenses/LICENSE-2.0
    19  //
    20  // Unless required by applicable law or agreed to in writing,
    21  // software distributed under the License is distributed on an
    22  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    23  // KIND, either express or implied.  See the License for the
    24  // specific language governing permissions and limitations
    25  // under the License.
    26  
    27  package opensearchapi
    28  
    29  import (
    30  	"context"
    31  	"net/http"
    32  	"strings"
    33  )
    34  
    35  func newInfoFunc(t Transport) Info {
    36  	return func(o ...func(*InfoRequest)) (*Response, error) {
    37  		var r = InfoRequest{}
    38  		for _, f := range o {
    39  			f(&r)
    40  		}
    41  		return r.Do(r.ctx, t)
    42  	}
    43  }
    44  
    45  // ----- API Definition -------------------------------------------------------
    46  
    47  // Info returns basic information about the cluster.
    48  type Info func(o ...func(*InfoRequest)) (*Response, error)
    49  
    50  // InfoRequest configures the Info API request.
    51  type InfoRequest struct {
    52  	Pretty     bool
    53  	Human      bool
    54  	ErrorTrace bool
    55  	FilterPath []string
    56  
    57  	Header http.Header
    58  
    59  	ctx context.Context
    60  }
    61  
    62  // InfoResp is a custom type to parse the Info Reponse
    63  type InfoResp struct {
    64  	Name        string `json:"name"`
    65  	ClusterName string `json:"cluster_name"`
    66  	ClusterUUID string `json:"cluster_uuid"`
    67  	Version     struct {
    68  		Distribution                     string `json:"distribution"`
    69  		Number                           string `json:"number"`
    70  		BuildType                        string `json:"build_type"`
    71  		BuildHash                        string `json:"build_hash"`
    72  		BuildDate                        string `json:"build_date"`
    73  		BuildSnapshot                    bool   `json:"build_snapshot"`
    74  		LuceneVersion                    string `json:"lucene_version"`
    75  		MinimumWireCompatibilityVersion  string `json:"minimum_wire_compatibility_version"`
    76  		MinimumIndexCompatibilityVersion string `json:"minimum_index_compatibility_version"`
    77  	} `json:"version"`
    78  	Tagline string `json:"tagline"`
    79  }
    80  
    81  // Do executes the request and returns response or error.
    82  func (r InfoRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
    83  	var (
    84  		method string
    85  		path   strings.Builder
    86  		params map[string]string
    87  	)
    88  
    89  	method = "GET"
    90  
    91  	path.Grow(len("/"))
    92  	path.WriteString("/")
    93  
    94  	params = make(map[string]string)
    95  
    96  	if r.Pretty {
    97  		params["pretty"] = "true"
    98  	}
    99  
   100  	if r.Human {
   101  		params["human"] = "true"
   102  	}
   103  
   104  	if r.ErrorTrace {
   105  		params["error_trace"] = "true"
   106  	}
   107  
   108  	if len(r.FilterPath) > 0 {
   109  		params["filter_path"] = strings.Join(r.FilterPath, ",")
   110  	}
   111  
   112  	req, err := newRequest(method, path.String(), nil)
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  
   117  	if len(params) > 0 {
   118  		q := req.URL.Query()
   119  		for k, v := range params {
   120  			q.Set(k, v)
   121  		}
   122  		req.URL.RawQuery = q.Encode()
   123  	}
   124  
   125  	if len(r.Header) > 0 {
   126  		if len(req.Header) == 0 {
   127  			req.Header = r.Header
   128  		} else {
   129  			for k, vv := range r.Header {
   130  				for _, v := range vv {
   131  					req.Header.Add(k, v)
   132  				}
   133  			}
   134  		}
   135  	}
   136  
   137  	if ctx != nil {
   138  		req = req.WithContext(ctx)
   139  	}
   140  
   141  	res, err := transport.Perform(req)
   142  	if err != nil {
   143  		return nil, err
   144  	}
   145  
   146  	response := Response{
   147  		StatusCode: res.StatusCode,
   148  		Body:       res.Body,
   149  		Header:     res.Header,
   150  	}
   151  
   152  	return &response, nil
   153  }
   154  
   155  // WithContext sets the request context.
   156  func (f Info) WithContext(v context.Context) func(*InfoRequest) {
   157  	return func(r *InfoRequest) {
   158  		r.ctx = v
   159  	}
   160  }
   161  
   162  // WithHuman makes statistical values human-readable.
   163  func (f Info) WithHuman() func(*InfoRequest) {
   164  	return func(r *InfoRequest) {
   165  		r.Human = true
   166  	}
   167  }
   168  
   169  // WithErrorTrace includes the stack trace for errors in the response body.
   170  func (f Info) WithErrorTrace() func(*InfoRequest) {
   171  	return func(r *InfoRequest) {
   172  		r.ErrorTrace = true
   173  	}
   174  }
   175  
   176  // WithFilterPath filters the properties of the response body.
   177  func (f Info) WithFilterPath(v ...string) func(*InfoRequest) {
   178  	return func(r *InfoRequest) {
   179  		r.FilterPath = v
   180  	}
   181  }
   182  
   183  // WithHeader adds the headers to the HTTP request.
   184  func (f Info) WithHeader(h map[string]string) func(*InfoRequest) {
   185  	return func(r *InfoRequest) {
   186  		if r.Header == nil {
   187  			r.Header = make(http.Header)
   188  		}
   189  		for k, v := range h {
   190  			r.Header.Add(k, v)
   191  		}
   192  	}
   193  }
   194  
   195  // WithOpaqueID adds the X-Opaque-Id header to the HTTP request.
   196  func (f Info) WithOpaqueID(s string) func(*InfoRequest) {
   197  	return func(r *InfoRequest) {
   198  		if r.Header == nil {
   199  			r.Header = make(http.Header)
   200  		}
   201  		r.Header.Set("X-Opaque-Id", s)
   202  	}
   203  }