cluster_state.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright 2012-2015 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. "encoding/json"
  7. "fmt"
  8. "net/url"
  9. "strings"
  10. "github.com/olivere/elastic/uritemplates"
  11. )
  12. // ClusterStateService returns the state of the cluster.
  13. // It is documented at http://www.elasticsearch.org/guide/en/elasticsearch/reference/1.4/cluster-state.html.
  14. type ClusterStateService struct {
  15. client *Client
  16. pretty bool
  17. indices []string
  18. metrics []string
  19. local *bool
  20. masterTimeout string
  21. flatSettings *bool
  22. }
  23. // NewClusterStateService creates a new ClusterStateService.
  24. func NewClusterStateService(client *Client) *ClusterStateService {
  25. return &ClusterStateService{
  26. client: client,
  27. indices: make([]string, 0),
  28. metrics: make([]string, 0),
  29. }
  30. }
  31. // Index the name of the index. Use _all or an empty string to perform
  32. // the operation on all indices.
  33. func (s *ClusterStateService) Index(index string) *ClusterStateService {
  34. s.indices = make([]string, 0)
  35. s.indices = append(s.indices, index)
  36. return s
  37. }
  38. // Indices is a list of index names. Use _all or an empty string to
  39. // perform the operation on all indices.
  40. func (s *ClusterStateService) Indices(indices ...string) *ClusterStateService {
  41. s.indices = make([]string, 0)
  42. s.indices = append(s.indices, indices...)
  43. return s
  44. }
  45. // Metric limits the information returned to the specified metric.
  46. // It can be one of: version, master_node, nodes, routing_table, metadata,
  47. // blocks, or customs.
  48. func (s *ClusterStateService) Metric(metric string) *ClusterStateService {
  49. s.metrics = make([]string, 0)
  50. s.metrics = append(s.metrics, metric)
  51. return s
  52. }
  53. // Metrics limits the information returned to the specified metrics.
  54. // It can be any of: version, master_node, nodes, routing_table, metadata,
  55. // blocks, or customs.
  56. func (s *ClusterStateService) Metrics(metrics ...string) *ClusterStateService {
  57. s.metrics = make([]string, 0)
  58. s.metrics = append(s.metrics, metrics...)
  59. return s
  60. }
  61. // Local indicates whether to return local information. If it is true,
  62. // we do not retrieve the state from master node (default: false).
  63. func (s *ClusterStateService) Local(local bool) *ClusterStateService {
  64. s.local = &local
  65. return s
  66. }
  67. // MasterTimeout specifies the timeout for connection to master.
  68. func (s *ClusterStateService) MasterTimeout(masterTimeout string) *ClusterStateService {
  69. s.masterTimeout = masterTimeout
  70. return s
  71. }
  72. // FlatSettings indicates whether to return settings in flat format (default: false).
  73. func (s *ClusterStateService) FlatSettings(flatSettings bool) *ClusterStateService {
  74. s.flatSettings = &flatSettings
  75. return s
  76. }
  77. // buildURL builds the URL for the operation.
  78. func (s *ClusterStateService) buildURL() (string, url.Values, error) {
  79. // Build URL
  80. metrics := strings.Join(s.metrics, ",")
  81. if metrics == "" {
  82. metrics = "_all"
  83. }
  84. indices := strings.Join(s.indices, ",")
  85. if indices == "" {
  86. indices = "_all"
  87. }
  88. path, err := uritemplates.Expand("/_cluster/state/{metrics}/{indices}", map[string]string{
  89. "metrics": metrics,
  90. "indices": indices,
  91. })
  92. if err != nil {
  93. return "", url.Values{}, err
  94. }
  95. // Add query string parameters
  96. params := url.Values{}
  97. if s.masterTimeout != "" {
  98. params.Set("master_timeout", s.masterTimeout)
  99. }
  100. if s.flatSettings != nil {
  101. params.Set("flat_settings", fmt.Sprintf("%v", *s.flatSettings))
  102. }
  103. if s.local != nil {
  104. params.Set("local", fmt.Sprintf("%v", *s.local))
  105. }
  106. return path, params, nil
  107. }
  108. // Validate checks if the operation is valid.
  109. func (s *ClusterStateService) Validate() error {
  110. return nil
  111. }
  112. // Do executes the operation.
  113. func (s *ClusterStateService) Do() (*ClusterStateResponse, error) {
  114. // Check pre-conditions
  115. if err := s.Validate(); err != nil {
  116. return nil, err
  117. }
  118. // Get URL for request
  119. path, params, err := s.buildURL()
  120. if err != nil {
  121. return nil, err
  122. }
  123. // Get HTTP response
  124. res, err := s.client.PerformRequest("GET", path, params, nil)
  125. if err != nil {
  126. return nil, err
  127. }
  128. // Return operation response
  129. ret := new(ClusterStateResponse)
  130. if err := json.Unmarshal(res.Body, ret); err != nil {
  131. return nil, err
  132. }
  133. return ret, nil
  134. }
  135. // ClusterStateResponse is the response of ClusterStateService.Do.
  136. type ClusterStateResponse struct {
  137. ClusterName string `json:"cluster_name"`
  138. Version int `json:"version"`
  139. MasterNode string `json:"master_node"`
  140. Blocks map[string]interface{} `json:"blocks"`
  141. Nodes map[string]*ClusterStateNode `json:"nodes"`
  142. Metadata *ClusterStateMetadata `json:"metadata"`
  143. RoutingTable map[string]*ClusterStateRoutingTable `json:"routing_table"`
  144. RoutingNodes *ClusterStateRoutingNode `json:"routing_nodes"`
  145. Allocations []interface{} `json:"allocations"`
  146. Customs map[string]interface{} `json:"customs"`
  147. }
  148. type ClusterStateMetadata struct {
  149. Templates map[string]interface{} `json:"templates"`
  150. Indices map[string]interface{} `json:"indices"`
  151. Repositories map[string]interface{} `json:"repositories"`
  152. }
  153. type ClusterStateNode struct {
  154. State string `json:"state"`
  155. Primary bool `json:"primary"`
  156. Node string `json:"node"`
  157. RelocatingNode *string `json:"relocating_node"`
  158. Shard int `json:"shard"`
  159. Index string `json:"index"`
  160. }
  161. type ClusterStateRoutingTable struct {
  162. Indices map[string]interface{} `json:"indices"`
  163. }
  164. type ClusterStateRoutingNode struct {
  165. Unassigned []interface{} `json:"unassigned"`
  166. Nodes map[string]interface{} `json:"nodes"`
  167. }