Return id, version, schema, and subject after creating a new schema

This commit is contained in:
Ildar Almakaev 2021-02-26 10:41:08 +03:00
parent 655aa41a72
commit 82f879c916
4 changed files with 44 additions and 50 deletions

View file

@ -0,0 +1,8 @@
package com.provectus.kafka.ui.cluster.model.schemaregistry;
import lombok.Data;
@Data
public class SubjectIdResponse {
private Integer id;
}

View file

@ -5,6 +5,7 @@ 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.InternalCompatibilityCheck; import com.provectus.kafka.ui.cluster.model.InternalCompatibilityCheck;
import com.provectus.kafka.ui.cluster.model.InternalCompatibilityLevel; import com.provectus.kafka.ui.cluster.model.InternalCompatibilityLevel;
import com.provectus.kafka.ui.cluster.model.schemaregistry.SubjectIdResponse;
import com.provectus.kafka.ui.model.CompatibilityCheckResponse; import com.provectus.kafka.ui.model.CompatibilityCheckResponse;
import com.provectus.kafka.ui.model.CompatibilityLevel; import com.provectus.kafka.ui.model.CompatibilityLevel;
import com.provectus.kafka.ui.model.NewSchemaSubject; import com.provectus.kafka.ui.model.NewSchemaSubject;
@ -12,20 +13,20 @@ import com.provectus.kafka.ui.model.SchemaSubject;
import java.util.Formatter; import java.util.Formatter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.core.ParameterizedTypeReference; import org.jetbrains.annotations.NotNull;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import java.util.Arrays;
import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.function.Function;
import static org.springframework.http.HttpStatus.NOT_FOUND;
@Service @Service
@Log4j2 @Log4j2
@ -69,10 +70,8 @@ public class SchemaRegistryService {
.map(cluster -> webClient.get() .map(cluster -> webClient.get()
.uri(cluster.getSchemaRegistry() + URL_SUBJECT_VERSIONS, schemaName) .uri(cluster.getSchemaRegistry() + URL_SUBJECT_VERSIONS, schemaName)
.retrieve() .retrieve()
.onStatus(HttpStatus.NOT_FOUND::equals, .onStatus(NOT_FOUND::equals,
resp -> Mono.error( throwIfNotFoundStatus(formatted("No such schema %s"))
new NotFoundException(formatted("No such schema %s"))
)
).bodyToFlux(Integer.class) ).bodyToFlux(Integer.class)
).orElse(Flux.error(new NotFoundException("No such cluster"))); ).orElse(Flux.error(new NotFoundException("No such cluster")));
} }
@ -90,12 +89,8 @@ public class SchemaRegistryService {
.map(cluster -> webClient.get() .map(cluster -> webClient.get()
.uri(cluster.getSchemaRegistry() + URL_SUBJECT_BY_VERSION, schemaName, version) .uri(cluster.getSchemaRegistry() + URL_SUBJECT_BY_VERSION, schemaName, version)
.retrieve() .retrieve()
.onStatus(HttpStatus.NOT_FOUND::equals, .onStatus(NOT_FOUND::equals,
resp -> Mono.error( throwIfNotFoundStatus(formatted("No such schema %s with version %s", schemaName, version))
new NotFoundException(
formatted("No such schema %s with version %s", schemaName, version)
)
)
).bodyToMono(SchemaSubject.class) ).bodyToMono(SchemaSubject.class)
.zipWith(getSchemaCompatibilityInfoOrGlobal(clusterName, schemaName)) .zipWith(getSchemaCompatibilityInfoOrGlobal(clusterName, schemaName))
.map(tuple -> { .map(tuple -> {
@ -105,7 +100,7 @@ public class SchemaRegistryService {
return schema; return schema;
}) })
) )
.orElseThrow(); .orElse(Mono.error(new NotFoundException("No such cluster")));
} }
public Mono<ResponseEntity<Void>> deleteSchemaSubjectByVersion(String clusterName, String schemaName, Integer version) { public Mono<ResponseEntity<Void>> deleteSchemaSubjectByVersion(String clusterName, String schemaName, Integer version) {
@ -121,46 +116,40 @@ public class SchemaRegistryService {
.map(cluster -> webClient.delete() .map(cluster -> webClient.delete()
.uri(cluster.getSchemaRegistry() + URL_SUBJECT_BY_VERSION, schemaName, version) .uri(cluster.getSchemaRegistry() + URL_SUBJECT_BY_VERSION, schemaName, version)
.retrieve() .retrieve()
.onStatus(HttpStatus.NOT_FOUND::equals, .onStatus(NOT_FOUND::equals,
resp -> Mono.error( throwIfNotFoundStatus(formatted("No such schema %s with version %s", schemaName, version))
new NotFoundException(
formatted("No such schema %s with version %s", schemaName, version)
)
)
).toBodilessEntity() ).toBodilessEntity()
).orElse(Mono.error(new NotFoundException("No such cluster"))); ).orElse(Mono.error(new NotFoundException("No such cluster")));
} }
public Mono<ResponseEntity<Void>> deleteSchemaSubject(String clusterName, String schemaName) { public Mono<ResponseEntity<Void>> deleteSchemaSubjectEntirely(String clusterName, String schemaName) {
return clustersStorage.getClusterByName(clusterName) return clustersStorage.getClusterByName(clusterName)
.map(cluster -> webClient.delete() .map(cluster -> webClient.delete()
.uri(cluster.getSchemaRegistry() + URL_SUBJECT, schemaName) .uri(cluster.getSchemaRegistry() + URL_SUBJECT, schemaName)
.retrieve() .retrieve()
.onStatus(HttpStatus.NOT_FOUND::equals, .onStatus(NOT_FOUND::equals, throwIfNotFoundStatus(formatted("No such schema %s", schemaName))
resp -> Mono.error(
new NotFoundException(
formatted("No such schema %s", schemaName)
)
)
) )
.toBodilessEntity()) .toBodilessEntity())
.orElse(Mono.error(new NotFoundException("No such cluster"))); .orElse(Mono.error(new NotFoundException("No such cluster")));
} }
public Mono<ResponseEntity<SchemaSubject>> createNewSubject(String clusterName, String schemaName, Mono<NewSchemaSubject> newSchemaSubject) { public Mono<SchemaSubject> createNewSchema(String clusterName, String schemaName, Mono<NewSchemaSubject> newSchemaSubject) {
return clustersStorage.getClusterByName(clusterName) var response = clustersStorage.getClusterByName(clusterName)
.map(cluster -> webClient.post() .map(cluster -> webClient.post()
.uri(cluster.getSchemaRegistry() + URL_SUBJECT_VERSIONS, schemaName) .uri(cluster.getSchemaRegistry() + URL_SUBJECT_VERSIONS, schemaName)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromPublisher(newSchemaSubject, NewSchemaSubject.class)) .body(BodyInserters.fromPublisher(newSchemaSubject, NewSchemaSubject.class))
.retrieve() .retrieve()
.onStatus(HttpStatus.NOT_FOUND::equals, .onStatus(NOT_FOUND::equals, throwIfNotFoundStatus(formatted("No such schema %s", schemaName)))
resp -> Mono.error( .bodyToMono(SubjectIdResponse.class)
new NotFoundException(formatted("No such schema %s", schemaName)))
)
.toEntity(SchemaSubject.class)
.log()) .log())
.orElse(Mono.error(new NotFoundException("No such cluster"))); .orElse(Mono.error(new NotFoundException("No such cluster")));
return response.then(getLatestSchemaSubject(clusterName, schemaName));
}
@NotNull
private Function<ClientResponse, Mono<? extends Throwable>> throwIfNotFoundStatus(String formatted) {
return resp -> Mono.error(new NotFoundException(formatted));
} }
/** /**
@ -178,8 +167,8 @@ public class SchemaRegistryService {
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromPublisher(compatibilityLevel, CompatibilityLevel.class)) .body(BodyInserters.fromPublisher(compatibilityLevel, CompatibilityLevel.class))
.retrieve() .retrieve()
.onStatus(HttpStatus.NOT_FOUND::equals, .onStatus(NOT_FOUND::equals,
resp -> Mono.error(new NotFoundException(formatted("No such schema %s", schemaName)))) throwIfNotFoundStatus(formatted("No such schema %s", schemaName)))
.bodyToMono(Void.class); .bodyToMono(Void.class);
}).orElse(Mono.error(new NotFoundException("No such cluster"))); }).orElse(Mono.error(new NotFoundException("No such cluster")));
} }
@ -217,8 +206,7 @@ public class SchemaRegistryService {
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromPublisher(newSchemaSubject, NewSchemaSubject.class)) .body(BodyInserters.fromPublisher(newSchemaSubject, NewSchemaSubject.class))
.retrieve() .retrieve()
.onStatus(HttpStatus.NOT_FOUND::equals, .onStatus(NOT_FOUND::equals, throwIfNotFoundStatus(formatted("No such schema %s", schemaName)))
resp -> Mono.error(new NotFoundException(formatted("No such schema %s", schemaName))))
.bodyToMono(InternalCompatibilityCheck.class) .bodyToMono(InternalCompatibilityCheck.class)
.map(mapper::toCompatibilityCheckResponse) .map(mapper::toCompatibilityCheckResponse)
.log() .log()

View file

@ -138,14 +138,16 @@ public class MetricsRestController implements ApiClustersApi {
@Override @Override
public Mono<ResponseEntity<Void>> deleteSchema(String clusterName, String subjectName, ServerWebExchange exchange) { public Mono<ResponseEntity<Void>> deleteSchema(String clusterName, String subjectName, ServerWebExchange exchange) {
return schemaRegistryService.deleteSchemaSubject(clusterName, subjectName); return schemaRegistryService.deleteSchemaSubjectEntirely(clusterName, subjectName);
} }
@Override @Override
public Mono<ResponseEntity<SchemaSubject>> createNewSchema(String clusterName, String subject, public Mono<ResponseEntity<SchemaSubject>> createNewSchema(String clusterName, String subject,
@Valid Mono<NewSchemaSubject> newSchemaSubject, @Valid Mono<NewSchemaSubject> newSchemaSubject,
ServerWebExchange exchange) { ServerWebExchange exchange) {
return schemaRegistryService.createNewSubject(clusterName, subject, newSchemaSubject); return schemaRegistryService
.createNewSchema(clusterName, subject, newSchemaSubject)
.map(ResponseEntity::ok);
} }
@Override @Override

View file

@ -980,6 +980,10 @@ components:
type: string type: string
required: required:
- id - id
- subject
- version
- schema
- compatibilityLevel
NewSchemaSubject: NewSchemaSubject:
type: object type: object
@ -1005,14 +1009,6 @@ components:
required: required:
- compatibility - compatibility
# CompatibilityLevelResponse:
# type: object
# properties:
# compatibilityLevel:
# type: string
# required:
# - compatibilityLevel
CompatibilityCheckResponse: CompatibilityCheckResponse:
type: object type: object
properties: properties: