-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: add support for system label application rules #235
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We generally allow users to delete default config. Why is this different?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be done. However deleting a default rule will require a new config store to persist it. Will add it if need arises.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will do it in separate PR