-
-
Notifications
You must be signed in to change notification settings - Fork 639
/
ConnectRepository.java
250 lines (220 loc) · 9.52 KB
/
ConnectRepository.java
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
package org.akhq.repositories;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException;
import io.micronaut.context.ApplicationContext;
import io.micronaut.retry.annotation.Retryable;
import io.micronaut.security.authentication.Authentication;
import io.micronaut.security.utils.SecurityService;
import org.akhq.models.ConnectDefinition;
import org.akhq.models.ConnectPlugin;
import org.akhq.modules.KafkaModule;
import org.akhq.utils.PagedList;
import org.akhq.utils.Pagination;
import org.sourcelab.kafka.connect.apiclient.request.dto.*;
import org.sourcelab.kafka.connect.apiclient.rest.exceptions.ConcurrentConfigModificationException;
import org.sourcelab.kafka.connect.apiclient.rest.exceptions.InvalidRequestException;
import org.sourcelab.kafka.connect.apiclient.rest.exceptions.ResourceNotFoundException;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
@Singleton
public class ConnectRepository extends AbstractRepository {
@Inject
private KafkaModule kafkaModule;
@Inject
private ApplicationContext applicationContext;
private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
@Retryable(includes = {
ConcurrentConfigModificationException.class,
ResourceNotFoundException.class
}, delay = "3s", attempts = "5")
public ConnectDefinition getDefinition(String clusterId, String connectId, String name) {
return new ConnectDefinition(
this.kafkaModule
.getConnectRestClient(clusterId)
.get(connectId)
.getConnector(name),
this.kafkaModule
.getConnectRestClient(clusterId)
.get(connectId)
.getConnectorStatus(name)
);
}
@Retryable(includes = {
ConcurrentConfigModificationException.class,
ResourceNotFoundException.class
}, delay = "3s", attempts = "5")
public PagedList<ConnectDefinition> getPaginatedDefinitions (String clusterId, String connectId, Pagination pagination, Optional<String> search, List<String> filters)
throws IOException, RestClientException, ExecutionException, InterruptedException{
List<ConnectDefinition> definitions = getDefinitions(clusterId, connectId, search, filters);
// I'm not sure of how to use the last parameter in this case
// I look at the implementation for the Schema Registry part, but I don't see how make a similar thing here
return PagedList.of(definitions, pagination, list -> list);
}
public List<ConnectDefinition> getDefinitions(String clusterId, String connectId, Optional<String> search, List<String> filters)
{
ConnectorsWithExpandedMetadata unfiltered = this.kafkaModule
.getConnectRestClient(clusterId)
.get(connectId)
.getConnectorsWithAllExpandedMetadata();
Collection<ConnectorDefinition> definitions = unfiltered.getAllDefinitions();
Collection<ConnectorDefinition> connectorsFilteredBySearch =
definitions.stream().filter(connector -> isSearchMatch(search, connector.getName())
&& isMatchRegex(filters, connector.getName())
).collect(Collectors.toList());
ArrayList<ConnectDefinition> filtered = new ArrayList<>();
for (ConnectorDefinition item : connectorsFilteredBySearch) {
if (isMatchRegex(filters, item.getName())) {
filtered.add(new ConnectDefinition(
item,
unfiltered.getStatusForConnector(item.getName())
));
}
}
return filtered;
}
public Optional<ConnectPlugin> validatePlugin(String clusterId, String connectId, String className,
Map<String, String> configs) {
return this.kafkaModule
.getConnectRestClient(clusterId)
.get(connectId)
.getConnectorPlugins()
.stream()
.filter(connectPlugin -> connectPlugin.getClassName().equals(className))
.map(s -> mapToConnectPlugin(s, clusterId, connectId, configs))
.findFirst();
}
public List<ConnectPlugin> getPlugins(String clusterId, String connectId) {
return this.kafkaModule
.getConnectRestClient(clusterId)
.get(connectId)
.getConnectorPlugins()
.stream()
.map(s -> mapToConnectPlugin(s, clusterId, connectId))
.collect(Collectors.toList());
}
public ConnectDefinition create(String clusterId, String connectId, String name, Map<String, String> configs) {
try {
this.kafkaModule
.getConnectRestClient(clusterId)
.get(connectId)
.addConnector(new NewConnectorDefinition(name, configs));
} catch (InvalidRequestException e) {
throw new IllegalArgumentException(e);
}
return getDefinition(clusterId, connectId, name);
}
public ConnectDefinition update(String clusterId, String connectId, String name, Map<String, String> configs) {
try {
this.kafkaModule
.getConnectRestClient(clusterId)
.get(connectId)
.updateConnectorConfig(name, configs);
} catch (InvalidRequestException e) {
throw new IllegalArgumentException(e);
}
return getDefinition(clusterId, connectId, name);
}
public boolean delete(String clusterId, String connectId, String name) {
try {
return this.kafkaModule
.getConnectRestClient(clusterId)
.get(connectId)
.deleteConnector(name);
} catch (InvalidRequestException e) {
throw new IllegalArgumentException(e);
}
}
public boolean pause(String clusterId, String connectId, String name) {
try {
return this.kafkaModule
.getConnectRestClient(clusterId)
.get(connectId)
.pauseConnector(name);
} catch (InvalidRequestException e) {
throw new IllegalArgumentException(e);
}
}
public boolean resume(String clusterId, String connectId, String name) {
try {
return this.kafkaModule
.getConnectRestClient(clusterId)
.get(connectId)
.resumeConnector(name);
} catch (InvalidRequestException e) {
throw new IllegalArgumentException(e);
}
}
public boolean restart(String clusterId, String connectId, String name) {
try {
return this.kafkaModule
.getConnectRestClient(clusterId)
.get(connectId)
.restartConnector(name);
} catch (InvalidRequestException e) {
throw new IllegalArgumentException(e);
}
}
public boolean restartTask(String clusterId, String connectId, String name, int task) {
try {
return this.kafkaModule
.getConnectRestClient(clusterId)
.get(connectId)
.restartConnectorTask(name, task);
} catch (InvalidRequestException e) {
throw new IllegalArgumentException(e);
}
}
public static Map<String, String> validConfigs(Map<String, String> configs, String transformsValue) {
Map<String, String> list = configs
.entrySet()
.stream()
.filter(config -> !config.getKey().equals("transforms-value"))
.filter(config -> !config.getValue().isEmpty())
.filter(config -> config.getKey().startsWith("configs["))
.map(entry -> new AbstractMap.SimpleEntry<>(
entry.getKey().substring("configs[".length(), entry.getKey().length() - 1),
entry.getValue()
)
)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
if (!transformsValue.trim().isEmpty()) {
list.putAll(gson.fromJson(
transformsValue,
new TypeToken<HashMap<String, String>>() {
}.getType()
));
}
return list;
}
private ConnectPlugin mapToConnectPlugin(ConnectorPlugin plugin, String clusterId, String connectId) {
Map<String,String> config = ImmutableMap.of(
"connector.class", plugin.getClassName(),
"topics", "getPlugins"
);
return this.mapToConnectPlugin(plugin, clusterId, connectId, config);
}
private ConnectPlugin mapToConnectPlugin(ConnectorPlugin plugin, String clusterId, String connectId,
Map<String,String> config) {
return new ConnectPlugin(
plugin,
this.kafkaModule
.getConnectRestClient(clusterId)
.get(connectId)
.validateConnectorPluginConfig(new ConnectorPluginConfigDefinition(
Iterables.getLast(Arrays.asList(plugin.getClassName().split("/"))),
config
)));
}
private String getShortClassName(String className) {
String[] split = className.split("\\.");
return split[split.length - 1];
}
}