2016-11-09 20:30:07 +00:00
|
|
|
// 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.
|
|
|
|
|
2019-06-19 12:50:48 +00:00
|
|
|
// Broker API support - Fetch and Search
|
|
|
|
// See: https://login.circonus.com/resources/api/calls/broker
|
|
|
|
|
2016-07-19 23:40:41 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-06-19 12:50:48 +00:00
|
|
|
"net/url"
|
|
|
|
"regexp"
|
|
|
|
|
|
|
|
"github.com/circonus-labs/circonus-gometrics/api/config"
|
2016-07-19 23:40:41 +00:00
|
|
|
)
|
|
|
|
|
2019-06-19 12:50:48 +00:00
|
|
|
// BrokerDetail defines instance attributes
|
2016-07-19 23:40:41 +00:00
|
|
|
type BrokerDetail struct {
|
2019-06-19 12:50:48 +00:00
|
|
|
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
|
2016-07-19 23:40:41 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 12:50:48 +00:00
|
|
|
// Broker defines a broker. See https://login.circonus.com/resources/api/calls/broker for more information.
|
2016-07-19 23:40:41 +00:00
|
|
|
type Broker struct {
|
2019-06-19 12:50:48 +00:00
|
|
|
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
|
2016-07-19 23:40:41 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 12:50:48 +00:00
|
|
|
// 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)
|
2016-07-19 23:40:41 +00:00
|
|
|
|
2019-06-19 12:50:48 +00:00
|
|
|
matched, err := regexp.MatchString(config.BrokerCIDRegex, brokerCID)
|
2016-07-19 23:40:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-19 12:50:48 +00:00
|
|
|
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))
|
|
|
|
}
|
2016-07-19 23:40:41 +00:00
|
|
|
|
|
|
|
response := new(Broker)
|
|
|
|
if err := json.Unmarshal(result, &response); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-19 12:50:48 +00:00
|
|
|
// FetchBrokers returns all brokers available to the API Token.
|
|
|
|
func (a *API) FetchBrokers() (*[]Broker, error) {
|
|
|
|
result, err := a.Get(config.BrokerPrefix)
|
2016-07-19 23:40:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-19 12:50:48 +00:00
|
|
|
var response []Broker
|
|
|
|
if err := json.Unmarshal(result, &response); err != nil {
|
2016-11-09 20:30:07 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-07-19 23:40:41 +00:00
|
|
|
|
2019-06-19 12:50:48 +00:00
|
|
|
return &response, nil
|
2016-07-19 23:40:41 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 12:50:48 +00:00
|
|
|
// 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())
|
2016-07-19 23:40:41 +00:00
|
|
|
if err != nil {
|
2019-06-19 12:50:48 +00:00
|
|
|
return nil, fmt.Errorf("[ERROR] API call error %+v", err)
|
2016-07-19 23:40:41 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 12:50:48 +00:00
|
|
|
var brokers []Broker
|
|
|
|
if err := json.Unmarshal(result, &brokers); err != nil {
|
2016-11-09 20:30:07 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-07-19 23:40:41 +00:00
|
|
|
|
2019-06-19 12:50:48 +00:00
|
|
|
return &brokers, nil
|
2016-07-19 23:40:41 +00:00
|
|
|
}
|