Skip to content

Commit

Permalink
Add meta model, return rebalance conditions
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Edgar <[email protected]>
  • Loading branch information
MikeEdgar committed Sep 18, 2024
1 parent 1f11667 commit 7c798d4
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@

import org.eclipse.microprofile.openapi.annotations.enums.Explode;
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
import org.eclipse.microprofile.openapi.annotations.media.Content;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.eclipse.microprofile.openapi.annotations.parameters.Parameter;
import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponseSchema;
import org.eclipse.microprofile.openapi.annotations.tags.Tag;
Expand Down Expand Up @@ -61,7 +59,7 @@ public class KafkaRebalancesResource {

@GET
@Produces(MediaType.APPLICATION_JSON)
@APIResponseSchema(KafkaRebalance.ListResponse.class)
@APIResponseSchema(KafkaRebalance.RebalanceDataList.class)
@APIResponse(responseCode = "500", ref = "ServerError")
@APIResponse(responseCode = "504", ref = "ServerTimeout")
public Response listRebalances(
Expand Down Expand Up @@ -90,6 +88,7 @@ public Response listRebalances(
KafkaRebalance.Fields.REPLICATION_THROTTLE,
KafkaRebalance.Fields.REPLICA_MOVEMENT_STRATEGIES,
KafkaRebalance.Fields.OPTIMIZATION_RESULT,
KafkaRebalance.Fields.CONDITIONS,
},
payload = ErrorCategory.InvalidQueryParameter.class)
@Parameter(
Expand All @@ -115,6 +114,7 @@ public Response listRebalances(
KafkaRebalance.Fields.REPLICATION_THROTTLE,
KafkaRebalance.Fields.REPLICA_MOVEMENT_STRATEGIES,
KafkaRebalance.Fields.OPTIMIZATION_RESULT,
KafkaRebalance.Fields.CONDITIONS,
}))
List<String> fields,

Expand All @@ -136,7 +136,7 @@ public Response listRebalances(
KafkaRebalance::fromCursor);

var rebalanceList = rebalanceService.listRebalances(listSupport);
var responseEntity = new KafkaRebalance.ListResponse(rebalanceList, listSupport);
var responseEntity = new KafkaRebalance.RebalanceDataList(rebalanceList, listSupport);

