diff --git a/label-application-rule-config-service-impl/src/main/java/org/hypertrace/label/application/rule/config/service/LabelApplicationRuleConfig.java b/label-application-rule-config-service-impl/src/main/java/org/hypertrace/label/application/rule/config/service/LabelApplicationRuleConfig.java new file mode 100644 index 00000000..9851e4a2 --- /dev/null +++ b/label-application-rule-config-service-impl/src/main/java/org/hypertrace/label/application/rule/config/service/LabelApplicationRuleConfig.java @@ -0,0 +1,67 @@ +package org.hypertrace.label.application.rule.config.service; + +import static java.util.function.Function.identity; + +import com.google.protobuf.util.JsonFormat; +import com.typesafe.config.Config; +import com.typesafe.config.ConfigFactory; +import com.typesafe.config.ConfigObject; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import lombok.Getter; +import lombok.SneakyThrows; +import org.hypertrace.label.application.rule.config.service.v1.LabelApplicationRule; + +public class LabelApplicationRuleConfig { + private static final String LABEL_APPLICATION_RULE_CONFIG_SERVICE_CONFIG = + "label.application.rule.config.service"; + private static final String MAX_DYNAMIC_LABEL_APPLICATION_RULES_PER_TENANT = + "max.dynamic.label.application.rules.per.tenant"; + private static final String SYSTEM_LABEL_APPLICATION_RULES = "system.label.application.rules"; + private static final int DEFAULT_MAX_DYNAMIC_LABEL_APPLICATION_RULES_PER_TENANT = 100; + + @Getter private final int maxDynamicLabelApplicationRulesAllowed; + @Getter private final List systemLabelApplicationRules; + @Getter private final Map systemLabelApplicationRulesMap; + + public LabelApplicationRuleConfig(Config config) { + Config labelApplicationRuleConfig = + config.hasPath(LABEL_APPLICATION_RULE_CONFIG_SERVICE_CONFIG) + ? config.getConfig(LABEL_APPLICATION_RULE_CONFIG_SERVICE_CONFIG) + : ConfigFactory.empty(); + this.maxDynamicLabelApplicationRulesAllowed = + labelApplicationRuleConfig.hasPath(MAX_DYNAMIC_LABEL_APPLICATION_RULES_PER_TENANT) + ? labelApplicationRuleConfig.getInt(MAX_DYNAMIC_LABEL_APPLICATION_RULES_PER_TENANT) + : DEFAULT_MAX_DYNAMIC_LABEL_APPLICATION_RULES_PER_TENANT; + if (labelApplicationRuleConfig.hasPath(SYSTEM_LABEL_APPLICATION_RULES)) { + final List systemLabelApplicationRulesObjectList = + labelApplicationRuleConfig.getObjectList(SYSTEM_LABEL_APPLICATION_RULES); + this.systemLabelApplicationRules = + buildSystemLabelApplicationRuleList(systemLabelApplicationRulesObjectList); + this.systemLabelApplicationRulesMap = + this.systemLabelApplicationRules.stream() + .collect(Collectors.toUnmodifiableMap(LabelApplicationRule::getId, identity())); + } else { + this.systemLabelApplicationRules = Collections.emptyList(); + this.systemLabelApplicationRulesMap = Collections.emptyMap(); + } + } + + private List buildSystemLabelApplicationRuleList( + List configObjectList) { + return configObjectList.stream() + .map(LabelApplicationRuleConfig::buildLabelApplicationRuleFromConfig) + .collect(Collectors.toUnmodifiableList()); + } + + @SneakyThrows + private static LabelApplicationRule buildLabelApplicationRuleFromConfig( + com.typesafe.config.ConfigObject configObject) { + String jsonString = configObject.render(); + LabelApplicationRule.Builder builder = LabelApplicationRule.newBuilder(); + JsonFormat.parser().merge(jsonString, builder); + return builder.build(); + } +} diff --git a/label-application-rule-config-service-impl/src/main/java/org/hypertrace/label/application/rule/config/service/LabelApplicationRuleConfigServiceImpl.java b/label-application-rule-config-service-impl/src/main/java/org/hypertrace/label/application/rule/config/service/LabelApplicationRuleConfigServiceImpl.java index e5da9d7b..4420ade1 100644 --- a/label-application-rule-config-service-impl/src/main/java/org/hypertrace/label/application/rule/config/service/LabelApplicationRuleConfigServiceImpl.java +++ b/label-application-rule-config-service-impl/src/main/java/org/hypertrace/label/application/rule/config/service/LabelApplicationRuleConfigServiceImpl.java @@ -1,22 +1,15 @@ package org.hypertrace.label.application.rule.config.service; -import static java.util.function.Function.identity; - -import com.google.protobuf.util.JsonFormat; import com.typesafe.config.Config; -import com.typesafe.config.ConfigFactory; import io.grpc.Channel; import io.grpc.Status; import io.grpc.StatusRuntimeException; import io.grpc.stub.StreamObserver; -import java.util.Collections; import java.util.List; -import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.UUID; import java.util.stream.Collectors; -import lombok.SneakyThrows; import org.hypertrace.config.objectstore.ConfigObject; import org.hypertrace.config.objectstore.IdentifiedObjectStore; import org.hypertrace.config.service.change.event.api.ConfigChangeEventGenerator; @@ -37,41 +30,14 @@ public class LabelApplicationRuleConfigServiceImpl extends LabelApplicationRuleConfigServiceGrpc.LabelApplicationRuleConfigServiceImplBase { - private static final String LABEL_APPLICATION_RULE_CONFIG_SERVICE_CONFIG = - "label.application.rule.config.service"; - private static final String MAX_DYNAMIC_LABEL_APPLICATION_RULES_PER_TENANT = - "max.dynamic.label.application.rules.per.tenant"; - private static final String SYSTEM_LABEL_APPLICATION_RULES = "system.label.application.rules"; - private static final int DEFAULT_MAX_DYNAMIC_LABEL_APPLICATION_RULES_PER_TENANT = 100; - private final IdentifiedObjectStore labelApplicationRuleStore; private final LabelApplicationRuleValidator requestValidator; - private final int maxDynamicLabelApplicationRulesAllowed; - private final List systemLabelApplicationRules; - private final Map systemLabelApplicationRulesMap; + private final LabelApplicationRuleConfig labelApplicationRuleConfig; public LabelApplicationRuleConfigServiceImpl( Channel configChannel, Config config, ConfigChangeEventGenerator configChangeEventGenerator) { - Config labelApplicationRuleConfig = - config.hasPath(LABEL_APPLICATION_RULE_CONFIG_SERVICE_CONFIG) - ? config.getConfig(LABEL_APPLICATION_RULE_CONFIG_SERVICE_CONFIG) - : ConfigFactory.empty(); - this.maxDynamicLabelApplicationRulesAllowed = - labelApplicationRuleConfig.hasPath(MAX_DYNAMIC_LABEL_APPLICATION_RULES_PER_TENANT) - ? labelApplicationRuleConfig.getInt(MAX_DYNAMIC_LABEL_APPLICATION_RULES_PER_TENANT) - : DEFAULT_MAX_DYNAMIC_LABEL_APPLICATION_RULES_PER_TENANT; - if (labelApplicationRuleConfig.hasPath(SYSTEM_LABEL_APPLICATION_RULES)) { - final List systemLabelApplicationRulesObjectList = - labelApplicationRuleConfig.getObjectList(SYSTEM_LABEL_APPLICATION_RULES); - this.systemLabelApplicationRules = - buildSystemLabelApplicationRuleList(systemLabelApplicationRulesObjectList); - this.systemLabelApplicationRulesMap = - this.systemLabelApplicationRules.stream() - .collect(Collectors.toUnmodifiableMap(LabelApplicationRule::getId, identity())); - } else { - this.systemLabelApplicationRules = Collections.emptyList(); - this.systemLabelApplicationRulesMap = Collections.emptyMap(); - } + this.labelApplicationRuleConfig = new LabelApplicationRuleConfig(config); + final ConfigServiceBlockingStub configServiceBlockingStub = ConfigServiceGrpc.newBlockingStub(configChannel) .withCallCredentials( @@ -124,7 +90,7 @@ public void getLabelApplicationRules( .map(LabelApplicationRule::getId) .collect(Collectors.toUnmodifiableSet()); List filteredSystemLabelApplicationRules = - systemLabelApplicationRules.stream() + this.labelApplicationRuleConfig.getSystemLabelApplicationRules().stream() .filter(rule -> !labelApplicationRuleIds.contains(rule.getId())) .collect(Collectors.toUnmodifiableList()); responseObserver.onNext( @@ -148,7 +114,12 @@ public void updateLabelApplicationRule( LabelApplicationRule existingRule = this.labelApplicationRuleStore .getData(requestContext, request.getId()) - .or(() -> Optional.ofNullable(systemLabelApplicationRulesMap.get(request.getId()))) + .or( + () -> + Optional.ofNullable( + this.labelApplicationRuleConfig + .getSystemLabelApplicationRulesMap() + .get(request.getId()))) .orElseThrow(Status.NOT_FOUND::asRuntimeException); LabelApplicationRule updateLabelApplicationRule = existingRule.toBuilder().setData(request.getData()).build(); @@ -174,7 +145,9 @@ public void deleteLabelApplicationRule( RequestContext requestContext = RequestContext.CURRENT.get(); this.requestValidator.validateOrThrow(requestContext, request); String labelApplicationRuleId = request.getId(); - if (systemLabelApplicationRulesMap.containsKey(labelApplicationRuleId)) { + if (this.labelApplicationRuleConfig + .getSystemLabelApplicationRulesMap() + .containsKey(labelApplicationRuleId)) { // Deleting a system label application rule is not allowed responseObserver.onError(new StatusRuntimeException(Status.INVALID_ARGUMENT)); return; @@ -200,25 +173,10 @@ private void checkRequestForDynamicLabelsLimit( .filter( action -> action.hasDynamicLabelExpression() || action.hasDynamicLabelKey()) .count(); - if (dynamicLabelApplicationRules >= maxDynamicLabelApplicationRulesAllowed) { + if (dynamicLabelApplicationRules + >= this.labelApplicationRuleConfig.getMaxDynamicLabelApplicationRulesAllowed()) { throw Status.RESOURCE_EXHAUSTED.asRuntimeException(); } } } - - private List buildSystemLabelApplicationRuleList( - List configObjectList) { - return configObjectList.stream() - .map(LabelApplicationRuleConfigServiceImpl::buildLabelApplicationRuleFromConfig) - .collect(Collectors.toUnmodifiableList()); - } - - @SneakyThrows - private static LabelApplicationRule buildLabelApplicationRuleFromConfig( - com.typesafe.config.ConfigObject configObject) { - String jsonString = configObject.render(); - LabelApplicationRule.Builder builder = LabelApplicationRule.newBuilder(); - JsonFormat.parser().merge(jsonString, builder); - return builder.build(); - } }