-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add support for system label application rules (#235)
- Loading branch information
1 parent
9519fa1
commit 834e663
Showing
6 changed files
with
209 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...java/org/hypertrace/label/application/rule/config/service/LabelApplicationRuleConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<LabelApplicationRule> systemLabelApplicationRules; | ||
@Getter private final Map<String, LabelApplicationRule> 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<? extends ConfigObject> 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<LabelApplicationRule> buildSystemLabelApplicationRuleList( | ||
List<? extends com.typesafe.config.ConfigObject> 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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
.../org/hypertrace/label/application/rule/config/service/LabelApplicationRuleConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.hypertrace.label.application.rule.config.service; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import com.google.protobuf.InvalidProtocolBufferException; | ||
import com.google.protobuf.util.JsonFormat; | ||
import com.typesafe.config.Config; | ||
import com.typesafe.config.ConfigFactory; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.hypertrace.label.application.rule.config.service.v1.LabelApplicationRule; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class LabelApplicationRuleConfigTest { | ||
private static final String SYSTEM_LABEL_APPLICATION_RULE_STR = | ||
"{\"id\":\"system-label-application-rule-1\",\"data\":{\"name\":\"SystemLabelApplicationRule1\",\"matching_condition\":{\"leaf_condition\":{\"key_condition\":{\"operator\":\"OPERATOR_EQUALS\",\"value\":\"test.key\"},\"unary_condition\":{\"operator\":\"OPERATOR_EXISTS\"}}},\"label_action\":{\"entity_types\":[\"API\"],\"operation\":\"OPERATION_MERGE\",\"static_labels\":{\"ids\":[\"static-label-id-1\"]}},\"enabled\":false}}"; | ||
LabelApplicationRule systemLabelApplicationRule; | ||
Map<String, LabelApplicationRule> stringLabelApplicationRuleMap; | ||
LabelApplicationRuleConfig labelApplicationRuleConfig; | ||
|
||
@BeforeEach | ||
void setUp() throws InvalidProtocolBufferException { | ||
String configStr = | ||
"label.application.rule.config.service {\n" | ||
+ "max.dynamic.label.application.rules.per.tenant = 5\n" | ||
+ "system.label.application.rules = [\n" | ||
+ SYSTEM_LABEL_APPLICATION_RULE_STR | ||
+ "\n]\n" | ||
+ "}\n"; | ||
Config config = ConfigFactory.parseString(configStr); | ||
LabelApplicationRule.Builder builder = LabelApplicationRule.newBuilder().clear(); | ||
JsonFormat.parser().merge(SYSTEM_LABEL_APPLICATION_RULE_STR, builder); | ||
systemLabelApplicationRule = builder.build(); | ||
stringLabelApplicationRuleMap = new HashMap<>(); | ||
stringLabelApplicationRuleMap.put( | ||
systemLabelApplicationRule.getId(), systemLabelApplicationRule); | ||
labelApplicationRuleConfig = new LabelApplicationRuleConfig(config); | ||
} | ||
|
||
@Test | ||
void test_getMaxDynamicLabelApplicationRulesAllowed() { | ||
assertEquals(5, labelApplicationRuleConfig.getMaxDynamicLabelApplicationRulesAllowed()); | ||
} | ||
|
||
@Test | ||
void test_getSystemLabelApplicationRules() { | ||
assertEquals( | ||
List.of(systemLabelApplicationRule), | ||
labelApplicationRuleConfig.getSystemLabelApplicationRules()); | ||
} | ||
|
||
@Test | ||
void test_getSystemLabelApplicationRulesMap() { | ||
assertEquals( | ||
stringLabelApplicationRuleMap, | ||
labelApplicationRuleConfig.getSystemLabelApplicationRulesMap()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters