Skip to content

Commit

Permalink
Add boolean at TopicOverview level to indicate if a schema can be cre…
Browse files Browse the repository at this point in the history
…ated directly on an environment or only promoted into that environment.

Signed-off-by: Aindriu Lavelle <aindriu.lavelle@aiven.io>
  • Loading branch information
aindriu-aiven committed Aug 11, 2023
1 parent 2259b0c commit 152aa65
Show file tree
Hide file tree
Showing 17 changed files with 66 additions and 37 deletions.
3 changes: 2 additions & 1 deletion coral/src/app/features/topics/details/TopicDetails.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const testTopicOverview: TopicOverview = {
schemaExists: false,
prefixAclsExists: false,
txnAclsExists: false,
createSchemaAllowed: false,
topicInfo: {
topicName: testTopicName,
noOfPartitions: 1,
Expand Down Expand Up @@ -148,6 +149,7 @@ const testTopicSchemas: TopicSchemaOverview = {
txnAclsExists: false,
allSchemaVersions: [1],
latestVersion: 1,
createSchemaAllowed: false,
schemaPromotionDetails: {
status: "SUCCESS",
sourceEnv: "3",
Expand All @@ -166,7 +168,6 @@ const testTopicSchemas: TopicSchemaOverview = {
showNext: false,
showPrev: false,
latest: true,
promoteOnly: false,
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const testTopicOverview: TopicOverview = {
schemaExists: false,
prefixAclsExists: false,
txnAclsExists: false,
createSchemaAllowed: false,
topicInfo: {
topicName: testTopicName,
noOfPartitions: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const testTopicOverview: TopicOverview = {
prefixAclsExists: false,
schemaExists: false,
txnAclsExists: false,
createSchemaAllowed: false,
topicPromotionDetails: { status: "NO_PROMOTION" },
topicIdForDocumentation: 99999,
topicInfo: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const testTopicOverview: TopicOverview = {
schemaExists: false,
prefixAclsExists: false,
txnAclsExists: false,
createSchemaAllowed: false,
topicInfo: {
noOfPartitions: 1,
noOfReplicas: "1",
Expand Down Expand Up @@ -72,6 +73,7 @@ const testTopicSchemas: TopicSchemaOverview = {
txnAclsExists: false,
allSchemaVersions: [1],
latestVersion: 1,
createSchemaAllowed: false,
schemaPromotionDetails: {
status: "SUCCESS",
sourceEnv: "3",
Expand All @@ -90,7 +92,6 @@ const testTopicSchemas: TopicSchemaOverview = {
showNext: false,
showPrev: false,
latest: true,
promoteOnly: false,
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const testTopicSchemas: TopicSchemaOverview = {
txnAclsExists: false,
topicExists: true,
schemaExists: true,
createSchemaAllowed: true,
allSchemaVersions: [3, 2, 1],
latestVersion: 3,
schemaPromotionDetails: {
Expand All @@ -45,7 +46,6 @@ const testTopicSchemas: TopicSchemaOverview = {
showNext: true,
showPrev: false,
latest: true,
promoteOnly: false,
},
};

Expand All @@ -54,6 +54,7 @@ const noPromotion_testTopicSchemas = {
schemaExists: true,
prefixAclsExists: false,
txnAclsExists: false,
createSchemaAllowed: false,
allSchemaVersions: [3, 2, 1],
latestVersion: 3,
schemaPromotionDetails: {
Expand All @@ -71,7 +72,6 @@ const noPromotion_testTopicSchemas = {
showNext: true,
showPrev: false,
latest: true,
promoteOnly: false,
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const testTopicOverview: TopicOverview = {
prefixAclsExists: false,
txnAclsExists: false,
schemaExists: false,
createSchemaAllowed: false,
topicInfo: testTopicInfo,
aclInfoList: [],
topicHistoryList: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const testTopicOverview: TopicOverview = {
schemaExists: false,
prefixAclsExists: false,
txnAclsExists: false,
createSchemaAllowed: false,
topicInfo: {
noOfPartitions: 1,
noOfReplicas: "1",
Expand Down
2 changes: 2 additions & 0 deletions coral/src/app/features/topics/details/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const mockUseTopicDetailsDataWithAcl: TopicOverview = {
schemaExists: false,
prefixAclsExists: false,
txnAclsExists: false,
createSchemaAllowed: false,
topicInfo: {
topicName: "aivendemotopic",
noOfPartitions: 1,
Expand Down Expand Up @@ -74,6 +75,7 @@ const mockUseTopicDetailsDataWithoutAcl: TopicOverview = {
schemaExists: false,
prefixAclsExists: false,
txnAclsExists: false,
createSchemaAllowed: false,
topicInfo: {
topicName: "aivendemotopic",
noOfPartitions: 1,
Expand Down
3 changes: 2 additions & 1 deletion coral/types/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,7 @@ export type components = {
schemaExists: boolean;
prefixAclsExists: boolean;
txnAclsExists: boolean;
createSchemaAllowed: boolean;
topicInfoList: (components["schemas"]["TopicOverviewInfo"])[];
aclInfoList?: (components["schemas"]["AclOverviewInfo"])[];
prefixedAclInfoList?: (components["schemas"]["AclOverviewInfo"])[];
Expand Down Expand Up @@ -1368,13 +1369,13 @@ export type components = {
showNext: boolean;
showPrev: boolean;
latest: boolean;
promoteOnly: boolean;
};
SchemaOverview: {
topicExists: boolean;
schemaExists: boolean;
prefixAclsExists: boolean;
txnAclsExists: boolean;
createSchemaAllowed: boolean;
allSchemaVersions?: (number)[];
/** Format: int32 */
latestVersion?: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ public class ResourceOverviewAttributes {
@NotNull boolean schemaExists;
@NotNull boolean prefixAclsExists;
@NotNull boolean txnAclsExists;
// Indicates if this schema env is restricted to only allow new schemas through promotion.
@NotNull private boolean createSchemaAllowed;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,4 @@ public class SchemaDetailsPerEnv {
@NotNull private boolean showPrev;
// simple boolean to identify if this is the latest schema on the topic for that environment.
@NotNull private boolean latest;
// Indicates if this schema env is restricted to only allow new schemas through promotion.
@NotNull private boolean promoteOnly;
}
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,14 @@ public boolean existsSchemaForTopic(String topicName, String topicEnvId, int ten
.isPresent();
}

public boolean isCreateNewSchemaAllowed(String schemaEnvId, int tenantId) {
KwTenantConfigModel tenantModel = manageDatabase.getTenantConfig().get(tenantId);
List<String> reqSchemaEnvs =
tenantModel == null ? new ArrayList<>() : tenantModel.getRequestSchemaEnvironmentsList();

return reqSchemaEnvs.contains(schemaEnvId);
}

public Env getEnvDetails(String envId, int tenantId) {
Optional<Env> envFound =
manageDatabase.getKafkaEnvList(tenantId).stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import io.aiven.klaw.dao.KwClusters;
import io.aiven.klaw.dao.MessageSchema;
import io.aiven.klaw.dao.Topic;
import io.aiven.klaw.model.KwTenantConfigModel;
import io.aiven.klaw.model.enums.KafkaClustersType;
import io.aiven.klaw.model.enums.PromotionStatusType;
import io.aiven.klaw.model.enums.RequestOperationType;
Expand Down Expand Up @@ -204,6 +203,8 @@ private void updateAvroSchema(
log.debug("SchemaDetails {}", schemaDetailsPerEnv);
schemaOverview.setSchemaDetailsPerEnv(schemaDetailsPerEnv);
}
schemaOverview.setCreateSchemaAllowed(
commonUtilsService.isCreateNewSchemaAllowed(schemaEnv.getId(), tenantId));
}
}

Expand All @@ -215,12 +216,6 @@ private void setSchemaDetailsPerEnvVersionAndCompatibility(
Env schemaEnv) {
updateIdAndCompatibility(schemaDetailsPerEnv, hashMapSchemaObj);
schemaDetailsPerEnv.setVersion(latestSchemaVersion);

KwTenantConfigModel tenantModel = manageDatabase.getTenantConfig().get(tenantId);
List<String> reqSchemaEnvs =
tenantModel == null ? new ArrayList<>() : tenantModel.getRequestSchemaEnvironmentsList();

schemaDetailsPerEnv.setPromoteOnly(!reqSchemaEnvs.contains(schemaEnv.getId()));
}

private static void updateIdAndCompatibility(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,12 @@ private void updateTopicOverviewItems(
topicOverview.setSchemaExists(
commonUtilsService.existsSchemaForTopic(topicNameSearch, environmentId, tenantId));
}
Env env = commonUtilsService.getEnvDetails(environmentId, tenantId);
if (env != null && env.getAssociatedEnv() != null) {
topicOverview.setCreateSchemaAllowed(
commonUtilsService.isCreateNewSchemaAllowed(
env.getAssociatedEnv().getId(), tenantId));
}
} else {
PromotionStatus promotionStatus = new PromotionStatus();
promotionStatus.setStatus(PromotionStatusType.NOT_AUTHORIZED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void getGivenASchemaWithManySchemaEnv_ReturnNextInPromotion() throws Exce
assertThat(returnedValue.getSchemaPromotionDetails()).isNotNull();
assertThat(returnedValue.getSchemaPromotionDetails().getStatus())
.isEqualTo(PromotionStatusType.SUCCESS);
assertThat(returnedValue.getSchemaDetailsPerEnv().isPromoteOnly()).isFalse();
assertThat(returnedValue.isCreateSchemaAllowed()).isTrue();
assertThat(returnedValue.getSchemaPromotionDetails().getSourceEnv()).isEqualTo("1");
assertThat(returnedValue.getSchemaPromotionDetails().getTargetEnv()).isEqualTo("test-2");
}
Expand Down Expand Up @@ -330,7 +330,7 @@ public void givenASchemaInHighestEnv_ReturnNoPromotion() throws Exception {
assertThat(returnedValue.getSchemaPromotionDetails()).isNotNull();
assertThat(returnedValue.getSchemaPromotionDetails().getStatus())
.isEqualTo(PromotionStatusType.NO_PROMOTION);
assertThat(returnedValue.getSchemaDetailsPerEnv().isPromoteOnly()).isTrue();
assertThat(returnedValue.isCreateSchemaAllowed()).isFalse();
}

private TreeMap<Integer, Map<String, Object>> getAvroSchemas(int numOfEntries) {
Expand Down Expand Up @@ -487,13 +487,8 @@ private void stubEnvironments() {
when(handleDbRequests.getEnvDetails("2", 101)).thenReturn(kafkaEnv2);
when(handleDbRequests.getEnvDetails("3", 101)).thenReturn(schemaEnv1);
when(handleDbRequests.getEnvDetails("4", 101)).thenReturn(schemaEnv2);
when(manageDatabase.getTenantConfig())
.thenReturn(
new HashMap<>() {
{
put(101, model);
}
});
when(commonUtilsService.isCreateNewSchemaAllowed(eq("3"), eq(101))).thenReturn(true);
when(commonUtilsService.isCreateNewSchemaAllowed(eq("4"), eq(101))).thenReturn(false);
}

private void stubKafkaPromotion(String testtopic, int numberOfEnvs) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import io.aiven.klaw.UtilMethods;
import io.aiven.klaw.config.ManageDatabase;
import io.aiven.klaw.dao.Acl;
import io.aiven.klaw.dao.Env;
import io.aiven.klaw.dao.KwClusters;
import io.aiven.klaw.dao.Topic;
import io.aiven.klaw.dao.UserInfo;
import io.aiven.klaw.dao.*;
import io.aiven.klaw.error.KlawException;
import io.aiven.klaw.helpers.db.rdbms.HandleDbRequestsJdbc;
import io.aiven.klaw.model.KwTenantConfigModel;
Expand Down Expand Up @@ -215,6 +211,7 @@ public void givenATopicWithOnlyOneKafkaEnv_ReturnNoPromotion() throws Exception
public void getGivenATopicWithManyKafkaEnv_ReturnNextInPromotion() throws Exception {
stubUserInfo();
stubKafkaPromotion(TESTTOPIC, 15);
stubEnvironment("1", "4");
stubSchemaPromotionInfo(TESTTOPIC, KafkaClustersType.KAFKA, 15);
when(commonUtilsService.getTopicsForTopicName(TESTTOPIC, 101))
.thenReturn(List.of(createTopic(TESTTOPIC)));
Expand Down Expand Up @@ -335,6 +332,7 @@ public void givenARequestWithAnOrderEnsureItIsCorrectlyUsed(AclGroupBy groupBy)
public void getTopicOverview() {
mockTenantConfig();
stubUserInfo();
stubEnvironment("1", "4");
when(commonUtilsService.getTenantId(any())).thenReturn(101);
when(commonUtilsService.getEnvsFromUserId(anyString()))
.thenReturn(new HashSet<>(Arrays.asList("1", "2", "3")));
Expand All @@ -349,6 +347,7 @@ public void getTopicOverview() {

when(manageDatabase.getAllEnvList(anyInt()))
.thenReturn(createListOfEnvs(KafkaClustersType.SCHEMA_REGISTRY, 5));
when(commonUtilsService.isCreateNewSchemaAllowed(eq("4"), eq(101))).thenReturn(true);

TopicOverview topicOverview =
topicOverviewService.getTopicOverview(TESTTOPIC, "1", AclGroupBy.NONE);
Expand All @@ -358,13 +357,15 @@ public void getTopicOverview() {
.isEqualTo(PromotionStatusType.NO_PROMOTION);
assertThat(topicOverview.getTopicInfoList().get(0).isTopicDeletable())
.isTrue(); // topic can be deleted

assertThat(topicOverview.isCreateSchemaAllowed()).isTrue();
when(commonUtilsService.getTopicsForTopicName(anyString(), anyInt()))
.thenReturn(utilMethods.getTopicInMultipleEnvs("testtopic", TEAMID, 2));
when(commonUtilsService.getFilteredTopicsForTenant(any()))
.thenReturn(utilMethods.getTopicInMultipleEnvs("testtopic", TEAMID, 2));

topicOverview = topicOverviewService.getTopicOverview(TESTTOPIC, "2", AclGroupBy.NONE);
stubEnvironment("1", "5");
when(commonUtilsService.isCreateNewSchemaAllowed(eq("5"), eq(101))).thenReturn(false);
assertThat(topicOverview.getAvailableEnvironments().size()).isEqualTo(2);
assertThat(topicOverview.getTopicInfoList().size()).isEqualTo(1);
assertThat(topicOverview.getTopicPromotionDetails().getStatus())
Expand Down Expand Up @@ -393,6 +394,7 @@ public void getTopicOverview() {
assertThat(topicOverview.getTopicInfoList().get(0).isHasOpenRequest())
.isFalse(); // topic hasAcl
assertThat(topicOverview.getTopicInfoList().get(0).isHasSchema()).isFalse(); // topic hasAcl
assertThat(topicOverview.isCreateSchemaAllowed()).isFalse();
}

@Test
Expand Down Expand Up @@ -980,4 +982,15 @@ private void stubUserInfo() {
when(mailService.getUserName(any())).thenReturn(TEAM_ID);
when(commonUtilsService.getTeamId(eq(TEAM_ID))).thenReturn(TEAMID);
}

private void stubEnvironment(String kafkaEnvId, String schemaEnvId) {
Env env = new Env();
EnvTag tag = new EnvTag();
tag.setId(schemaEnvId);
tag.setName("ENV");
env.setEnvExists("true");
env.setAssociatedEnv(tag);
env.setId(kafkaEnvId);
when(commonUtilsService.getEnvDetails(eq(kafkaEnvId), eq(101))).thenReturn(env);
}
}
19 changes: 11 additions & 8 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6976,6 +6976,9 @@
"txnAclsExists" : {
"type" : "boolean"
},
"createSchemaAllowed" : {
"type" : "boolean"
},
"topicInfoList" : {
"type" : "array",
"items" : {
Expand Down Expand Up @@ -7023,7 +7026,7 @@
"format" : "int32"
}
},
"required" : [ "availableEnvironments", "prefixAclsExists", "schemaExists", "topicExists", "topicIdForDocumentation", "topicInfoList", "topicPromotionDetails", "txnAclsExists" ]
"required" : [ "availableEnvironments", "createSchemaAllowed", "prefixAclsExists", "schemaExists", "topicExists", "topicIdForDocumentation", "topicInfoList", "topicPromotionDetails", "txnAclsExists" ]
},
"TopicOverviewInfo" : {
"properties" : {
Expand Down Expand Up @@ -7093,10 +7096,10 @@
"type" : "integer",
"format" : "int32"
},
"highestEnv" : {
"topicOwner" : {
"type" : "boolean"
},
"topicOwner" : {
"highestEnv" : {
"type" : "boolean"
}
},
Expand Down Expand Up @@ -7805,12 +7808,9 @@
},
"latest" : {
"type" : "boolean"
},
"promoteOnly" : {
"type" : "boolean"
}
},
"required" : [ "compatibility", "content", "env", "id", "latest", "nextVersion", "prevVersion", "promoteOnly", "showNext", "showPrev", "version" ]
"required" : [ "compatibility", "content", "env", "id", "latest", "nextVersion", "prevVersion", "showNext", "showPrev", "version" ]
},
"SchemaOverview" : {
"properties" : {
Expand All @@ -7826,6 +7826,9 @@
"txnAclsExists" : {
"type" : "boolean"
},
"createSchemaAllowed" : {
"type" : "boolean"
},
"allSchemaVersions" : {
"type" : "array",
"items" : {
Expand All @@ -7844,7 +7847,7 @@
"$ref" : "#/components/schemas/SchemaDetailsPerEnv"
}
},
"required" : [ "prefixAclsExists", "schemaExists", "schemaPromotionDetails", "topicExists", "txnAclsExists" ]
"required" : [ "createSchemaAllowed", "prefixAclsExists", "schemaExists", "schemaPromotionDetails", "topicExists", "txnAclsExists" ]
},
"KwReport" : {
"properties" : {
Expand Down

0 comments on commit 152aa65

Please sign in to comment.