-
Notifications
You must be signed in to change notification settings - Fork 11
/
exhibitor.go
86 lines (71 loc) · 1.89 KB
/
exhibitor.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
package main
import (
"net/http"
"crypto/tls"
"fmt"
"encoding/json"
"errors"
)
func KafkaSeedBrokers(exhibitorUrl, kafkaChroot string) ([]string, error) {
tr := http.DefaultTransport.(*http.Transport)
tr.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
client := &http.Client{Transport: tr}
listUrl := fmt.Sprintf("%s/exhibitor/v1/explorer/node/?key=/%s/%s", exhibitorUrl, kafkaChroot, "brokers/ids")
res, err := client.Get(listUrl)
if err != nil {
return nil, err
}
if res.StatusCode != 200 {
return nil, errors.New(fmt.Sprintf("non 200 from get broker list: %d %s", res.StatusCode, res.Body))
}
var dir []ZkDirEntry
err = json.NewDecoder(res.Body).Decode(&dir)
if err != nil {
return nil, err
}
var addresses []string
for _, broker := range dir {
address, err := brokerAddress(client, exhibitorUrl, broker.Key)
if err != nil {
return nil, err
}
fmt.Println(address)
addresses = append(addresses, address + "/" + kafkaChroot)
}
return addresses, nil
}
func brokerAddress(client *http.Client, exhibitorUrl, zkPath string) (string, error) {
seedUrl := fmt.Sprintf("%s/exhibitor/v1/explorer/node-data/?key=%s", exhibitorUrl, zkPath)
res2, err := client.Get(seedUrl)
if err != nil {
return "", err
}
if res2.StatusCode != 200 {
return "", errors.New("non 200 from get broker info")
}
brokerNode := &ZkNodeEntry{}
err = json.NewDecoder(res2.Body).Decode(brokerNode)
if err != nil {
return "", err
}
seedBrokerJson := brokerNode.Str
seedBroker := &ZkBrokerEntry{}
err = json.Unmarshal([]byte(seedBrokerJson), seedBroker)
if err != nil {
return "", err
}
return fmt.Sprintf("%s:%d", seedBroker.Host, seedBroker.Port), nil
}
type ZkDirEntry struct{
Title string `json:"title"`
Key string `json:"key"`
}
type ZkNodeEntry struct {
Str string `json:"str"`
}
type ZkBrokerEntry struct {
Host string `json:"host"`
Port int `json:"port"`
}