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