health.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package api
  2. import (
  3. "fmt"
  4. )
  5. // HealthCheck is used to represent a single check
  6. type HealthCheck struct {
  7. Node string
  8. CheckID string
  9. Name string
  10. Status string
  11. Notes string
  12. Output string
  13. ServiceID string
  14. ServiceName string
  15. }
  16. // ServiceEntry is used for the health service endpoint
  17. type ServiceEntry struct {
  18. Node *Node
  19. Service *AgentService
  20. Checks []*HealthCheck
  21. }
  22. // Health can be used to query the Health endpoints
  23. type Health struct {
  24. c *Client
  25. }
  26. // Health returns a handle to the health endpoints
  27. func (c *Client) Health() *Health {
  28. return &Health{c}
  29. }
  30. // Node is used to query for checks belonging to a given node
  31. func (h *Health) Node(node string, q *QueryOptions) ([]*HealthCheck, *QueryMeta, error) {
  32. r := h.c.newRequest("GET", "/v1/health/node/"+node)
  33. r.setQueryOptions(q)
  34. rtt, resp, err := requireOK(h.c.doRequest(r))
  35. if err != nil {
  36. return nil, nil, err
  37. }
  38. defer resp.Body.Close()
  39. qm := &QueryMeta{}
  40. parseQueryMeta(resp, qm)
  41. qm.RequestTime = rtt
  42. var out []*HealthCheck
  43. if err := decodeBody(resp, &out); err != nil {
  44. return nil, nil, err
  45. }
  46. return out, qm, nil
  47. }
  48. // Checks is used to return the checks associated with a service
  49. func (h *Health) Checks(service string, q *QueryOptions) ([]*HealthCheck, *QueryMeta, error) {
  50. r := h.c.newRequest("GET", "/v1/health/checks/"+service)
  51. r.setQueryOptions(q)
  52. rtt, resp, err := requireOK(h.c.doRequest(r))
  53. if err != nil {
  54. return nil, nil, err
  55. }
  56. defer resp.Body.Close()
  57. qm := &QueryMeta{}
  58. parseQueryMeta(resp, qm)
  59. qm.RequestTime = rtt
  60. var out []*HealthCheck
  61. if err := decodeBody(resp, &out); err != nil {
  62. return nil, nil, err
  63. }
  64. return out, qm, nil
  65. }
  66. // Service is used to query health information along with service info
  67. // for a given service. It can optionally do server-side filtering on a tag
  68. // or nodes with passing health checks only.
  69. func (h *Health) Service(service, tag string, passingOnly bool, q *QueryOptions) ([]*ServiceEntry, *QueryMeta, error) {
  70. r := h.c.newRequest("GET", "/v1/health/service/"+service)
  71. r.setQueryOptions(q)
  72. if tag != "" {
  73. r.params.Set("tag", tag)
  74. }
  75. if passingOnly {
  76. r.params.Set("passing", "1")
  77. }
  78. rtt, resp, err := requireOK(h.c.doRequest(r))
  79. if err != nil {
  80. return nil, nil, err
  81. }
  82. defer resp.Body.Close()
  83. qm := &QueryMeta{}
  84. parseQueryMeta(resp, qm)
  85. qm.RequestTime = rtt
  86. var out []*ServiceEntry
  87. if err := decodeBody(resp, &out); err != nil {
  88. return nil, nil, err
  89. }
  90. return out, qm, nil
  91. }
  92. // State is used to retrieve all the checks in a given state.
  93. // The wildcard "any" state can also be used for all checks.
  94. func (h *Health) State(state string, q *QueryOptions) ([]*HealthCheck, *QueryMeta, error) {
  95. switch state {
  96. case "any":
  97. case "warning":
  98. case "critical":
  99. case "passing":
  100. case "unknown":
  101. default:
  102. return nil, nil, fmt.Errorf("Unsupported state: %v", state)
  103. }
  104. r := h.c.newRequest("GET", "/v1/health/state/"+state)
  105. r.setQueryOptions(q)
  106. rtt, resp, err := requireOK(h.c.doRequest(r))
  107. if err != nil {
  108. return nil, nil, err
  109. }
  110. defer resp.Body.Close()
  111. qm := &QueryMeta{}
  112. parseQueryMeta(resp, qm)
  113. qm.RequestTime = rtt
  114. var out []*HealthCheck
  115. if err := decodeBody(resp, &out); err != nil {
  116. return nil, nil, err
  117. }
  118. return out, qm, nil
  119. }