ping.go 3.0 KB

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