/vendor/github.com/circonus-labs/circonus-gometrics/api/metric_cluster.go

https://github.com/hashicorp/vault · Go · 261 lines · 193 code · 51 blank · 17 comment · 69 complexity · 5674272374a26ebf3c6ed00ded989b44 MD5 · raw file

  1. // Copyright 2016 Circonus, Inc. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Metric Cluster API support - Fetch, Create, Update, Delete, and Search
  5. // See: https://login.circonus.com/resources/api/calls/metric_cluster
  6. package api
  7. import (
  8. "encoding/json"
  9. "fmt"
  10. "net/url"
  11. "regexp"
  12. "github.com/circonus-labs/circonus-gometrics/api/config"
  13. )
  14. // MetricQuery object
  15. type MetricQuery struct {
  16. Query string `json:"query"`
  17. Type string `json:"type"`
  18. }
  19. // MetricCluster defines a metric cluster. See https://login.circonus.com/resources/api/calls/metric_cluster for more information.
  20. type MetricCluster struct {
  21. CID string `json:"_cid,omitempty"` // string
  22. Description string `json:"description"` // string
  23. MatchingMetrics []string `json:"_matching_metrics,omitempty"` // [] len >= 1 (result info only, if query has extras - cannot be set)
  24. MatchingUUIDMetrics map[string][]string `json:"_matching_uuid_metrics,omitempty"` // [] len >= 1 (result info only, if query has extras - cannot be set)
  25. Name string `json:"name"` // string
  26. Queries []MetricQuery `json:"queries"` // [] len >= 1
  27. Tags []string `json:"tags"` // [] len >= 0
  28. }
  29. // NewMetricCluster returns a new MetricCluster (with defaults, if applicable)
  30. func NewMetricCluster() *MetricCluster {
  31. return &MetricCluster{}
  32. }
  33. // FetchMetricCluster retrieves metric cluster with passed cid.
  34. func (a *API) FetchMetricCluster(cid CIDType, extras string) (*MetricCluster, error) {
  35. if cid == nil || *cid == "" {
  36. return nil, fmt.Errorf("Invalid metric cluster CID [none]")
  37. }
  38. clusterCID := string(*cid)
  39. matched, err := regexp.MatchString(config.MetricClusterCIDRegex, clusterCID)
  40. if err != nil {
  41. return nil, err
  42. }
  43. if !matched {
  44. return nil, fmt.Errorf("Invalid metric cluster CID [%s]", clusterCID)
  45. }
  46. reqURL := url.URL{
  47. Path: clusterCID,
  48. }
  49. extra := ""
  50. switch extras {
  51. case "metrics":
  52. extra = "_matching_metrics"
  53. case "uuids":
  54. extra = "_matching_uuid_metrics"
  55. }
  56. if extra != "" {
  57. q := url.Values{}
  58. q.Set("extra", extra)
  59. reqURL.RawQuery = q.Encode()
  60. }
  61. result, err := a.Get(reqURL.String())
  62. if err != nil {
  63. return nil, err
  64. }
  65. if a.Debug {
  66. a.Log.Printf("[DEBUG] fetch metric cluster, received JSON: %s", string(result))
  67. }
  68. cluster := &MetricCluster{}
  69. if err := json.Unmarshal(result, cluster); err != nil {
  70. return nil, err
  71. }
  72. return cluster, nil
  73. }
  74. // FetchMetricClusters retrieves all metric clusters available to API Token.
  75. func (a *API) FetchMetricClusters(extras string) (*[]MetricCluster, error) {
  76. reqURL := url.URL{
  77. Path: config.MetricClusterPrefix,
  78. }
  79. extra := ""
  80. switch extras {
  81. case "metrics":
  82. extra = "_matching_metrics"
  83. case "uuids":
  84. extra = "_matching_uuid_metrics"
  85. }
  86. if extra != "" {
  87. q := url.Values{}
  88. q.Set("extra", extra)
  89. reqURL.RawQuery = q.Encode()
  90. }
  91. result, err := a.Get(reqURL.String())
  92. if err != nil {
  93. return nil, err
  94. }
  95. var clusters []MetricCluster
  96. if err := json.Unmarshal(result, &clusters); err != nil {
  97. return nil, err
  98. }
  99. return &clusters, nil
  100. }
  101. // UpdateMetricCluster updates passed metric cluster.
  102. func (a *API) UpdateMetricCluster(cfg *MetricCluster) (*MetricCluster, error) {
  103. if cfg == nil {
  104. return nil, fmt.Errorf("Invalid metric cluster config [nil]")
  105. }
  106. clusterCID := string(cfg.CID)
  107. matched, err := regexp.MatchString(config.MetricClusterCIDRegex, clusterCID)
  108. if err != nil {
  109. return nil, err
  110. }
  111. if !matched {
  112. return nil, fmt.Errorf("Invalid metric cluster CID [%s]", clusterCID)
  113. }
  114. jsonCfg, err := json.Marshal(cfg)
  115. if err != nil {
  116. return nil, err
  117. }
  118. if a.Debug {
  119. a.Log.Printf("[DEBUG] update metric cluster, sending JSON: %s", string(jsonCfg))
  120. }
  121. result, err := a.Put(clusterCID, jsonCfg)
  122. if err != nil {
  123. return nil, err
  124. }
  125. cluster := &MetricCluster{}
  126. if err := json.Unmarshal(result, cluster); err != nil {
  127. return nil, err
  128. }
  129. return cluster, nil
  130. }
  131. // CreateMetricCluster creates a new metric cluster.
  132. func (a *API) CreateMetricCluster(cfg *MetricCluster) (*MetricCluster, error) {
  133. if cfg == nil {
  134. return nil, fmt.Errorf("Invalid metric cluster config [nil]")
  135. }
  136. jsonCfg, err := json.Marshal(cfg)
  137. if err != nil {
  138. return nil, err
  139. }
  140. if a.Debug {
  141. a.Log.Printf("[DEBUG] create metric cluster, sending JSON: %s", string(jsonCfg))
  142. }
  143. result, err := a.Post(config.MetricClusterPrefix, jsonCfg)
  144. if err != nil {
  145. return nil, err
  146. }
  147. cluster := &MetricCluster{}
  148. if err := json.Unmarshal(result, cluster); err != nil {
  149. return nil, err
  150. }
  151. return cluster, nil
  152. }
  153. // DeleteMetricCluster deletes passed metric cluster.
  154. func (a *API) DeleteMetricCluster(cfg *MetricCluster) (bool, error) {
  155. if cfg == nil {
  156. return false, fmt.Errorf("Invalid metric cluster config [nil]")
  157. }
  158. return a.DeleteMetricClusterByCID(CIDType(&cfg.CID))
  159. }
  160. // DeleteMetricClusterByCID deletes metric cluster with passed cid.
  161. func (a *API) DeleteMetricClusterByCID(cid CIDType) (bool, error) {
  162. if cid == nil || *cid == "" {
  163. return false, fmt.Errorf("Invalid metric cluster CID [none]")
  164. }
  165. clusterCID := string(*cid)
  166. matched, err := regexp.MatchString(config.MetricClusterCIDRegex, clusterCID)
  167. if err != nil {
  168. return false, err
  169. }
  170. if !matched {
  171. return false, fmt.Errorf("Invalid metric cluster CID [%s]", clusterCID)
  172. }
  173. _, err = a.Delete(clusterCID)
  174. if err != nil {
  175. return false, err
  176. }
  177. return true, nil
  178. }
  179. // SearchMetricClusters returns metric clusters matching the specified
  180. // search query and/or filter. If nil is passed for both parameters
  181. // all metric clusters will be returned.
  182. func (a *API) SearchMetricClusters(searchCriteria *SearchQueryType, filterCriteria *SearchFilterType) (*[]MetricCluster, error) {
  183. q := url.Values{}
  184. if searchCriteria != nil && *searchCriteria != "" {
  185. q.Set("search", string(*searchCriteria))
  186. }
  187. if filterCriteria != nil && len(*filterCriteria) > 0 {
  188. for filter, criteria := range *filterCriteria {
  189. for _, val := range criteria {
  190. q.Add(filter, val)
  191. }
  192. }
  193. }
  194. if q.Encode() == "" {
  195. return a.FetchMetricClusters("")
  196. }
  197. reqURL := url.URL{
  198. Path: config.MetricClusterPrefix,
  199. RawQuery: q.Encode(),
  200. }
  201. result, err := a.Get(reqURL.String())
  202. if err != nil {
  203. return nil, fmt.Errorf("[ERROR] API call error %+v", err)
  204. }
  205. var clusters []MetricCluster
  206. if err := json.Unmarshal(result, &clusters); err != nil {
  207. return nil, err
  208. }
  209. return &clusters, nil
  210. }