-
Notifications
You must be signed in to change notification settings - Fork 72
/
topics_util.go
112 lines (93 loc) · 2.56 KB
/
topics_util.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
package kafkacmdutil
import (
"github.com/redhat-developer/app-services-cli/pkg/shared/contextutil"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
"github.com/spf13/cobra"
)
// FilterValidTopicNameArgs filters topics from the API and returns the names
// This is used in for dynamic completion of topic names
func FilterValidTopicNameArgs(f *factory.Factory, toComplete string) (validNames []string, directive cobra.ShellCompDirective) {
validNames = []string{}
directive = cobra.ShellCompDirectiveNoSpace
svcContext, err := f.ServiceContext.Load()
if err != nil {
return validNames, directive
}
currCtx, err := contextutil.GetCurrentContext(svcContext, f.Localizer)
if err != nil {
return validNames, directive
}
instanceID := currCtx.KafkaID
if instanceID == "" {
return validNames, directive
}
conn, err := f.Connection()
if err != nil {
return validNames, directive
}
api, _, err := conn.API().KafkaAdmin(instanceID)
if err != nil {
return validNames, directive
}
req := api.TopicsApi.GetTopics(f.Context)
if toComplete != "" {
req = req.Filter(toComplete)
}
topicRes, httpRes, err := req.Execute()
if httpRes != nil {
defer func() {
_ = httpRes.Body.Close()
}()
}
if err != nil {
return validNames, directive
}
items := topicRes.GetItems()
for _, topic := range items {
validNames = append(validNames, topic.GetName())
}
return validNames, directive
}
// FilterValidConsumerGroupIDs returns the list of consumer group IDs from the API
func FilterValidConsumerGroupIDs(f *factory.Factory, toComplete string) (validIDs []string, directive cobra.ShellCompDirective) {
validIDs = []string{}
directive = cobra.ShellCompDirectiveNoSpace
svcContext, err := f.ServiceContext.Load()
if err != nil {
return validIDs, directive
}
currCtx, err := contextutil.GetCurrentContext(svcContext, f.Localizer)
if err != nil {
return validIDs, directive
}
instanceID := currCtx.KafkaID
if instanceID == "" {
return validIDs, directive
}
conn, err := f.Connection()
if err != nil {
return validIDs, directive
}
api, _, err := conn.API().KafkaAdmin(instanceID)
if err != nil {
return validIDs, directive
}
req := api.GroupsApi.GetConsumerGroups(f.Context)
if toComplete != "" {
req = req.GroupIdFilter(toComplete)
}
cgRes, httpRes, err := req.Execute()
if httpRes != nil {
defer func() {
_ = httpRes.Body.Close()
}()
}
if err != nil {
return validIDs, directive
}
items := cgRes.GetItems()
for _, cg := range items {
validIDs = append(validIDs, cg.GetGroupId())
}
return validIDs, directive
}