github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.indices.delete_datastream.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 package opensearchapi 11 12 import ( 13 "context" 14 "net/http" 15 "strings" 16 "time" 17 ) 18 19 func newIndicesDeleteDataStreamFunc(t Transport) IndicesDeleteDataStream { 20 return func(name string, o ...func(*IndicesDeleteDataStreamRequest)) (*Response, error) { 21 var r = IndicesDeleteDataStreamRequest{Name: name} 22 for _, f := range o { 23 f(&r) 24 } 25 return r.Do(r.ctx, t) 26 } 27 } 28 29 // ----- API Definition ------------------------------------------------------- 30 31 // IndicesDeleteDataStream deletes the backing indices of a data stream and then deletes the data stream itself. 32 type IndicesDeleteDataStream func(name string, o ...func(*IndicesDeleteDataStreamRequest)) (*Response, error) 33 34 // IndicesDeleteDataStreamRequest configures the Data Stream Delete Template API request. 35 type IndicesDeleteDataStreamRequest struct { 36 Name string 37 38 ClusterManagerTimeout time.Duration 39 Timeout time.Duration 40 41 Pretty bool 42 Human bool 43 ErrorTrace bool 44 FilterPath []string 45 46 Header http.Header 47 48 ctx context.Context 49 } 50 51 // Do execute the request and returns response or error. 52 func (r IndicesDeleteDataStreamRequest) Do(ctx context.Context, transport Transport) (*Response, error) { 53 var ( 54 method string 55 path strings.Builder 56 params map[string]string 57 ) 58 59 method = "DELETE" 60 61 path.Grow(1 + len("_data_stream") + 1 + len(r.Name)) 62 path.WriteString("/_data_stream/") 63 path.WriteString(r.Name) 64 65 params = make(map[string]string) 66 67 if r.ClusterManagerTimeout != 0 { 68 params["cluster_manager_timeout"] = formatDuration(r.ClusterManagerTimeout) 69 } 70 71 if r.Timeout != 0 { 72 params["timeout"] = formatDuration(r.Timeout) 73 } 74 75 if r.Pretty { 76 params["pretty"] = "true" 77 } 78 79 if r.Human { 80 params["human"] = "true" 81 } 82 83 if r.ErrorTrace { 84 params["error_trace"] = "true" 85 } 86 87 if len(r.FilterPath) > 0 { 88 params["filter_path"] = strings.Join(r.FilterPath, ",") 89 } 90 91 req, err := newRequest(method, path.String(), nil) 92 if err != nil { 93 return nil, err 94 } 95 96 if len(params) > 0 { 97 q := req.URL.Query() 98 for k, v := range params { 99 q.Set(k, v) 100 } 101 req.URL.RawQuery = q.Encode() 102 } 103 104 if len(r.Header) > 0 { 105 if len(req.Header) == 0 { 106 req.Header = r.Header 107 } else { 108 for k, vv := range r.Header { 109 for _, v := range vv { 110 req.Header.Add(k, v) 111 } 112 } 113 } 114 } 115 116 if ctx != nil { 117 req = req.WithContext(ctx) 118 } 119 120 res, err := transport.Perform(req) 121 if err != nil { 122 return nil, err 123 } 124 125 response := Response{ 126 StatusCode: res.StatusCode, 127 Body: res.Body, 128 Header: res.Header, 129 } 130 131 return &response, nil 132 } 133 134 // WithContext sets the request context. 135 func (f IndicesDeleteDataStream) WithContext(v context.Context) func(*IndicesDeleteDataStreamRequest) { 136 return func(r *IndicesDeleteDataStreamRequest) { 137 r.ctx = v 138 } 139 } 140 141 // WithClusterManagerTimeout - explicit operation timeout for connection to cluster-manager node. 142 func (f IndicesDeleteDataStream) WithClusterManagerTimeout(v time.Duration) func(*IndicesDeleteDataStreamRequest) { 143 return func(r *IndicesDeleteDataStreamRequest) { 144 r.ClusterManagerTimeout = v 145 } 146 } 147 148 // WithTimeout - explicit operation timeout. 149 func (f IndicesDeleteDataStream) WithTimeout(v time.Duration) func(*IndicesDeleteDataStreamRequest) { 150 return func(r *IndicesDeleteDataStreamRequest) { 151 r.Timeout = v 152 } 153 } 154 155 // WithPretty makes the response body pretty-printed. 156 func (f IndicesDeleteDataStream) WithPretty() func(*IndicesDeleteDataStreamRequest) { 157 return func(r *IndicesDeleteDataStreamRequest) { 158 r.Pretty = true 159 } 160 } 161 162 // WithHuman makes statistical values human-readable. 163 func (f IndicesDeleteDataStream) WithHuman() func(*IndicesDeleteDataStreamRequest) { 164 return func(r *IndicesDeleteDataStreamRequest) { 165 r.Human = true 166 } 167 } 168 169 // WithErrorTrace includes the stack trace for errors in the response body. 170 func (f IndicesDeleteDataStream) WithErrorTrace() func(*IndicesDeleteDataStreamRequest) { 171 return func(r *IndicesDeleteDataStreamRequest) { 172 r.ErrorTrace = true 173 } 174 } 175 176 // WithFilterPath filters the properties of the response body. 177 func (f IndicesDeleteDataStream) WithFilterPath(v ...string) func(*IndicesDeleteDataStreamRequest) { 178 return func(r *IndicesDeleteDataStreamRequest) { 179 r.FilterPath = v 180 } 181 } 182 183 // WithHeader adds the headers to the HTTP request. 184 func (f IndicesDeleteDataStream) WithHeader(h map[string]string) func(*IndicesDeleteDataStreamRequest) { 185 return func(r *IndicesDeleteDataStreamRequest) { 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 IndicesDeleteDataStream) WithOpaqueID(s string) func(*IndicesDeleteDataStreamRequest) { 197 return func(r *IndicesDeleteDataStreamRequest) { 198 if r.Header == nil { 199 r.Header = make(http.Header) 200 } 201 r.Header.Set("X-Opaque-Id", s) 202 } 203 }