Skip to content

Commit 119d1b4

Browse files
Delete a specific version of a schema (#428)
1 parent b42607c commit 119d1b4

20 files changed

+576
-92
lines changed

src/main/java/com/michelin/ns4kafka/controller/ConnectorController.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static com.michelin.ns4kafka.util.FormatErrorUtils.invalidOwner;
44
import static com.michelin.ns4kafka.util.enumation.Kind.CONNECTOR;
5+
import static io.micronaut.core.util.StringUtils.EMPTY_STRING;
56

67
import com.michelin.ns4kafka.controller.generic.NamespacedResourceController;
78
import com.michelin.ns4kafka.model.Namespace;
@@ -11,6 +12,7 @@
1112
import com.michelin.ns4kafka.service.ResourceQuotaService;
1213
import com.michelin.ns4kafka.util.enumation.ApplyStatus;
1314
import com.michelin.ns4kafka.util.exception.ResourceValidationException;
15+
import io.micronaut.core.util.StringUtils;
1416
import io.micronaut.http.HttpResponse;
1517
import io.micronaut.http.HttpStatus;
1618
import io.micronaut.http.MutableHttpResponse;
@@ -143,8 +145,13 @@ public Mono<HttpResponse<Connector>> apply(String namespace, @Valid @Body Connec
143145
return Mono.just(formatHttpResponse(connector, status));
144146
}
145147

146-
sendEventLog(connector, status, existingConnector.<Object>map(Connector::getSpec).orElse(null),
147-
connector.getSpec());
148+
sendEventLog(
149+
connector,
150+
status,
151+
existingConnector.<Object>map(Connector::getSpec).orElse(null),
152+
connector.getSpec(),
153+
EMPTY_STRING
154+
);
148155

149156
return Mono.just(formatHttpResponse(connectorService.createOrUpdate(connector), status));
150157
});
@@ -180,7 +187,14 @@ public Mono<HttpResponse<Void>> delete(String namespace, String connector,
180187
}
181188

182189
Connector connectorToDelete = optionalConnector.get();
183-
sendEventLog(connectorToDelete, ApplyStatus.deleted, connectorToDelete.getSpec(), null);
190+
191+
sendEventLog(
192+
connectorToDelete,
193+
ApplyStatus.deleted,
194+
connectorToDelete.getSpec(),
195+
null,
196+
EMPTY_STRING
197+
);
184198

185199
return connectorService
186200
.delete(ns, optionalConnector.get())
@@ -264,7 +278,13 @@ public Flux<Connector> importResources(String namespace, @QueryValue(defaultValu
264278
return unsynchronizedConnector;
265279
}
266280

267-
sendEventLog(unsynchronizedConnector, ApplyStatus.created, null, unsynchronizedConnector.getSpec());
281+
sendEventLog(
282+
unsynchronizedConnector,
283+
ApplyStatus.created,
284+
null,
285+
unsynchronizedConnector.getSpec(),
286+
EMPTY_STRING
287+
);
268288

269289
return connectorService.createOrUpdate(unsynchronizedConnector);
270290
});

src/main/java/com/michelin/ns4kafka/controller/ConsumerGroupController.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static com.michelin.ns4kafka.util.FormatErrorUtils.invalidConsumerGroupOperation;
44
import static com.michelin.ns4kafka.util.FormatErrorUtils.invalidOwner;
55
import static com.michelin.ns4kafka.util.enumation.Kind.CONSUMER_GROUP_RESET_OFFSET;
6+
import static io.micronaut.core.util.StringUtils.EMPTY_STRING;
67

78
import com.michelin.ns4kafka.controller.generic.NamespacedResourceController;
89
import com.michelin.ns4kafka.model.Namespace;
@@ -86,7 +87,14 @@ public List<ConsumerGroupResetOffsetsResponse> resetOffsets(String namespace, St
8687
consumerGroupResetOffsets.getSpec().getMethod());
8788

8889
if (!dryrun) {
89-
sendEventLog(consumerGroupResetOffsets, ApplyStatus.changed, null, consumerGroupResetOffsets.getSpec());
90+
sendEventLog(
91+
consumerGroupResetOffsets,
92+
ApplyStatus.changed,
93+
null,
94+
consumerGroupResetOffsets.getSpec(),
95+
EMPTY_STRING
96+
);
97+
9098
consumerGroupService.alterConsumerGroupOffsets(ns, consumerGroup, preparedOffsets);
9199
}
92100

