#224 added deleteTopic operation api (#228)

* added deleteTopic operation

* added not found response
This commit is contained in:
Ramazan Yapparov 2021-03-04 17:51:18 +03:00 committed by GitHub
parent ae1acbce9b
commit a9cb9567d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 0 deletions

View file

@ -1,5 +1,6 @@
package com.provectus.kafka.ui.cluster.service; package com.provectus.kafka.ui.cluster.service;
import com.provectus.kafka.ui.cluster.exception.NotFoundException;
import com.provectus.kafka.ui.cluster.mapper.ClusterMapper; import com.provectus.kafka.ui.cluster.mapper.ClusterMapper;
import com.provectus.kafka.ui.cluster.model.ClustersStorage; import com.provectus.kafka.ui.cluster.model.ClustersStorage;
import com.provectus.kafka.ui.cluster.model.ConsumerPosition; import com.provectus.kafka.ui.cluster.model.ConsumerPosition;
@ -169,12 +170,27 @@ public class ClusterService {
).orElse(Mono.empty()); ).orElse(Mono.empty());
} }
public Mono<Void> deleteTopic(String clusterName, String topicName) {
var cluster = clustersStorage.getClusterByName(clusterName)
.orElseThrow(() -> new NotFoundException("No such cluster"));
getTopicDetails(clusterName, topicName)
.orElseThrow(() -> new NotFoundException("No such topic"));
return kafkaService.deleteTopic(cluster, topicName)
.doOnNext(t -> updateCluster(topicName, clusterName, cluster));
}
private KafkaCluster updateCluster(InternalTopic topic, String clusterName, KafkaCluster cluster) { private KafkaCluster updateCluster(InternalTopic topic, String clusterName, KafkaCluster cluster) {
final KafkaCluster updatedCluster = kafkaService.getUpdatedCluster(cluster, topic); final KafkaCluster updatedCluster = kafkaService.getUpdatedCluster(cluster, topic);
clustersStorage.setKafkaCluster(clusterName, updatedCluster); clustersStorage.setKafkaCluster(clusterName, updatedCluster);
return updatedCluster; return updatedCluster;
} }
private KafkaCluster updateCluster(String topicToDelete, String clusterName, KafkaCluster cluster) {
final KafkaCluster updatedCluster = kafkaService.getUpdatedCluster(cluster, topicToDelete);
clustersStorage.setKafkaCluster(clusterName, updatedCluster);
return updatedCluster;
}
public Flux<TopicMessage> getMessages(String clusterName, String topicName, ConsumerPosition consumerPosition, String query, Integer limit) { public Flux<TopicMessage> getMessages(String clusterName, String topicName, ConsumerPosition consumerPosition, String query, Integer limit) {
return clustersStorage.getClusterByName(clusterName) return clustersStorage.getClusterByName(clusterName)
.map(c -> consumingService.loadMessages(c, topicName, consumerPosition, query, limit)) .map(c -> consumingService.loadMessages(c, topicName, consumerPosition, query, limit))

View file

@ -56,6 +56,12 @@ public class KafkaService {
return cluster.toBuilder().topics(topics).build(); return cluster.toBuilder().topics(topics).build();
} }
public KafkaCluster getUpdatedCluster(KafkaCluster cluster, String topicToDelete) {
final Map<String, InternalTopic> topics = new HashMap<>(cluster.getTopics());
topics.remove(topicToDelete);
return cluster.toBuilder().topics(topics).build();
}
@SneakyThrows @SneakyThrows
public Mono<KafkaCluster> getUpdatedCluster(KafkaCluster cluster) { public Mono<KafkaCluster> getUpdatedCluster(KafkaCluster cluster) {
return getOrCreateAdminClient(cluster) return getOrCreateAdminClient(cluster)
@ -184,6 +190,13 @@ public class KafkaService {
return getOrCreateAdminClient(cluster).flatMap(ac -> createTopic(ac.getAdminClient(), topicFormData)); return getOrCreateAdminClient(cluster).flatMap(ac -> createTopic(ac.getAdminClient(), topicFormData));
} }
public Mono<Void> deleteTopic(KafkaCluster cluster, String topicName) {
return getOrCreateAdminClient(cluster)
.map(ExtendedAdminClient::getAdminClient)
.map(adminClient -> adminClient.deleteTopics(List.of(topicName)))
.then();
}
@SneakyThrows @SneakyThrows
public Mono<InternalTopic> createTopic(AdminClient adminClient, Mono<TopicFormData> topicFormData) { public Mono<InternalTopic> createTopic(AdminClient adminClient, Mono<TopicFormData> topicFormData) {
return topicFormData.flatMap( return topicFormData.flatMap(

View file

@ -158,6 +158,11 @@ public class MetricsRestController implements ApiClustersApi {
return clusterService.updateTopic(clusterId, topicName, topicFormData).map(ResponseEntity::ok); return clusterService.updateTopic(clusterId, topicName, topicFormData).map(ResponseEntity::ok);
} }
@Override
public Mono<ResponseEntity<Void>> deleteTopic(String clusterName, String topicName, ServerWebExchange exchange) {
return clusterService.deleteTopic(clusterName, topicName).map(ResponseEntity::ok);
}
@Override @Override
public Mono<ResponseEntity<CompatibilityLevel>> getGlobalSchemaCompatibilityLevel(String clusterName, ServerWebExchange exchange) { public Mono<ResponseEntity<CompatibilityLevel>> getGlobalSchemaCompatibilityLevel(String clusterName, ServerWebExchange exchange) {
return schemaRegistryService.getGlobalSchemaCompatibilityLevel(clusterName) return schemaRegistryService.getGlobalSchemaCompatibilityLevel(clusterName)

View file

@ -215,6 +215,27 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/Topic' $ref: '#/components/schemas/Topic'
delete:
tags:
- /api/clusters
summary: deleteTopic
operationId: deleteTopic
parameters:
- name: clusterName
in: path
required: true
schema:
type: string
- name: topicName
in: path
required: true
schema:
type: string
responses:
200:
description: OK
404:
description: Not found
/api/clusters/{clusterName}/topics/{topicName}/config: /api/clusters/{clusterName}/topics/{topicName}/config:
get: get: