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