src/main/java/com/michelin/ns4kafka/controller/NamespaceController.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static com.michelin.ns4kafka.util.FormatErrorUtils.invalidImmutableValue;
44
import static com.michelin.ns4kafka.util.enumation.Kind.NAMESPACE;
5+
import static io.micronaut.core.util.StringUtils.EMPTY_STRING;
56

67
import com.michelin.ns4kafka.controller.generic.NonNamespacedResourceController;
78
import com.michelin.ns4kafka.model.Namespace;
@@ -102,8 +103,13 @@ public HttpResponse<Namespace> apply(@Valid @Body Namespace namespace,
102103
return formatHttpResponse(namespace, status);
103104
}
104105

105-
sendEventLog(namespace, status, existingNamespace.<Object>map(Namespace::getSpec).orElse(null),
106-
namespace.getSpec());
106+
sendEventLog(
107+
namespace,
108+
status,
109+
existingNamespace.<Object>map(Namespace::getSpec).orElse(null),
110+
namespace.getSpec(),
111+
EMPTY_STRING
112+
);
107113

108114
return formatHttpResponse(namespaceService.createOrUpdate(namespace), status);
109115
}
@@ -136,7 +142,15 @@ public HttpResponse<Void> delete(String namespace, @QueryValue(defaultValue = "f
136142
}
137143

138144
var namespaceToDelete = optionalNamespace.get();
139-
sendEventLog(namespaceToDelete, ApplyStatus.deleted, namespaceToDelete.getSpec(), null);
145+
146+
sendEventLog(
147+
namespaceToDelete,
148+
ApplyStatus.deleted,
149+
namespaceToDelete.getSpec(),
150+
null,
151+
EMPTY_STRING
152+
);
153+
140154
namespaceService.delete(optionalNamespace.get());
141155
return HttpResponse.noContent();
142156
}

src/main/java/com/michelin/ns4kafka/controller/RoleBindingController.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.michelin.ns4kafka.controller;
22

3+
import static io.micronaut.core.util.StringUtils.EMPTY_STRING;
4+
35
import com.michelin.ns4kafka.controller.generic.NamespacedResourceController;
46
import com.michelin.ns4kafka.model.Namespace;
57
import com.michelin.ns4kafka.model.RoleBinding;
@@ -88,8 +90,14 @@ public HttpResponse<RoleBinding> apply(String namespace, @Valid @Body RoleBindin
8890
return formatHttpResponse(roleBinding, status);
8991
}
9092

91-
sendEventLog(roleBinding, status, existingRoleBinding.<Object>map(RoleBinding::getSpec).orElse(null),
92-
roleBinding.getSpec());
93+
sendEventLog(
94+
roleBinding,
95+
status,
96+
existingRoleBinding.<Object>map(RoleBinding::getSpec).orElse(null),
97+
roleBinding.getSpec(),
98+
EMPTY_STRING
99+
);
100+
93101
roleBindingService.create(roleBinding);
94102
return formatHttpResponse(roleBinding, status);
95103
}
@@ -116,7 +124,15 @@ public HttpResponse<Void> delete(String namespace, String name,
116124
}
117125

118126
var roleBindingToDelete = roleBinding.get();
119-
sendEventLog(roleBindingToDelete, ApplyStatus.deleted, roleBindingToDelete.getSpec(), null);
127+
128+
sendEventLog(
129+
roleBindingToDelete,
130+
ApplyStatus.deleted,
131+
roleBindingToDelete.getSpec(),
132+
null,
133+
EMPTY_STRING
134+
);
135+
120136
roleBindingService.delete(roleBindingToDelete);
121137
return HttpResponse.noContent();
122138
}

src/main/java/com/michelin/ns4kafka/controller/SchemaController.java

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static com.michelin.ns4kafka.util.FormatErrorUtils.invalidOwner;
44
import static com.michelin.ns4kafka.util.enumation.Kind.SCHEMA;
5+
import static io.micronaut.core.util.StringUtils.EMPTY_STRING;
56

67
import com.michelin.ns4kafka.controller.generic.NamespacedResourceController;
78
import com.michelin.ns4kafka.model.Namespace;
@@ -72,7 +73,7 @@ public Mono<Schema> get(String namespace, String subject) {
7273
return Mono.empty();
7374
}
7475

75-
return schemaService.getLatestSubject(ns, subject);
76+
return schemaService.getSubjectLatestVersion(ns, subject);
7677
}
7778

7879
/**
@@ -128,11 +129,14 @@ public Mono<HttpResponse<Schema>> apply(String namespace, @Valid @Body Schema sc
128129
return schemaService
129130
.register(ns, schema)
130131
.map(id -> {
131-
sendEventLog(schema, status,
132+
sendEventLog(
133+
schema,
134+
status,
132135
oldSchemas.isEmpty() ? null : oldSchemas.stream()
133-
.max(Comparator.comparingInt(
134-
(Schema s) -> s.getSpec().getId())),
135-
schema.getSpec());
136+
.max(Comparator.comparingInt((Schema s) -> s.getSpec().getId())),
137+
schema.getSpec(),
138+
EMPTY_STRING
139+
);
136140

137141
return formatHttpResponse(schema, status);
138142
});
@@ -142,16 +146,19 @@ public Mono<HttpResponse<Schema>> apply(String namespace, @Valid @Body Schema sc
142146
}
143147

144148
/**
145-
* Delete all schemas under the given subject.
149+
* Delete all schema versions under the given subject, or a specific version of the schema if specified.
146150
*
147-
* @param namespace The current namespace
148-
* @param subject The current subject to delete
149-
* @param dryrun Run in dry mode or not
151+
* @param namespace The namespace
152+
* @param subject The subject
153+
* @param versionOptional The version of the schema to delete
154+
* @param dryrun Run in dry mode or not
150155
* @return A HTTP response
151156
*/
152157
@Status(HttpStatus.NO_CONTENT)
153158
@Delete("/{subject}")
154-
public Mono<HttpResponse<Void>> delete(String namespace, @PathVariable String subject,
159+
public Mono<HttpResponse<Void>> delete(String namespace,
160+
@PathVariable String subject,
161+
@QueryValue("version") Optional<String> versionOptional,
155162
@QueryValue(defaultValue = "false") boolean dryrun) {
156163
Namespace ns = getNamespace(namespace);
157164

@@ -160,24 +167,38 @@ public Mono<HttpResponse<Void>> delete(String namespace, @PathVariable String su
160167
return Mono.error(new ResourceValidationException(SCHEMA, subject, invalidOwner(subject)));
161168
}
162169

163-
return schemaService.getLatestSubject(ns, subject)
170+
return versionOptional
171+
// If version is specified, get the schema with the version
172+
.map(version -> schemaService.getSubjectByVersion(ns, subject, version))
173+
// If version is not specified, get the latest schema
174+
.orElseGet(() -> schemaService.getSubjectLatestVersion(ns, subject))
164175
.map(Optional::of)
165176
.defaultIfEmpty(Optional.empty())
166-
.flatMap(latestSubjectOptional -> {
167-
if (latestSubjectOptional.isEmpty()) {
177+
.flatMap(subjectOptional -> {
178+
if (subjectOptional.isEmpty()) {
168179
return Mono.just(HttpResponse.notFound());
169180
}
170181

171182
if (dryrun) {
172183
return Mono.just(HttpResponse.noContent());
173184
}
174185

175-
Schema schemaToDelete = latestSubjectOptional.get();
176-
sendEventLog(schemaToDelete, ApplyStatus.deleted, schemaToDelete.getSpec(), null);
186+
return (versionOptional.isEmpty()
187+
? schemaService.deleteAllVersions(ns, subject) :
188+
schemaService.deleteVersion(ns, subject, versionOptional.get()))
189+
.map(deletedVersionIds -> {
190+
Schema deletedSchema = subjectOptional.get();
177191

178-
return schemaService
179-
.delete(ns, subject)
180-
.map(deletedSchemaIds -> HttpResponse.noContent());
192+
sendEventLog(
193+
deletedSchema,
194+
ApplyStatus.deleted,
195+
deletedSchema.getSpec(),
196+
null,
197+
versionOptional.map(v -> String.valueOf(deletedVersionIds)).orElse(EMPTY_STRING)
198+
);
199+
200+
return HttpResponse.noContent();
201+
});
181202
});
182203
}
183204

@@ -198,7 +219,7 @@ public Mono<HttpResponse<SchemaCompatibilityState>> config(String namespace, @Pa
198219
return Mono.error(new ResourceValidationException(SCHEMA, subject, invalidOwner(subject)));
199220
}
200221

201-
return schemaService.getLatestSubject(ns, subject)
222+
return schemaService.getSubjectLatestVersion(ns, subject)
202223
.map(Optional::of)
203224
.defaultIfEmpty(Optional.empty())
204225
.flatMap(latestSubjectOptional -> {
@@ -220,8 +241,13 @@ public Mono<HttpResponse<SchemaCompatibilityState>> config(String namespace, @Pa
220241
return schemaService
221242
.updateSubjectCompatibility(ns, latestSubjectOptional.get(), compatibility)
222243
.map(schemaCompatibility -> {
223-
sendEventLog(latestSubjectOptional.get(), ApplyStatus.changed,
224-
latestSubjectOptional.get().getSpec().getCompatibility(), compatibility);
244+
sendEventLog(
245+
latestSubjectOptional.get(),
246+
ApplyStatus.changed,
247+
latestSubjectOptional.get().getSpec().getCompatibility(),
248+
compatibility,
249+
EMPTY_STRING
250+
);
225251

226252
return HttpResponse.ok(state);
227253
});

src/main/java/com/michelin/ns4kafka/controller/StreamController.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static com.michelin.ns4kafka.util.FormatErrorUtils.invalidOwner;
44
import static com.michelin.ns4kafka.util.enumation.Kind.KAFKA_STREAM;
5+
import static io.micronaut.core.util.StringUtils.EMPTY_STRING;
56

67
import com.michelin.ns4kafka.controller.generic.NamespacedResourceController;
78
import com.michelin.ns4kafka.model.KafkaStream;
@@ -93,8 +94,13 @@ HttpResponse<KafkaStream> apply(String namespace, @Body @Valid KafkaStream strea
9394
return formatHttpResponse(stream, status);
9495
}
9596

96-
sendEventLog(stream, status, existingStream.<Object>map(KafkaStream::getMetadata).orElse(null),
97-
stream.getMetadata());
97+
sendEventLog(
98+
stream,
99+
status,
100+
existingStream.<Object>map(KafkaStream::getMetadata).orElse(null),
101+
stream.getMetadata(),
102+
EMPTY_STRING
103+
);
98104

99105
return formatHttpResponse(streamService.create(stream), status);
100106
}
@@ -126,7 +132,15 @@ HttpResponse<Void> delete(String namespace, String stream, @QueryValue(defaultVa
126132
}
127133

128134
var streamToDelete = optionalStream.get();
129-
sendEventLog(streamToDelete, ApplyStatus.deleted, streamToDelete.getMetadata(), null);
135+
136+
sendEventLog(
137+
streamToDelete,
138+
ApplyStatus.deleted,
139+
streamToDelete.getMetadata(),
140+
null,
141+
EMPTY_STRING
142+
);
143+
130144
streamService.delete(ns, optionalStream.get());
131145
return HttpResponse.noContent();
132146
}

src/main/java/com/michelin/ns4kafka/controller/UserController.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static com.michelin.ns4kafka.util.FormatErrorUtils.invalidKafkaUser;
44
import static com.michelin.ns4kafka.util.enumation.Kind.KAFKA_USER_RESET_PASSWORD;
5+
import static io.micronaut.core.util.StringUtils.EMPTY_STRING;
56

67
import com.michelin.ns4kafka.controller.generic.NamespacedResourceController;
78
import com.michelin.ns4kafka.model.KafkaUserResetPassword;
@@ -61,7 +62,14 @@ public HttpResponse<KafkaUserResetPassword> resetPassword(String namespace, Stri
6162
.build())
6263
.build();
6364

64-
sendEventLog(response, ApplyStatus.changed, null, response.getSpec());
65+
sendEventLog(
66+
response,
67+
ApplyStatus.changed,
68+
null,
69+
response.getSpec(),
70+
EMPTY_STRING
71+
);
72+
6573
return HttpResponse.ok(response);
6674
}
6775
}

0 commit comments

Comments
 (0)