Debian Codegraph @

ping.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 "encoding/json"
10 "net/http"
11 "net/url"
12 )
13
14 // PingService checks if an Elasticsearch server on a given URL is alive.
15 // When asked for, it can also return various information about the
16 // Elasticsearch server, e.g. the Elasticsearch version number.
17 //
18 // Ping simply starts a HTTP GET request to the URL of the server.
19 // If the server responds with HTTP Status code 200 OK, the server is alive.
20 type PingService struct {
21 client *Client
22 url string
23 timeout string
24 httpHeadOnly bool
25 pretty bool
26 }
27
28 // PingResult is the result returned from querying the Elasticsearch server.
29 type PingResult struct {
30 Name string `json:"name"`
31 ClusterName string `json:"cluster_name"`
32 Version struct {
33 Number string `json:"number"`
34 BuildHash string `json:"build_hash"`
35 BuildTimestamp string `json:"build_timestamp"`
36 BuildSnapshot bool `json:"build_snapshot"`
37 LuceneVersion string `json:"lucene_version"`
38 } `json:"version"`
39 TagLine string `json:"tagline"`
40 }
41
42 func NewPingService(client *Client) *PingService {
43 return &PingService{
44 client: client,
45 url: DefaultURL,
46 httpHeadOnly: false,
47 pretty: false,
48 }
49 }
50
51 func (s *PingService) URL(url string) *PingService {
52 s.url = url
53 return s
54 }
55
56 func (s *PingService) Timeout(timeout string) *PingService {
57 s.timeout = timeout
58 return s
59 }
60
61 // HeadOnly makes the service to only return the status code in Do;
62 // the PingResult will be nil.
63 func (s *PingService) HttpHeadOnly(httpHeadOnly bool) *PingService {
64 s.httpHeadOnly = httpHeadOnly
65 return s
66 }
67
68 func (s *PingService) Pretty(pretty bool) *PingService {
69 s.pretty = pretty
70 return s
71 }
72
73 // Do returns the PingResult, the HTTP status code of the Elasticsearch
74 // server, and an error.
75 func (s *PingService) Do(ctx context.Context) (*PingResult, int, error) {
76 s.client.mu.RLock()
77 basicAuth := s.client.basicAuth
78 basicAuthUsername := s.client.basicAuthUsername
79 basicAuthPassword := s.client.basicAuthPassword
80 s.client.mu.RUnlock()
81
82 url_ := s.url + "/"
83
84 params := make(url.Values)
85 if s.timeout != "" {
86 params.Set("timeout", s.timeout)
87 }
88 if s.pretty {
89 params.Set("pretty", "1")
90 }
91 if len(params) > 0 {
92 url_ += "?" + params.Encode()
93 }
94
95 var method string
96 if s.httpHeadOnly {
97 method = "HEAD"
98 } else {
99 method = "GET"
100 }
101
102 // Notice: This service must NOT use PerformRequest!
103 req, err := NewRequest(method, url_)
104 if err != nil {
105 return nil, 0, err
106 }
107
108 if basicAuth {
109 req.SetBasicAuth(basicAuthUsername, basicAuthPassword)
110 }
111
112 res, err := s.client.c.Do((*http.Request)(req).WithContext(ctx))
113 if err != nil {
114 return nil, 0, err
115 }
116 defer res.Body.Close()
117
118 var ret *PingResult
119 if !s.httpHeadOnly {
120 ret = new(PingResult)
121 if err := json.NewDecoder(res.Body).Decode(ret); err != nil {
122 return nil, res.StatusCode, err
123 }
124 }
125
126 return ret, res.StatusCode, nil
127 }