Debian Codegraph @

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