Debian Codegraph @

put_template.go

raw
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
5 package elastic
6
7 import (
8 "context"
9 "fmt"
10 "net/url"
11
12 "gopkg.in/olivere/elastic.v5/uritemplates"
13 )
14
15 // PutTemplateService creates or updates a search template.
16 // The documentation can be found at
17 // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-template.html.
18 type PutTemplateService struct {
19 client *Client
20 pretty bool
21 id string
22 opType string
23 version *int
24 versionType string
25 bodyJson interface{}
26 bodyString string
27 }
28
29 // NewPutTemplateService creates a new PutTemplateService.
30 func NewPutTemplateService(client *Client) *PutTemplateService {
31 return &PutTemplateService{
32 client: client,
33 }
34 }
35
36 // Id is the template ID.
37 func (s *PutTemplateService) Id(id string) *PutTemplateService {
38 s.id = id
39 return s
40 }
41
42 // OpType is an explicit operation type.
43 func (s *PutTemplateService) OpType(opType string) *PutTemplateService {
44 s.opType = opType
45 return s
46 }
47
48 // Version is an explicit version number for concurrency control.
49 func (s *PutTemplateService) Version(version int) *PutTemplateService {
50 s.version = &version
51 return s
52 }
53
54 // VersionType is a specific version type.
55 func (s *PutTemplateService) VersionType(versionType string) *PutTemplateService {
56 s.versionType = versionType
57 return s
58 }
59
60 // BodyJson is the document as a JSON serializable object.
61 func (s *PutTemplateService) BodyJson(body interface{}) *PutTemplateService {
62 s.bodyJson = body
63 return s
64 }
65
66 // BodyString is the document as a string.
67 func (s *PutTemplateService) BodyString(body string) *PutTemplateService {
68 s.bodyString = body
69 return s
70 }
71
72 // buildURL builds the URL for the operation.
73 func (s *PutTemplateService) buildURL() (string, url.Values, error) {
74 // Build URL
75 path, err := uritemplates.Expand("/_search/template/{id}", map[string]string{
76 "id": s.id,
77 })
78 if err != nil {
79 return "", url.Values{}, err
80 }
81
82 // Add query string parameters
83 params := url.Values{}
84 if s.version != nil {
85 params.Set("version", fmt.Sprintf("%d", *s.version))
86 }
87 if s.versionType != "" {
88 params.Set("version_type", s.versionType)
89 }
90 if s.opType != "" {
91 params.Set("op_type", s.opType)
92 }
93
94 return path, params, nil
95 }
96
97 // Validate checks if the operation is valid.
98 func (s *PutTemplateService) Validate() error {
99 var invalid []string
100 if s.id == "" {
101 invalid = append(invalid, "Id")
102 }
103 if s.bodyString == "" && s.bodyJson == nil {
104 invalid = append(invalid, "BodyJson")
105 }
106 if len(invalid) > 0 {
107 return fmt.Errorf("missing required fields: %v", invalid)
108 }
109 return nil
110 }
111
112 // Do executes the operation.
113 func (s *PutTemplateService) Do(ctx context.Context) (*AcknowledgedResponse, error) {
114 // Check pre-conditions
115 if err := s.Validate(); err != nil {
116 return nil, err
117 }
118
119 // Get URL for request
120 path, params, err := s.buildURL()
121 if err != nil {
122 return nil, err
123 }
124
125 // Setup HTTP request body
126 var body interface{}
127 if s.bodyJson != nil {
128 body = s.bodyJson
129 } else {
130 body = s.bodyString
131 }
132
133 // Get HTTP response
134 res, err := s.client.PerformRequest(ctx, "PUT", path, params, body)
135 if err != nil {
136 return nil, err
137 }
138
139 // Return operation response
140 ret := new(AcknowledgedResponse)
141 if err := s.client.decoder.Decode(res.Body, ret); err != nil {
142 return nil, err
143 }
144 return ret, nil
145 }