-
Notifications
You must be signed in to change notification settings - Fork 2
/
topics.go
217 lines (188 loc) · 6.08 KB
/
topics.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package gokafkaesque
import (
"fmt"
)
func callError(msg string) func(error) error {
return func(err error) error {
return fmt.Errorf("%s ERROR: %v", msg, err.Error())
}
}
// GetTopics is a method that returns all Kafka topics.
func (client *Client) GetTopics() (AllTopics, error) {
e := callError("LIST TOPICS")
resp, err := client.Rest.R().Get("/topics")
if err != nil {
return AllTopics{}, e(err)
}
if resp.StatusCode() >= 200 && resp.StatusCode() <= 299 {
var data AllTopics
err := client.Rest.JSONUnmarshal(resp.Body(), &data)
if err != nil {
return AllTopics{}, e(err)
}
return data, nil
}
return AllTopics{}, e(fmt.Errorf("%v: %v", resp.Status(), string(resp.Body())))
}
// Count is a method that returns total size of topics.
func (t AllTopics) Count() int {
return len(t.Topics)
}
// TopicsToString is a method that returns a slice of topics.
func (t AllTopics) TopicsToString() []string {
return t.Topics
}
// GetTopic is a method that return a Kafka topic
func (client *Client) GetTopic(t string) (Topic, error) {
e := callError(fmt.Sprintf("GET TOPIC %s", t))
resp, err := client.Rest.R().Get(uriPath("/topics", t))
if err != nil {
return Topic{}, e(err)
}
if resp.StatusCode() >= 200 && resp.StatusCode() <= 299 {
var data Topic
err := client.Rest.JSONUnmarshal(resp.Body(), &data)
if err != nil {
return Topic{}, e(err)
}
return data, nil
}
return Topic{}, e(fmt.Errorf("%v: %v", resp.Status(), string(resp.Body())))
}
// GetPartitions is a method that returns partitions of a topic.
func (t Topic) GetPartitions() string {
return t.Partitions
}
// GetReplicationFactor is a method that returns partitions of a topic.
func (t Topic) GetReplicationFactor() string {
return fmt.Sprintf("%s", t.ReplicationFactor)
}
// GetRetentionMs is a method that returns partitions of a topic.
func (c *Config) GetRetentionMs() string {
return fmt.Sprintf("%s", c.RetentionMs)
}
// GetSegmentBytes is a method that returns partitions of a topic.
func (c *Config) GetSegmentBytes() string {
return fmt.Sprintf("%s", c.SegmentBytes)
}
// GetCleanupPolicy is a method that returns cleanup policy of a topic.
func (c *Config) GetCleanupPolicy() string {
return fmt.Sprintf("%s", c.CleanupPolicy)
}
// GetSegmentMs is a method that returns the time after which Kafka will
// force the log to rollof a topic.
func (c *Config) GetSegmentMs() string {
return fmt.Sprintf("%s", c.SegmentMs)
}
// GetRetentionBytes is a method that returns the retention bytes for the topic.
// force the log to rollof a topic.
func (c *Config) GetRetentionBytes() string {
return fmt.Sprintf("%s", c.RetentionBytes)
}
// GetMinInSyncReplicas is a method that returns the minimum number of insync replicas.
// force the log to rollof a topic.
func (c *Config) GetMinInSyncReplicas() string {
return fmt.Sprintf("%s", c.MinInsyncReplicas)
}
// TopicBuilder is an interface that builds a Kafka Topic
// Config.
type TopicBuilder interface {
SetPartitions(string) TopicBuilder
SetReplicationFactor(string) TopicBuilder
SetConfig(Config) TopicBuilder
BuildTopic() Topic
}
// NewTopic accepts a string topic name and returns a TopicBuilder interface.
func NewTopic(name string) TopicBuilder {
return &Topic{
Name: &name,
}
}
// SetPartitions is a method that accepts an int64 and sets Topic
// partition.
func (t *Topic) SetPartitions(p string) TopicBuilder {
t.Partitions = p
return t
}
// SetReplicationFactor is a method that accepts an int64 and sets Topic
// replication factor.
func (t *Topic) SetReplicationFactor(r string) TopicBuilder {
t.ReplicationFactor = r
return t
}
// SetConfig is a method that accepts a Config struct and
// sets Topic config such as retention periond in ms.
func (t *Topic) SetConfig(c Config) TopicBuilder {
t.Config = &c
return t
}
// BuildTopic is a method that builds a Topic parameters and pass
// this as an argument when calling CreateTopic method.
func (t *Topic) BuildTopic() Topic {
return Topic{
Name: t.Name,
ReplicationFactor: t.ReplicationFactor,
Partitions: t.Partitions,
Config: t.Config,
}
}
// CreateTopic accepts a Topic and returns an "Ok" response
// or error.
func (client *Client) CreateTopic(t Topic) (Response, error) {
e := callError(fmt.Sprintf("CREATE TOPIC %+v", t))
resp, err := client.Rest.R().SetBody(t).Post("/topics")
if err != nil {
return Response{}, e(err)
}
if resp.StatusCode() >= 200 && resp.StatusCode() <= 299 {
var data Response
err := client.Rest.JSONUnmarshal(resp.Body(), &data)
if err != nil {
return Response{}, e(err)
}
return data, nil
}
return Response{}, e(fmt.Errorf("%v: %v", resp.Status(), string(resp.Body())))
}
// DeleteTopic method accepts a string topic, deletes this Kafka topic and
// returns a string response or error.
func (client *Client) DeleteTopic(t string) (Response, error) {
e := callError(fmt.Sprintf("DELETE TOPIC %v", t))
resp, err := client.Rest.R().Delete(uriPath("/topics", t))
if err != nil {
return Response{}, e(err)
}
if resp.StatusCode() >= 200 && resp.StatusCode() <= 299 {
var data Response
err := client.Rest.JSONUnmarshal(resp.Body(), &data)
if err != nil {
return Response{}, e(err)
}
return data, nil
}
return Response{}, e(fmt.Errorf("%v: %v", resp.Status(), string(resp.Body())))
}
// uriPath function accepts path and topic string and returns a
// valid uri path of /topics/<topic_name>.
func uriPath(p, t string) string {
return p + "/" + t
}
// UpdateTopic is a method that update a Kafka topic. This requires complete
// config parameters set. If we want to allow only update optional params,
// we need to implement PATCH request instead.
func (client *Client) UpdateTopic(t Topic) (Response, error) {
e := callError(fmt.Sprintf("Update TOPIC %+v", t))
resp, err := client.Rest.R().SetBody(t).Put(uriPath("/topics", *t.Name))
if err != nil {
return Response{}, e(err)
}
if resp.StatusCode() >= 200 && resp.StatusCode() <= 299 {
var data Response
err := client.Rest.JSONUnmarshal(resp.Body(), &data)
if err != nil {
return Response{}, e(err)
}
return data, nil
}
return Response{}, e(fmt.Errorf("%v", resp.Status()))
}