1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
// Copyright 2016 Circonus, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Broker API support - Fetch and Search
// See: https://login.circonus.com/resources/api/calls/broker
package api
import (
"encoding/json"
"fmt"
"net/url"
"regexp"
"github.com/circonus-labs/circonus-gometrics/api/config"
)
// BrokerDetail defines instance attributes
type BrokerDetail struct {
ClusterIP *string `json:"cluster_ip"` // string or null
CN string `json:"cn"` // string
ExternalHost *string `json:"external_host"` // string or null
ExternalPort uint16 `json:"external_port"` // uint16
IP *string `json:"ipaddress"` // string or null
MinVer uint `json:"minimum_version_required"` // uint
Modules []string `json:"modules"` // [] len >= 0
Port *uint16 `json:"port"` // uint16 or null
Skew *string `json:"skew"` // BUG doc: floating point number, api object: string or null
Status string `json:"status"` // string
Version *uint `json:"version"` // uint or null
}
// Broker defines a broker. See https://login.circonus.com/resources/api/calls/broker for more information.
type Broker struct {
CID string `json:"_cid"` // string
Details []BrokerDetail `json:"_details"` // [] len >= 1
Latitude *string `json:"_latitude"` // string or null
Longitude *string `json:"_longitude"` // string or null
Name string `json:"_name"` // string
Tags []string `json:"_tags"` // [] len >= 0
Type string `json:"_type"` // string
}
// FetchBroker retrieves broker with passed cid.
func (a *API) FetchBroker(cid CIDType) (*Broker, error) {
if cid == nil || *cid == "" {
return nil, fmt.Errorf("Invalid broker CID [none]")
}
brokerCID := string(*cid)
matched, err := regexp.MatchString(config.BrokerCIDRegex, brokerCID)
if err != nil {
return nil, err
}
if !matched {
return nil, fmt.Errorf("Invalid broker CID [%s]", brokerCID)
}
result, err := a.Get(brokerCID)
if err != nil {
return nil, err
}
if a.Debug {
a.Log.Printf("[DEBUG] fetch broker, received JSON: %s", string(result))
}
response := new(Broker)
if err := json.Unmarshal(result, &response); err != nil {
return nil, err
}
return response, nil
}
// FetchBrokers returns all brokers available to the API Token.
func (a *API) FetchBrokers() (*[]Broker, error) {
result, err := a.Get(config.BrokerPrefix)
if err != nil {
return nil, err
}
var response []Broker
if err := json.Unmarshal(result, &response); err != nil {
return nil, err
}
return &response, nil
}
// SearchBrokers returns brokers matching the specified search
// query and/or filter. If nil is passed for both parameters
// all brokers will be returned.
func (a *API) SearchBrokers(searchCriteria *SearchQueryType, filterCriteria *SearchFilterType) (*[]Broker, error) {
q := url.Values{}
if searchCriteria != nil && *searchCriteria != "" {
q.Set("search", string(*searchCriteria))
}
if filterCriteria != nil && len(*filterCriteria) > 0 {
for filter, criteria := range *filterCriteria {
for _, val := range criteria {
q.Add(filter, val)
}
}
}
if q.Encode() == "" {
return a.FetchBrokers()
}
reqURL := url.URL{
Path: config.BrokerPrefix,
RawQuery: q.Encode(),
}
result, err := a.Get(reqURL.String())
if err != nil {
return nil, fmt.Errorf("[ERROR] API call error %+v", err)
}
var brokers []Broker
if err := json.Unmarshal(result, &brokers); err != nil {
return nil, err
}
return &brokers, nil
}
|