Skip to content

Commit

Permalink
Add not exists, not equals and not matches regex operator support for…
Browse files Browse the repository at this point in the history
… label application rules (#196)
  • Loading branch information
sanket-mundra authored Jan 9, 2024
1 parent 95c6f4e commit 3c6d8a3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ message LabelApplicationRuleData {
OPERATOR_MATCHES_REGEX = 2; // operator to check if the key exists, and value matches the provided regex value
OPERATOR_MATCHES_IPS = 3; // operator to check if the key exists, and IP value matches provided IP(s), CIDR(s)
OPERATOR_NOT_MATCHES_IPS = 4; // operator to check if the key exists, and IP doesn't match provided IP(s), CIDR(s)
OPERATOR_NOT_EQUALS = 5; // operator to check if the key exists, and value is not equal to the provided value
OPERATOR_NOT_MATCHES_REGEX = 6; // operator to check if the key exists, and value not matches the provided regex value
}
}

Expand All @@ -70,6 +72,7 @@ message LabelApplicationRuleData {
enum Operator {
OPERATOR_UNSPECIFIED = 0;
OPERATOR_EXISTS = 1; // checks for existence of the key
OPERATOR_NOT_EXISTS = 2; // checks for non-existence of the key
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ private void validateStringCondition(StringCondition stringCondition) {
validateNonDefaultPresenceOrThrow(stringCondition, StringCondition.OPERATOR_FIELD_NUMBER);
switch (stringCondition.getOperator()) {
case OPERATOR_MATCHES_REGEX:
case OPERATOR_NOT_MATCHES_REGEX:
validateNonDefaultPresenceOrThrow(stringCondition, StringCondition.VALUE_FIELD_NUMBER);
final String pattern = stringCondition.getValue();
final Status regexValidationStatus = RegexValidator.validate(pattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public class LabelApplicationRuleValidatorTest {
private final StringCondition correctAuthKeyCondition;
private final StringCondition correctStringValueCondition;
private final StringCondition correctRegexStringValueCondition;
private final StringCondition correctRegexNotMatchingCondition;
private final StringCondition incorrectRegexStringValueCondition;
private final StringCondition incorrectRegexNotMatchingCondition;
private final StringCondition correctStringValueConditionWithMatchesIPs;
private final StringCondition incorrectStringValueConditionWithMatchesIPs;
private final UnaryCondition errorUnaryValueCondition;
Expand Down Expand Up @@ -69,11 +71,21 @@ public LabelApplicationRuleValidatorTest() {
.setOperator(StringCondition.Operator.OPERATOR_MATCHES_REGEX)
.setValue(".*bar.*")
.build();
correctRegexNotMatchingCondition =
StringCondition.newBuilder()
.setOperator(StringCondition.Operator.OPERATOR_NOT_MATCHES_REGEX)
.setValue(".*bar.*")
.build();
incorrectRegexStringValueCondition =
StringCondition.newBuilder()
.setOperator(StringCondition.Operator.OPERATOR_MATCHES_REGEX)
.setValue("((?!bar)")
.build();
incorrectRegexNotMatchingCondition =
StringCondition.newBuilder()
.setOperator(StringCondition.Operator.OPERATOR_NOT_MATCHES_REGEX)
.setValue("((?!bar)")
.build();
correctStringValueConditionWithMatchesIPs =
StringCondition.newBuilder()
.setOperator(StringCondition.Operator.OPERATOR_MATCHES_IPS)
Expand Down Expand Up @@ -341,6 +353,22 @@ void validateOrThrowCreateRuleLeafConditionWithMatchRegex() {
labelApplicationRuleValidator.validateOrThrow(REQUEST_CONTEXT, request);
}

@Test
void validateOrThrowCreateRuleLeafConditionWithNotMatchRegex() {
// This will check the condition that foo(key) = bar(value)
LeafCondition correctLeafCondition =
LeafCondition.newBuilder()
.setKeyCondition(correctKeyCondition)
.setStringCondition(correctRegexNotMatchingCondition)
.build();
Condition matchingCondition =
Condition.newBuilder().setLeafCondition(correctLeafCondition).build();
CreateLabelApplicationRuleRequest request =
buildCreateCreateLabelApplicationRuleRequest(
"Correct Leaf Rule", matchingCondition, Optional.empty());
labelApplicationRuleValidator.validateOrThrow(REQUEST_CONTEXT, request);
}

@Test
void validateOrThrowCreateRuleInvalidLeafConditionWithMatchRegex() {
// This will check the condition that foo(key) = bar(value)
Expand All @@ -362,6 +390,27 @@ void validateOrThrowCreateRuleInvalidLeafConditionWithMatchRegex() {
});
}

@Test
void validateOrThrowCreateRuleInvalidLeafConditionWithNotMatchRegex() {
// This will check the condition that foo(key) = bar(value)
LeafCondition invalidLeafCondition =
LeafCondition.newBuilder()
.setKeyCondition(correctKeyCondition)
.setStringCondition(incorrectRegexNotMatchingCondition)
.build();
Condition matchingCondition =
Condition.newBuilder().setLeafCondition(invalidLeafCondition).build();
CreateLabelApplicationRuleRequest request =
buildCreateCreateLabelApplicationRuleRequest(
"Incorrect Leaf Rule", matchingCondition, Optional.empty());

assertThrows(
StatusRuntimeException.class,
() -> {
labelApplicationRuleValidator.validateOrThrow(REQUEST_CONTEXT, request);
});
}

private CreateLabelApplicationRuleRequest buildCreateCreateLabelApplicationRuleRequest(
String name, Condition matchingCondition, Optional<String> labelExpression) {
return buildCreateCreateLabelApplicationRuleRequest(
Expand Down

0 comments on commit 3c6d8a3

Please sign in to comment.