indices_put_template.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/url"
  9. "gopkg.in/olivere/elastic.v5/uritemplates"
  10. )
  11. // IndicesPutTemplateService creates or updates index mappings.
  12. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-templates.html.
  13. type IndicesPutTemplateService struct {
  14. client *Client
  15. pretty bool
  16. name string
  17. cause string
  18. order interface{}
  19. version *int
  20. create *bool
  21. timeout string
  22. masterTimeout string
  23. flatSettings *bool
  24. bodyJson interface{}
  25. bodyString string
  26. }
  27. // NewIndicesPutTemplateService creates a new IndicesPutTemplateService.
  28. func NewIndicesPutTemplateService(client *Client) *IndicesPutTemplateService {
  29. return &IndicesPutTemplateService{
  30. client: client,
  31. }
  32. }
  33. // Name is the name of the index template.
  34. func (s *IndicesPutTemplateService) Name(name string) *IndicesPutTemplateService {
  35. s.name = name
  36. return s
  37. }
  38. // Cause describes the cause for this index template creation. This is currently
  39. // undocumented, but part of the Java source.
  40. func (s *IndicesPutTemplateService) Cause(cause string) *IndicesPutTemplateService {
  41. s.cause = cause
  42. return s
  43. }
  44. // Timeout is an explicit operation timeout.
  45. func (s *IndicesPutTemplateService) Timeout(timeout string) *IndicesPutTemplateService {
  46. s.timeout = timeout
  47. return s
  48. }
  49. // MasterTimeout specifies the timeout for connection to master.
  50. func (s *IndicesPutTemplateService) MasterTimeout(masterTimeout string) *IndicesPutTemplateService {
  51. s.masterTimeout = masterTimeout
  52. return s
  53. }
  54. // FlatSettings indicates whether to return settings in flat format (default: false).
  55. func (s *IndicesPutTemplateService) FlatSettings(flatSettings bool) *IndicesPutTemplateService {
  56. s.flatSettings = &flatSettings
  57. return s
  58. }
  59. // Order is the order for this template when merging multiple matching ones
  60. // (higher numbers are merged later, overriding the lower numbers).
  61. func (s *IndicesPutTemplateService) Order(order interface{}) *IndicesPutTemplateService {
  62. s.order = order
  63. return s
  64. }
  65. // Version sets the version number for this template.
  66. func (s *IndicesPutTemplateService) Version(version int) *IndicesPutTemplateService {
  67. s.version = &version
  68. return s
  69. }
  70. // Create indicates whether the index template should only be added if
  71. // new or can also replace an existing one.
  72. func (s *IndicesPutTemplateService) Create(create bool) *IndicesPutTemplateService {
  73. s.create = &create
  74. return s
  75. }
  76. // Pretty indicates that the JSON response be indented and human readable.
  77. func (s *IndicesPutTemplateService) Pretty(pretty bool) *IndicesPutTemplateService {
  78. s.pretty = pretty
  79. return s
  80. }
  81. // BodyJson is documented as: The template definition.
  82. func (s *IndicesPutTemplateService) BodyJson(body interface{}) *IndicesPutTemplateService {
  83. s.bodyJson = body
  84. return s
  85. }
  86. // BodyString is documented as: The template definition.
  87. func (s *IndicesPutTemplateService) BodyString(body string) *IndicesPutTemplateService {
  88. s.bodyString = body
  89. return s
  90. }
  91. // buildURL builds the URL for the operation.
  92. func (s *IndicesPutTemplateService) buildURL() (string, url.Values, error) {
  93. // Build URL
  94. path, err := uritemplates.Expand("/_template/{name}", map[string]string{
  95. "name": s.name,
  96. })
  97. if err != nil {
  98. return "", url.Values{}, err
  99. }
  100. // Add query string parameters
  101. params := url.Values{}
  102. if s.pretty {
  103. params.Set("pretty", "1")
  104. }
  105. if s.order != nil {
  106. params.Set("order", fmt.Sprintf("%v", s.order))
  107. }
  108. if s.version != nil {
  109. params.Set("version", fmt.Sprintf("%v", *s.version))
  110. }
  111. if s.create != nil {
  112. params.Set("create", fmt.Sprintf("%v", *s.create))
  113. }
  114. if s.cause != "" {
  115. params.Set("cause", s.cause)
  116. }
  117. if s.timeout != "" {
  118. params.Set("timeout", s.timeout)
  119. }
  120. if s.masterTimeout != "" {
  121. params.Set("master_timeout", s.masterTimeout)
  122. }
  123. if s.flatSettings != nil {
  124. params.Set("flat_settings", fmt.Sprintf("%v", *s.flatSettings))
  125. }
  126. return path, params, nil
  127. }
  128. // Validate checks if the operation is valid.
  129. func (s *IndicesPutTemplateService) Validate() error {
  130. var invalid []string
  131. if s.name == "" {
  132. invalid = append(invalid, "Name")
  133. }
  134. if s.bodyString == "" && s.bodyJson == nil {
  135. invalid = append(invalid, "BodyJson")
  136. }
  137. if len(invalid) > 0 {
  138. return fmt.Errorf("missing required fields: %v", invalid)
  139. }
  140. return nil
  141. }
  142. // Do executes the operation.
  143. func (s *IndicesPutTemplateService) Do(ctx context.Context) (*IndicesPutTemplateResponse, error) {
  144. // Check pre-conditions
  145. if err := s.Validate(); err != nil {
  146. return nil, err
  147. }
  148. // Get URL for request
  149. path, params, err := s.buildURL()
  150. if err != nil {
  151. return nil, err
  152. }
  153. // Setup HTTP request body
  154. var body interface{}
  155. if s.bodyJson != nil {
  156. body = s.bodyJson
  157. } else {
  158. body = s.bodyString
  159. }
  160. // Get HTTP response
  161. res, err := s.client.PerformRequest(ctx, "PUT", path, params, body)
  162. if err != nil {
  163. return nil, err
  164. }
  165. // Return operation response
  166. ret := new(IndicesPutTemplateResponse)
  167. if err := s.client.decoder.Decode(res.Body, ret); err != nil {
  168. return nil, err
  169. }
  170. return ret, nil
  171. }
  172. // IndicesPutTemplateResponse is the response of IndicesPutTemplateService.Do.
  173. type IndicesPutTemplateResponse struct {
  174. Acknowledged bool `json:"acknowledged,omitempty"`
  175. }