-
Notifications
You must be signed in to change notification settings - Fork 9
/
admin.go
306 lines (241 loc) · 6.14 KB
/
admin.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package kafkactl
import (
"errors"
"github.com/Shopify/sarama"
)
type clusterAdmin struct {
client sarama.Client
conf *sarama.Config
}
func (kc *KClient) Admin() sarama.ClusterAdmin {
return &clusterAdmin{
client: kc.cl,
conf: kc.config,
}
}
func (ca *clusterAdmin) Close() error {
return ca.client.Close()
}
func (ca *clusterAdmin) Controller() (*sarama.Broker, error) {
return ca.client.Controller()
}
func (ca *clusterAdmin) CreateTopic(topic string, detail *sarama.TopicDetail, validateOnly bool) error {
if topic == "" {
return sarama.ErrInvalidTopic
}
if detail == nil {
return errors.New("You must specify topic details")
}
topicDetails := make(map[string]*sarama.TopicDetail)
topicDetails[topic] = detail
request := &sarama.CreateTopicsRequest{
TopicDetails: topicDetails,
ValidateOnly: validateOnly,
Timeout: ca.conf.Admin.Timeout,
}
if ca.conf.Version.IsAtLeast(sarama.V0_11_0_0) {
request.Version = 1
}
if ca.conf.Version.IsAtLeast(sarama.V1_0_0_0) {
request.Version = 2
}
b, err := ca.Controller()
if err != nil {
return err
}
rsp, err := b.CreateTopics(request)
if err != nil {
return err
}
topicErr, ok := rsp.TopicErrors[topic]
if !ok {
return sarama.ErrIncompleteResponse
}
if topicErr.Err != sarama.ErrNoError {
return topicErr.Err
}
return nil
}
func (ca *clusterAdmin) DeleteTopic(topic string) error {
if topic == "" {
return sarama.ErrInvalidTopic
}
request := &sarama.DeleteTopicsRequest{
Topics: []string{topic},
Timeout: ca.conf.Admin.Timeout,
}
if ca.conf.Version.IsAtLeast(sarama.V0_11_0_0) {
request.Version = 1
}
b, err := ca.Controller()
if err != nil {
return err
}
rsp, err := b.DeleteTopics(request)
if err != nil {
return err
}
topicErr, ok := rsp.TopicErrorCodes[topic]
if !ok {
return sarama.ErrIncompleteResponse
}
if topicErr != sarama.ErrNoError {
return topicErr
}
return nil
}
func (ca *clusterAdmin) CreatePartitions(topic string, count int32, assignment [][]int32, validateOnly bool) error {
if topic == "" {
return sarama.ErrInvalidTopic
}
topicPartitions := make(map[string]*sarama.TopicPartition)
topicPartitions[topic] = &sarama.TopicPartition{Count: count, Assignment: assignment}
request := &sarama.CreatePartitionsRequest{
TopicPartitions: topicPartitions,
Timeout: ca.conf.Admin.Timeout,
}
b, err := ca.Controller()
if err != nil {
return err
}
rsp, err := b.CreatePartitions(request)
if err != nil {
return err
}
topicErr, ok := rsp.TopicPartitionErrors[topic]
if !ok {
return sarama.ErrIncompleteResponse
}
if topicErr.Err != sarama.ErrNoError {
return topicErr.Err
}
return nil
}
func (ca *clusterAdmin) DeleteRecords(topic string, partitionOffsets map[int32]int64) error {
if topic == "" {
return sarama.ErrInvalidTopic
}
topics := make(map[string]*sarama.DeleteRecordsRequestTopic)
topics[topic] = &sarama.DeleteRecordsRequestTopic{PartitionOffsets: partitionOffsets}
request := &sarama.DeleteRecordsRequest{
Topics: topics,
Timeout: ca.conf.Admin.Timeout,
}
b, err := ca.Controller()
if err != nil {
return err
}
rsp, err := b.DeleteRecords(request)
if err != nil {
return err
}
_, ok := rsp.Topics[topic]
if !ok {
return sarama.ErrIncompleteResponse
}
//todo since we are dealing with couple of partitions it would be good if we return slice of errors
//for each partition instead of one error
return nil
}
func (ca *clusterAdmin) DescribeConfig(resource sarama.ConfigResource) ([]sarama.ConfigEntry, error) {
var entries []sarama.ConfigEntry
var resources []*sarama.ConfigResource
resources = append(resources, &resource)
request := &sarama.DescribeConfigsRequest{
Resources: resources,
}
b, err := ca.Controller()
if err != nil {
return nil, err
}
rsp, err := b.DescribeConfigs(request)
if err != nil {
return nil, err
}
for _, rspResource := range rsp.Resources {
if rspResource.Name == resource.Name {
if rspResource.ErrorMsg != "" {
return nil, errors.New(rspResource.ErrorMsg)
}
for _, cfgEntry := range rspResource.Configs {
entries = append(entries, *cfgEntry)
}
}
}
return entries, nil
}
func (ca *clusterAdmin) AlterConfig(resourceType sarama.ConfigResourceType, name string, entries map[string]*string, validateOnly bool) error {
var resources []*sarama.AlterConfigsResource
resources = append(resources, &sarama.AlterConfigsResource{
Type: resourceType,
Name: name,
ConfigEntries: entries,
})
request := &sarama.AlterConfigsRequest{
Resources: resources,
ValidateOnly: validateOnly,
}
b, err := ca.Controller()
if err != nil {
return err
}
rsp, err := b.AlterConfigs(request)
if err != nil {
return err
}
for _, rspResource := range rsp.Resources {
if rspResource.Name == name {
if rspResource.ErrorMsg != "" {
return errors.New(rspResource.ErrorMsg)
}
}
}
return nil
}
func (ca *clusterAdmin) CreateACL(resource sarama.Resource, acl sarama.Acl) error {
var acls []*sarama.AclCreation
acls = append(acls, &sarama.AclCreation{resource, acl})
request := &sarama.CreateAclsRequest{AclCreations: acls}
b, err := ca.Controller()
if err != nil {
return err
}
_, err = b.CreateAcls(request)
return err
}
func (ca *clusterAdmin) ListAcls(filter sarama.AclFilter) ([]sarama.ResourceAcls, error) {
request := &sarama.DescribeAclsRequest{AclFilter: filter}
b, err := ca.Controller()
if err != nil {
return nil, err
}
rsp, err := b.DescribeAcls(request)
if err != nil {
return nil, err
}
var lAcls []sarama.ResourceAcls
for _, rAcl := range rsp.ResourceAcls {
lAcls = append(lAcls, *rAcl)
}
return lAcls, nil
}
func (ca *clusterAdmin) DeleteACL(filter sarama.AclFilter, validateOnly bool) ([]sarama.MatchingAcl, error) {
var filters []*sarama.AclFilter
filters = append(filters, &filter)
request := &sarama.DeleteAclsRequest{Filters: filters}
b, err := ca.Controller()
if err != nil {
return nil, err
}
rsp, err := b.DeleteAcls(request)
if err != nil {
return nil, err
}
var mAcls []sarama.MatchingAcl
for _, fr := range rsp.FilterResponses {
for _, mACL := range fr.MatchingAcls {
mAcls = append(mAcls, *mACL)
}
}
return mAcls, nil
}