Debian Codegraph @

indices_close.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 // IndicesCloseService closes an index.
16 //
17 // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-open-close.html
18 // for details.
19 type IndicesCloseService struct {
20 client *Client
21 pretty bool
22 index string
23 timeout string
24 masterTimeout string
25 ignoreUnavailable *bool
26 allowNoIndices *bool
27 expandWildcards string
28 }
29
30 // NewIndicesCloseService creates and initializes a new IndicesCloseService.
31 func NewIndicesCloseService(client *Client) *IndicesCloseService {
32 return &IndicesCloseService{client: client}
33 }
34
35 // Index is the name of the index to close.
36 func (s *IndicesCloseService) Index(index string) *IndicesCloseService {
37 s.index = index
38 return s
39 }
40
41 // Timeout is an explicit operation timeout.
42 func (s *IndicesCloseService) Timeout(timeout string) *IndicesCloseService {
43 s.timeout = timeout
44 return s
45 }
46
47 // MasterTimeout specifies the timeout for connection to master.
48 func (s *IndicesCloseService) MasterTimeout(masterTimeout string) *IndicesCloseService {
49 s.masterTimeout = masterTimeout
50 return s
51 }
52
53 // IgnoreUnavailable indicates whether specified concrete indices should be
54 // ignored when unavailable (missing or closed).
55 func (s *IndicesCloseService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesCloseService {
56 s.ignoreUnavailable = &ignoreUnavailable
57 return s
58 }
59
60 // AllowNoIndices indicates whether to ignore if a wildcard indices
61 // expression resolves into no concrete indices. (This includes `_all` string or when no indices have been specified).
62 func (s *IndicesCloseService) AllowNoIndices(allowNoIndices bool) *IndicesCloseService {
63 s.allowNoIndices = &allowNoIndices
64 return s
65 }
66
67 // ExpandWildcards indicates whether to expand wildcard expression to
68 // concrete indices that are open, closed or both.
69 func (s *IndicesCloseService) ExpandWildcards(expandWildcards string) *IndicesCloseService {
70 s.expandWildcards = expandWildcards
71 return s
72 }
73
74 // Pretty indicates that the JSON response be indented and human readable.
75 func (s *IndicesCloseService) Pretty(pretty bool) *IndicesCloseService {
76 s.pretty = pretty
77 return s
78 }
79
80 // buildURL builds the URL for the operation.
81 func (s *IndicesCloseService) buildURL() (string, url.Values, error) {
82 // Build URL
83 path, err := uritemplates.Expand("/{index}/_close", map[string]string{
84 "index": s.index,
85 })
86 if err != nil {
87 return "", url.Values{}, err
88 }
89
90 // Add query string parameters
91 params := url.Values{}
92 if s.allowNoIndices != nil {
93 params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices))
94 }
95 if s.expandWildcards != "" {
96 params.Set("expand_wildcards", s.expandWildcards)
97 }
98 if s.timeout != "" {
99 params.Set("timeout", s.timeout)
100 }
101 if s.masterTimeout != "" {
102 params.Set("master_timeout", s.masterTimeout)
103 }
104 if s.ignoreUnavailable != nil {
105 params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable))
106 }
107
108 return path, params, nil
109 }
110
111 // Validate checks if the operation is valid.
112 func (s *IndicesCloseService) Validate() error {
113 var invalid []string
114 if s.index == "" {
115 invalid = append(invalid, "Index")
116 }
117 if len(invalid) > 0 {
118 return fmt.Errorf("missing required fields: %v", invalid)
119 }
120 return nil
121 }
122
123 // Do executes the operation.
124 func (s *IndicesCloseService) Do(ctx context.Context) (*IndicesCloseResponse, error) {
125 // Check pre-conditions
126 if err := s.Validate(); err != nil {
127 return nil, err
128 }
129
130 // Get URL for request
131 path, params, err := s.buildURL()
132 if err != nil {
133 return nil, err
134 }
135
136 // Get HTTP response
137 res, err := s.client.PerformRequest(ctx, "POST", path, params, nil)
138 if err != nil {
139 return nil, err
140 }
141
142 // Return operation response
143 ret := new(IndicesCloseResponse)
144 if err := s.client.decoder.Decode(res.Body, ret); err != nil {
145 return nil, err
146 }
147 return ret, nil
148 }
149
150 // IndicesCloseResponse is the response of IndicesCloseService.Do.
151 type IndicesCloseResponse struct {
152 Acknowledged bool `json:"acknowledged"`
153 }