-
Notifications
You must be signed in to change notification settings - Fork 5
/
broker.go
139 lines (114 loc) · 4.06 KB
/
broker.go
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
133
134
135
136
137
138
139
// 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 apiclient
import (
"encoding/json"
"fmt"
"net/url"
"regexp"
"strings"
"github.com/circonus-labs/go-apiclient/config"
"github.com/pkg/errors"
)
// BrokerDetail defines instance attributes
type BrokerDetail struct {
CN string `json:"cn"` // string
Status string `json:"status"` // string
ClusterIP *string `json:"cluster_ip"` // string or null
ExternalHost *string `json:"external_host"` // string or null
IP *string `json:"ipaddress"` // string or null
Skew *string `json:"skew"` // BUG doc: floating point number, api object: string or null
Port *uint16 `json:"port"` // uint16 or null
Version *uint `json:"version"` // uint or null
Modules []string `json:"modules"` // [] len >= 0
ExternalPort uint16 `json:"external_port"` // uint16
MinVer uint `json:"minimum_version_required"` // uint
}
// Broker defines a broker. See https://login.circonus.com/resources/api/calls/broker for more information.
type Broker struct {
CID string `json:"_cid"` // string
Name string `json:"_name"` // string
Type string `json:"_type"` // string
Latitude *string `json:"_latitude"` // string or null
Longitude *string `json:"_longitude"` // string or null
Tags []string `json:"_tags"` // [] len >= 0
Details []BrokerDetail `json:"_details"` // [] len >= 1
}
// FetchBroker retrieves broker with passed cid.
func (a *API) FetchBroker(cid CIDType) (*Broker, error) {
if cid == nil || *cid == "" {
return nil, errors.Errorf("invalid broker CID (none)")
}
var brokerCID string
if !strings.HasPrefix(*cid, config.BrokerPrefix) {
brokerCID = fmt.Sprintf("%s/%s", config.BrokerPrefix, *cid)
} else {
brokerCID = *cid
}
matched, err := regexp.MatchString(config.BrokerCIDRegex, brokerCID)
if err != nil {
return nil, err
}
if !matched {
return nil, errors.Errorf("invalid broker CID (%s)", brokerCID)
}
result, err := a.Get(brokerCID)
if err != nil {
return nil, errors.Wrap(err, "fetching broker")
}
if a.Debug {
a.Log.Printf("fetch broker, received JSON: %s", string(result))
}
response := new(Broker)
if err := json.Unmarshal(result, &response); err != nil {
return nil, errors.Wrap(err, "parsing broker")
}
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, errors.Wrap(err, "fetching brokers")
}
var response []Broker
if err := json.Unmarshal(result, &response); err != nil {
return nil, errors.Wrap(err, "parsing brokers")
}
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, errors.Wrap(err, "searching brokers")
}
var brokers []Broker
if err := json.Unmarshal(result, &brokers); err != nil {
return nil, errors.Wrap(err, "parsing brokers")
}
return &brokers, nil
}