Skip to content

Commit 9e9f096

Browse files
committed
[strimzi#434] feat: edits for api standards
Signed-off-by: ilkerkocatepe <[email protected]>
1 parent 6fdaefb commit 9e9f096

File tree

7 files changed

+238
-128
lines changed

7 files changed

+238
-128
lines changed
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
d79befc630208ffc467595a5c413954da1cb03e6212b211257c6db29e5c2ad46
1+
e6ff5b267f241378e60e023ea1c57ccf90fd17170045b92e00e39d29bec17522

documentation/book/api/index.adoc

+55-33
Original file line numberDiff line numberDiff line change
@@ -2292,31 +2292,32 @@ endif::internal-generation[]
22922292
[.createTopic]
22932293
==== createTopic
22942294

2295-
`POST /admin/topics/{topicname}`
2295+
`POST /admin/topics`
22962296

22972297

22982298

22992299
===== Description
23002300

2301-
Creates a topic with given topic name.
2301+
Creates a topic with given name, partitions count and replication factor.
23022302

23032303

2304-
// markup not found, no include::{specDir}admin/topics/\{topicname\}/POST/spec.adoc[opts=optional]
2304+
// markup not found, no include::{specDir}admin/topics/POST/spec.adoc[opts=optional]
23052305

23062306

23072307

23082308
===== Parameters
23092309

23102310

2311+
23112312
[cols="2,3,1,1,1"]
2312-
.Path Parameters
2313+
.Body Parameter
23132314
|===
23142315
|Name| Description| Required| Default| Pattern
23152316

2316-
| topicname
2317-
| Name of the topic will be created.
2317+
| CreateTopic
2318+
| Creates a topic with given name, partitions count and replication factor. <<CreateTopic>>
23182319
| X
2319-
| null
2320+
|
23202321
|
23212322

23222323
|===
@@ -2325,26 +2326,6 @@ Creates a topic with given topic name.
23252326

23262327

23272328

2328-
[cols="2,3,1,1,1"]
2329-
.Query Parameters
2330-
|===
2331-
|Name| Description| Required| Default| Pattern
2332-
2333-
| partitions
2334-
| Number of partitions for the topic. (Optional)
2335-
| -
2336-
| null
2337-
|
2338-
2339-
| replication_factor
2340-
| Replication factor for the topic. (Optional)
2341-
| -
2342-
| null
2343-
|
2344-
2345-
|===
2346-
2347-
23482329
===== Return Type
23492330

23502331

@@ -2364,29 +2345,34 @@ Creates a topic with given topic name.
23642345
| Code | Message | Datatype
23652346

23662347

2367-
| 200
2368-
| No content
2348+
| 201
2349+
| Created
23692350
| <<>>
23702351

2352+
2353+
| 422
2354+
| Request body is null or it doesn&#39;t contain topic name.
2355+
| <<Error>>
2356+
23712357
|===
23722358

23732359
===== Samples
23742360

23752361

2376-
// markup not found, no include::{snippetDir}admin/topics/\{topicname\}/POST/http-request.adoc[opts=optional]
2362+
// markup not found, no include::{snippetDir}admin/topics/POST/http-request.adoc[opts=optional]
23772363

23782364

2379-
// markup not found, no include::{snippetDir}admin/topics/\{topicname\}/POST/http-response.adoc[opts=optional]
2365+
// markup not found, no include::{snippetDir}admin/topics/POST/http-response.adoc[opts=optional]
23802366

23812367

23822368

2383-
// file not found, no * wiremock data link :admin/topics/{topicname}/POST/POST.json[]
2369+
// file not found, no * wiremock data link :admin/topics/POST/POST.json[]
23842370

23852371

23862372
ifdef::internal-generation[]
23872373
===== Implementation
23882374

2389-
// markup not found, no include::{specDir}admin/topics/\{topicname\}/POST/implementation.adoc[opts=optional]
2375+
// markup not found, no include::{specDir}admin/topics/POST/implementation.adoc[opts=optional]
23902376

23912377

23922378
endif::internal-generation[]
@@ -3225,6 +3211,42 @@ Information about Kafka Bridge instance.
32253211

32263212

32273213

3214+
[#CreateTopic]
3215+
=== _CreateTopic_ CreateTopic
3216+
3217+
3218+
3219+
3220+
[.fields-CreateTopic]
3221+
[cols="2,1,1,2,4,1"]
3222+
|===
3223+
| Field Name| Required| Nullable | Type| Description | Format
3224+
3225+
| topic_name
3226+
|
3227+
|
3228+
| String
3229+
| Name of the topic to create.
3230+
|
3231+
3232+
| partitions_count
3233+
|
3234+
| X
3235+
| Integer
3236+
| Number of partitions for the topic.
3237+
|
3238+
3239+
| replication_factor
3240+
|
3241+
| X
3242+
| Integer
3243+
| Number of replicas for each partition.
3244+
|
3245+
3246+
|===
3247+
3248+
3249+
32283250
[#CreatedConsumer]
32293251
=== _CreatedConsumer_ CreatedConsumer
32303252

src/main/java/io/strimzi/kafka/bridge/http/HttpAdminBridgeEndpoint.java

+21-9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import io.strimzi.kafka.bridge.config.BridgeConfig;
1616
import io.strimzi.kafka.bridge.http.converter.JsonUtils;
1717
import io.strimzi.kafka.bridge.http.model.HttpBridgeError;
18+
import io.vertx.core.json.JsonObject;
1819
import io.vertx.ext.web.RoutingContext;
1920
import org.apache.kafka.clients.admin.Config;
2021
import org.apache.kafka.clients.admin.ConfigEntry;
@@ -179,23 +180,34 @@ public void doGetTopic(RoutingContext routingContext) {
179180
}
180181

181182
/**
182-
* Create a topic described in the HTTP request
183+
* Create a topic with described name, partitions count and replication factor in the body of the HTTP request
183184
*
184185
* @param routingContext the routing context
185186
*/
186187
public void doCreateTopic(RoutingContext routingContext) {
187-
String topicName = routingContext.pathParam("topicname");
188-
Optional<Integer> partitions = Optional.ofNullable(routingContext.queryParams().get("partitions"))
189-
.map(Integer::valueOf);
190-
Optional<Short> replicationFactor = Optional.ofNullable(routingContext.queryParams().get("replication_factor"))
191-
.map(Short::valueOf);
188+
JsonObject jsonBody = routingContext.body().asJsonObject();
189+
190+
if (jsonBody.isEmpty() || jsonBody.getString("topic_name").isBlank()) {
191+
HttpBridgeError error = new HttpBridgeError(
192+
HttpResponseStatus.UNPROCESSABLE_ENTITY.code(),
193+
"Request body must be a JSON object"
194+
);
195+
HttpUtils.sendResponse(routingContext, HttpResponseStatus.UNPROCESSABLE_ENTITY.code(),
196+
BridgeContentType.KAFKA_JSON, JsonUtils.jsonToBytes(error.toJson()));
197+
return;
198+
}
192199

193-
this.kafkaBridgeAdmin.createTopic(topicName, partitions, replicationFactor)
200+
String topicName = jsonBody.getString("topic_name");
201+
Optional<Integer> partitionsCount = Optional.ofNullable(jsonBody.getInteger("partitions_count"));
202+
Optional<Short> replicationFactor = Optional.ofNullable(jsonBody.getInteger("replication_factor"))
203+
.map(Integer::shortValue);
204+
205+
this.kafkaBridgeAdmin.createTopic(topicName, partitionsCount, replicationFactor)
194206
.whenComplete(((topic, exception) -> {
195207
LOGGER.trace("Create topic handler thread {}", Thread.currentThread());
196208
if (exception == null) {
197-
ArrayNode root = JsonUtils.createArrayNode();
198-
HttpUtils.sendResponse(routingContext, HttpResponseStatus.OK.code(),
209+
JsonNode root = JsonUtils.createObjectNode();
210+
HttpUtils.sendResponse(routingContext, HttpResponseStatus.CREATED.code(),
199211
BridgeContentType.KAFKA_JSON, JsonUtils.jsonToBytes(root));
200212
} else {
201213
HttpBridgeError error = new HttpBridgeError(

src/main/resources/openapi.json

+60-34
Original file line numberDiff line numberDiff line change
@@ -753,51 +753,51 @@
753753
}
754754
]
755755
},
756-
"/admin/topics/{topicname}": {
756+
"/admin/topics": {
757757
"post": {
758758
"tags": [
759759
"Topics"
760760
],
761-
"description": "Creates a topic with given topic name.",
761+
"description": "Creates a topic with given name, partitions count and replication factor.",
762762
"operationId": "createTopic",
763+
"requestBody": {
764+
"description": "Creates a topic with given name, partitions count and replication factor.",
765+
"content": {
766+
"application/vnd.kafka.json.v2+json": {
767+
"schema": {
768+
"$ref": "#/components/schemas/CreateTopic"
769+
}
770+
}
771+
},
772+
"required": true
773+
},
763774
"responses": {
764-
"200": {
765-
"description": "No content",
775+
"201": {
776+
"description": "Created",
766777
"content": {
767778
"application/vnd.kafka.v2+json": {}
768779
}
780+
},
781+
"422": {
782+
"description": "Request body is null or it doesn't contain topic name.",
783+
"content": {
784+
"application/vnd.kafka.v2+json": {
785+
"schema": {
786+
"$ref": "#/components/schemas/Error"
787+
},
788+
"examples": {
789+
"response": {
790+
"value": {
791+
"error_code": 422,
792+
"message": "Request body is null or it doesn't contain topic name."
793+
}
794+
}
795+
}
796+
}
797+
}
769798
}
770799
}
771-
},
772-
"parameters": [
773-
{
774-
"name": "topicname",
775-
"in": "path",
776-
"description": "Name of the topic will be created.",
777-
"required": true,
778-
"schema": {
779-
"type": "string"
780-
}
781-
},
782-
{
783-
"name": "partitions",
784-
"in": "query",
785-
"description": "Number of partitions for the topic. (Optional)",
786-
"required": false,
787-
"schema": {
788-
"type": "integer"
789-
}
790-
},
791-
{
792-
"name": "replication_factor",
793-
"in": "query",
794-
"description": "Replication factor for the topic. (Optional)",
795-
"required": false,
796-
"schema": {
797-
"type": "integer"
798-
}
799-
}
800-
]
800+
}
801801
},
802802
"/consumers/{groupid}/instances/{name}/records": {
803803
"get": {
@@ -2239,6 +2239,32 @@
22392239
}
22402240
],
22412241
"nullable" : true
2242+
},
2243+
"CreateTopic": {
2244+
"title": "CreateTopic",
2245+
"type": "object",
2246+
"properties": {
2247+
"topic_name": {
2248+
"description": "Name of the topic to create.",
2249+
"type": "string"
2250+
},
2251+
"partitions_count": {
2252+
"description": "Number of partitions for the topic.",
2253+
"type": "integer",
2254+
"nullable": true
2255+
},
2256+
"replication_factor": {
2257+
"description": "Number of replicas for each partition.",
2258+
"type": "integer",
2259+
"nullable": true
2260+
}
2261+
},
2262+
"additionalProperties": false,
2263+
"example": {
2264+
"topic_name": "my-topic",
2265+
"partitions": 3,
2266+
"replication_factor": 2
2267+
}
22422268
}
22432269
}
22442270
},

0 commit comments

Comments
 (0)