return Response.ok(responseEntity).build();
}
Expand All @@ -145,7 +145,7 @@ public Response listRebalances(
@PATCH
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@APIResponseSchema(responseCode = "200", value = KafkaRebalance.Singleton.class)
@APIResponseSchema(responseCode = "200", value = KafkaRebalance.RebalanceData.class)
@Expression(
targetName = "args",
// Only check when the request body Id is present (separately checked for @NotNull)
Expand All @@ -166,10 +166,7 @@ public Response patchRebalance(
String rebalanceId,

@Valid
@RequestBody(content = @Content(
schema = @Schema(implementation = KafkaRebalance.Singleton.class))
)
KafkaRebalance.Singleton rebalance) {
KafkaRebalance.RebalanceData rebalance) {

requestedFields.accept(List.of(
KafkaRebalance.Fields.NAME,
Expand All @@ -190,7 +187,7 @@ public Response patchRebalance(
KafkaRebalance.Fields.OPTIMIZATION_RESULT));

var result = rebalanceService.patchRebalance(rebalanceId, rebalance.getData());
var responseEntity = new KafkaRebalance.Singleton(result);
var responseEntity = new KafkaRebalance.RebalanceData(result);

return Response.ok(responseEntity).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
@JsonInclude(value = Include.NON_NULL)
public class Error {

@Schema(description = "A meta object containing non-standard meta-information about the error")
Map<String, Object> meta;
@Schema(
description = "A meta object containing non-standard meta-information about the error",
implementation = JsonApiMeta.class)
JsonApiMeta meta;

@Schema(description = """
a links object that MAY contain the following members:
Expand Down Expand Up @@ -58,15 +60,12 @@ public Error(String title, String detail, Throwable cause) {
this.cause = cause;
}

public Map<String, Object> getMeta() {
public JsonApiMeta getMeta() {
return meta;
}

public Error addMeta(String key, Object value) {
if (meta == null) {
meta = new LinkedHashMap<>();
}
meta.put(key, value);
meta = JsonApiMeta.put(meta, key, value);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@JsonInclude(value = Include.NON_NULL)
public abstract class JsonApiDocument {

private Map<String, Object> meta;
private JsonApiMeta meta;
private Map<String, String> links;

static <K, V> Map<K, V> addEntry(Map<K, V> map, K key, V value) {
Expand All @@ -27,7 +27,7 @@ static <K, V> Map<K, V> addEntry(Map<K, V> map, K key, V value) {
}

@JsonProperty
public Map<String, Object> meta() {
public JsonApiMeta meta() {
return meta;
}

Expand All @@ -36,7 +36,7 @@ public Object meta(String key) {
}

public JsonApiDocument addMeta(String key, Object value) {
meta = addEntry(meta, key, value);
meta = JsonApiMeta.put(meta, key, value);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.github.streamshub.console.api.model;

import java.util.LinkedHashMap;
import java.util.Map;

import org.eclipse.microprofile.openapi.annotations.media.Schema;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;

@Schema(additionalProperties = Object.class)
public class JsonApiMeta {

public static JsonApiMeta put(JsonApiMeta meta, String key, Object value) {
if (meta == null) {
meta = new JsonApiMeta();
}
meta.put(key, value);
return meta;
}

@JsonIgnore
private Map<String, Object> meta;

@JsonAnyGetter
public Map<String, Object> get() {
return meta;
}

public Object get(String key) {
return meta != null ? meta.get(key) : null;
}

@JsonAnySetter
public JsonApiMeta put(String key, Object value) {
if (meta == null) {
meta = new LinkedHashMap<>();
}
meta.put(key, value);
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
import jakarta.json.JsonValue;

import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.eclipse.microprofile.openapi.annotations.media.SchemaProperty;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.github.streamshub.console.api.support.ComparatorBuilder;
import com.github.streamshub.console.api.support.ErrorCategory;
import com.github.streamshub.console.api.support.ListRequestContext;
Expand All @@ -28,7 +31,12 @@
import static java.util.Comparator.comparing;
import static java.util.Comparator.nullsLast;

@Schema(name = "KafkaRebalance")
@Schema(
name = "KafkaRebalance",
properties = {
@SchemaProperty(name = "type", enumeration = KafkaRebalance.TYPE),
@SchemaProperty(name = "meta", implementation = KafkaRebalance.Meta.class)
})
@Expression(
value = "self.id != null",
message = "resource ID is required",
Expand Down Expand Up @@ -62,6 +70,7 @@ public static class Fields {
public static final String REPLICATION_THROTTLE = "replicationThrottle";
public static final String REPLICA_MOVEMENT_STRATEGIES = "replicaMovementStrategies";
public static final String OPTIMIZATION_RESULT = "optimizationResult";
public static final String CONDITIONS = "conditions";

static final Comparator<KafkaRebalance> ID_COMPARATOR =
comparing(KafkaRebalance::getId, nullsLast(String::compareTo));
Expand Down Expand Up @@ -105,9 +114,9 @@ public static Comparator<KafkaRebalance> comparator(String fieldName, boolean de
}
}

@Schema(name = "KafkaRebalanceListResponse")
public static final class ListResponse extends DataList<KafkaRebalance> {
public ListResponse(List<KafkaRebalance> data, ListRequestContext<KafkaRebalance> listSupport) {
@Schema(name = "KafkaRebalanceDataList")
public static final class RebalanceDataList extends DataList<KafkaRebalance> {
public RebalanceDataList(List<KafkaRebalance> data, ListRequestContext<KafkaRebalance> listSupport) {
super(data.stream()
.map(entry -> {
entry.addMeta("page", listSupport.buildPageMeta(entry::toCursor));
Expand All @@ -119,15 +128,47 @@ public ListResponse(List<KafkaRebalance> data, ListRequestContext<KafkaRebalance
}
}

@Schema(name = "KafkaRebalanceSingleton")
public static final class Singleton extends DataSingleton<KafkaRebalance> {
@Schema(name = "KafkaRebalanceData")
public static final class RebalanceData extends DataSingleton<KafkaRebalance> {
@JsonCreator
public Singleton(@JsonProperty("data") KafkaRebalance data) {
public RebalanceData(@JsonProperty("data") KafkaRebalance data) {
super(data);
}
}

@Schema(name = "KafkaRebalanceMeta", additionalProperties = Object.class)
@JsonInclude(value = Include.NON_NULL)
static final class Meta extends JsonApiMeta {
@JsonProperty
@Schema(
description = """
Action to be taken against the Kafka Rebalance resource. \
Depends on the current resource state.
""",
enumeration = { "approve", "refresh", "stop" },
writeOnly = true,
nullable = true)
@StringEnumeration(
allowedValues = { "approve", "refresh", "stop" },
payload = ErrorCategory.InvalidResource.class,
message = "invalid rebalance action"
)
/**
* @see io.strimzi.api.kafka.model.rebalance.KafkaRebalanceAnnotation
*/
String action;

public String action() {
return action;
}

public void action(String action) {
this.action = action;
}
}

@JsonFilter("fieldFilter")
@Schema(name = "KafkaRebalanceAttributes")
static class Attributes {
@JsonProperty
@Schema(readOnly = true)
Expand Down Expand Up @@ -194,43 +235,34 @@ static class Attributes {
Map<String, Object> optimizationResult = new HashMap<>(0);

@JsonProperty
@Schema(writeOnly = true, nullable = true)
@StringEnumeration(
allowedValues = { "approve", "refresh", "stop" },
payload = ErrorCategory.InvalidResource.class,
message = "invalid rebalance action"
)
/**
* @see io.strimzi.api.kafka.model.rebalance.KafkaRebalanceAnnotation
*/
String action;
List<Condition> conditions;
}

public KafkaRebalance(String id) {
super(id, TYPE, new Attributes());
super(id, TYPE, Meta::new, new Attributes());
}

@JsonCreator
public KafkaRebalance(String id, String type, KafkaRebalance.Attributes attributes) {
super(id, type, attributes);
public KafkaRebalance(String id, String type, KafkaRebalance.Attributes attributes, Meta meta) {
super(id, type, meta, attributes);
}

/**
* Constructs a "cursor" Topic from the encoded string representation of the subset
* of Topic fields used to compare entities for pagination/sorting.
* Constructs a "cursor" KafkaRebalance from the encoded string representation of the subset
* of KafkaRebalance fields used to compare entities for pagination/sorting.
*/
public static KafkaRebalance fromCursor(JsonObject cursor) {
if (cursor == null) {
return null;
}

KafkaRebalance cluster = new KafkaRebalance(cursor.getString("id"));
KafkaRebalance rebalance = new KafkaRebalance(cursor.getString("id"));
JsonObject attr = cursor.getJsonObject("attributes");
cluster.name(attr.getString(Fields.NAME, null));
cluster.namespace(attr.getString(Fields.NAMESPACE, null));
cluster.creationTimestamp(attr.getString(Fields.CREATION_TIMESTAMP, null));
rebalance.name(attr.getString(Fields.NAME, null));
rebalance.namespace(attr.getString(Fields.NAMESPACE, null));
rebalance.creationTimestamp(attr.getString(Fields.CREATION_TIMESTAMP, null));

return cluster;
return rebalance;
}

public String toCursor(List<String> sortFields) {
Expand All @@ -252,6 +284,10 @@ static void maybeAddAttribute(JsonObjectBuilder attrBuilder, List<String> sortFi
}
}

public String action() {
return ((Meta) super.getMeta()).action();
}

public String name() {
return attributes.name;
}
Expand Down Expand Up @@ -380,11 +416,11 @@ public Map<String, Object> optimizationResult() {
return attributes.optimizationResult;
}

public String action() {
return attributes.action;
public List<Condition> conditions() {
return attributes.conditions;
}

public void action(String action) {
attributes.action = action;
public void conditions(List<Condition> conditions) {
attributes.conditions = conditions;
}
}
Loading

0 comments on commit 7c798d4

Please sign in to comment.