You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
4.6 KiB

  1. // Copyright 2012-present Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. import (
  6. "context"
  7. "fmt"
  8. "net/http"
  9. "net/url"
  10. "strings"
  11. "github.com/olivere/elastic/v7/uritemplates"
  12. )
  13. // DeleteScriptService removes a stored script in Elasticsearch.
  14. //
  15. // See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/modules-scripting.html
  16. // for details.
  17. type DeleteScriptService struct {
  18. client *Client
  19. pretty *bool // pretty format the returned JSON response
  20. human *bool // return human readable values for statistics
  21. errorTrace *bool // include the stack trace of returned errors
  22. filterPath []string // list of filters used to reduce the response
  23. headers http.Header // custom request-level HTTP headers
  24. id string
  25. timeout string
  26. masterTimeout string
  27. }
  28. // NewDeleteScriptService creates a new DeleteScriptService.
  29. func NewDeleteScriptService(client *Client) *DeleteScriptService {
  30. return &DeleteScriptService{
  31. client: client,
  32. }
  33. }
  34. // Pretty tells Elasticsearch whether to return a formatted JSON response.
  35. func (s *DeleteScriptService) Pretty(pretty bool) *DeleteScriptService {
  36. s.pretty = &pretty
  37. return s
  38. }
  39. // Human specifies whether human readable values should be returned in
  40. // the JSON response, e.g. "7.5mb".
  41. func (s *DeleteScriptService) Human(human bool) *DeleteScriptService {
  42. s.human = &human
  43. return s
  44. }
  45. // ErrorTrace specifies whether to include the stack trace of returned errors.
  46. func (s *DeleteScriptService) ErrorTrace(errorTrace bool) *DeleteScriptService {
  47. s.errorTrace = &errorTrace
  48. return s
  49. }
  50. // FilterPath specifies a list of filters used to reduce the response.
  51. func (s *DeleteScriptService) FilterPath(filterPath ...string) *DeleteScriptService {
  52. s.filterPath = filterPath
  53. return s
  54. }
  55. // Header adds a header to the request.
  56. func (s *DeleteScriptService) Header(name string, value string) *DeleteScriptService {
  57. if s.headers == nil {
  58. s.headers = http.Header{}
  59. }
  60. s.headers.Add(name, value)
  61. return s
  62. }
  63. // Headers specifies the headers of the request.
  64. func (s *DeleteScriptService) Headers(headers http.Header) *DeleteScriptService {
  65. s.headers = headers
  66. return s
  67. }
  68. // Id is the script ID.
  69. func (s *DeleteScriptService) Id(id string) *DeleteScriptService {
  70. s.id = id
  71. return s
  72. }
  73. // Timeout is an explicit operation timeout.
  74. func (s *DeleteScriptService) Timeout(timeout string) *DeleteScriptService {
  75. s.timeout = timeout
  76. return s
  77. }
  78. // MasterTimeout is the timeout for connecting to master.
  79. func (s *DeleteScriptService) MasterTimeout(masterTimeout string) *DeleteScriptService {
  80. s.masterTimeout = masterTimeout
  81. return s
  82. }
  83. // buildURL builds the URL for the operation.
  84. func (s *DeleteScriptService) buildURL() (string, string, url.Values, error) {
  85. var (
  86. err error
  87. method = "DELETE"
  88. path string
  89. )
  90. path, err = uritemplates.Expand("/_scripts/{id}", map[string]string{
  91. "id": s.id,
  92. })
  93. if err != nil {
  94. return "", "", url.Values{}, err
  95. }
  96. // Add query string parameters
  97. params := url.Values{}
  98. if v := s.pretty; v != nil {
  99. params.Set("pretty", fmt.Sprint(*v))
  100. }
  101. if v := s.human; v != nil {
  102. params.Set("human", fmt.Sprint(*v))
  103. }
  104. if v := s.errorTrace; v != nil {
  105. params.Set("error_trace", fmt.Sprint(*v))
  106. }
  107. if len(s.filterPath) > 0 {
  108. params.Set("filter_path", strings.Join(s.filterPath, ","))
  109. }
  110. if s.timeout != "" {
  111. params.Set("timeout", s.timeout)
  112. }
  113. if s.masterTimeout != "" {
  114. params.Set("master_timestamp", s.masterTimeout)
  115. }
  116. return method, path, params, nil
  117. }
  118. // Validate checks if the operation is valid.
  119. func (s *DeleteScriptService) Validate() error {
  120. var invalid []string
  121. if s.id == "" {
  122. invalid = append(invalid, "Id")
  123. }
  124. if len(invalid) > 0 {
  125. return fmt.Errorf("missing required fields: %v", invalid)
  126. }
  127. return nil
  128. }
  129. // Do executes the operation.
  130. func (s *DeleteScriptService) Do(ctx context.Context) (*DeleteScriptResponse, error) {
  131. // Check pre-conditions
  132. if err := s.Validate(); err != nil {
  133. return nil, err
  134. }
  135. // Get URL for request
  136. method, path, params, err := s.buildURL()
  137. if err != nil {
  138. return nil, err
  139. }
  140. // Get HTTP response
  141. res, err := s.client.PerformRequest(ctx, PerformRequestOptions{
  142. Method: method,
  143. Path: path,
  144. Params: params,
  145. Headers: s.headers,
  146. })
  147. if err != nil {
  148. return nil, err
  149. }
  150. // Return operation response
  151. ret := new(DeleteScriptResponse)
  152. if err := s.client.decoder.Decode(res.Body, ret); err != nil {
  153. return nil, err
  154. }
  155. return ret, nil
  156. }
  157. // DeleteScriptResponse is the result of deleting a stored script
  158. // in Elasticsearch.
  159. type DeleteScriptResponse struct {
  160. AcknowledgedResponse
  161. }