health.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package storageos
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "github.com/storageos/go-api/types"
  8. )
  9. var (
  10. // HealthAPIPrefix is a partial path to the HTTP endpoint.
  11. HealthAPIPrefix = "health"
  12. )
  13. // CPHealth returns the health of the control plane server at a given url.
  14. func (c *Client) CPHealth(ctx context.Context, hostname string) (*types.CPHealthStatus, error) {
  15. url := fmt.Sprintf("http://%s:%s/v1/%s", hostname, DefaultPort, HealthAPIPrefix)
  16. req, err := http.NewRequest("GET", url, nil)
  17. if err != nil {
  18. return nil, err
  19. }
  20. req.Header.Set("User-Agent", c.userAgent)
  21. if c.username != "" && c.secret != "" {
  22. req.SetBasicAuth(c.username, c.secret)
  23. }
  24. c.configLock.RLock()
  25. resp, err := c.httpClient.Do(req.WithContext(ctx))
  26. c.configLock.RUnlock()
  27. if err != nil {
  28. return nil, err
  29. }
  30. defer resp.Body.Close()
  31. var status *types.CPHealthStatus
  32. if err := json.NewDecoder(resp.Body).Decode(&status); err != nil {
  33. return nil, err
  34. }
  35. return status, nil
  36. }
  37. // DPHealth returns the health of the data plane server at a given url.
  38. func (c *Client) DPHealth(ctx context.Context, hostname string) (*types.DPHealthStatus, error) {
  39. url := fmt.Sprintf("http://%s:%s/v1/%s", hostname, DataplaneHealthPort, HealthAPIPrefix)
  40. req, err := http.NewRequest("GET", url, nil)
  41. if err != nil {
  42. return nil, err
  43. }
  44. req.Header.Set("User-Agent", c.userAgent)
  45. if c.username != "" && c.secret != "" {
  46. req.SetBasicAuth(c.username, c.secret)
  47. }
  48. c.configLock.RLock()
  49. resp, err := c.httpClient.Do(req.WithContext(ctx))
  50. c.configLock.RUnlock()
  51. if err != nil {
  52. return nil, err
  53. }
  54. defer resp.Body.Close()
  55. var status *types.DPHealthStatus
  56. if err := json.NewDecoder(resp.Body).Decode(&status); err != nil {
  57. return nil, err
  58. }
  59. return status, nil
  60. }