-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): ExecutionLabelsCondition
closes #2720
- Loading branch information
Showing
2 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
core/src/main/java/io/kestra/core/models/conditions/types/ExecutionLabelsCondition.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 io.kestra.core.models.conditions.types; | ||
|
||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
import io.kestra.core.exceptions.InternalException; | ||
import io.kestra.core.models.Label; | ||
import io.kestra.core.models.annotations.Example; | ||
import io.kestra.core.models.annotations.Plugin; | ||
import io.kestra.core.models.annotations.PluginProperty; | ||
import io.kestra.core.models.conditions.Condition; | ||
import io.kestra.core.models.conditions.ConditionContext; | ||
import io.kestra.core.serializers.ListOrMapOfLabelDeserializer; | ||
import io.kestra.core.serializers.ListOrMapOfLabelSerializer; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.ToString; | ||
import lombok.experimental.SuperBuilder; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@SuperBuilder | ||
@ToString | ||
@EqualsAndHashCode | ||
@Getter | ||
@NoArgsConstructor | ||
@Schema( | ||
title = "Condition for a specific flow. Note that this condition is deprecated, use `ExecutionFlowCondition` instead." | ||
) | ||
@Plugin( | ||
examples = { | ||
@Example( | ||
full = true, | ||
code = { | ||
"- conditions:", | ||
" - type: io.kestra.core.models.conditions.types.ExecutionLabelsCondition", | ||
" labels:", | ||
" owner: john.doe" | ||
} | ||
) | ||
} | ||
) | ||
public class ExecutionLabelsCondition extends Condition { | ||
|
||
@JsonSerialize(using = ListOrMapOfLabelSerializer.class) | ||
@JsonDeserialize(using = ListOrMapOfLabelDeserializer.class) | ||
@NotNull | ||
@Schema( | ||
description = "String against which to match the execution namespace depending on the provided comparison.", | ||
implementation = Object.class, anyOf = {List.class, Map.class} | ||
) | ||
@PluginProperty | ||
List<Label> labels; | ||
|
||
@Override | ||
public boolean test(ConditionContext conditionContext) throws InternalException { | ||
for (Label label : this.labels) { | ||
if (conditionContext.getExecution().getLabels() == null || !conditionContext.getExecution().getLabels().contains(label)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
core/src/test/java/io/kestra/core/models/conditions/types/ExecutionLabelsConditionTest.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,91 @@ | ||
package io.kestra.core.models.conditions.types; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import io.kestra.core.models.Label; | ||
import io.kestra.core.models.executions.Execution; | ||
import io.kestra.core.models.flows.Flow; | ||
import io.kestra.core.services.ConditionService; | ||
import io.kestra.core.utils.TestsUtils; | ||
import io.micronaut.test.extensions.junit5.annotation.MicronautTest; | ||
import jakarta.inject.Inject; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
@MicronautTest | ||
public class ExecutionLabelsConditionTest { | ||
@Inject | ||
ConditionService conditionService; | ||
|
||
@Test | ||
void valid() { | ||
Flow flow = TestsUtils.mockFlow(); | ||
Execution execution = TestsUtils | ||
.mockExecution(flow, ImmutableMap.of()) | ||
.toBuilder() | ||
.labels(List.of(new Label("key", "value"))) | ||
.build(); | ||
|
||
ExecutionLabelsCondition build = ExecutionLabelsCondition.builder() | ||
.labels(List.of(new Label("key", "value"))) | ||
.build(); | ||
|
||
boolean test = conditionService.isValid(build, flow, execution); | ||
|
||
assertThat(test, is(true)); | ||
} | ||
|
||
@Test | ||
void validMultiples() { | ||
Flow flow = TestsUtils.mockFlow(); | ||
Execution execution = TestsUtils | ||
.mockExecution(flow, ImmutableMap.of()) | ||
.toBuilder() | ||
.labels(List.of(new Label("key", "value"), new Label("key2", "value2"))) | ||
.build(); | ||
|
||
ExecutionLabelsCondition build = ExecutionLabelsCondition.builder() | ||
.labels(List.of(new Label("key", "value"), new Label("key2", "value2"))) | ||
.build(); | ||
|
||
boolean test = conditionService.isValid(build, flow, execution); | ||
|
||
assertThat(test, is(true)); | ||
} | ||
|
||
@Test | ||
void invalid() { | ||
Flow flow = TestsUtils.mockFlow(); | ||
Execution execution = TestsUtils | ||
.mockExecution(flow, ImmutableMap.of()); | ||
|
||
ExecutionLabelsCondition build = ExecutionLabelsCondition.builder() | ||
.labels(List.of(new Label("key", "value"))) | ||
.build(); | ||
|
||
boolean test = conditionService.isValid(build, flow, execution); | ||
|
||
assertThat(test, is(false)); | ||
} | ||
|
||
@Test | ||
void invalidMultiples() { | ||
Flow flow = TestsUtils.mockFlow(); | ||
Execution execution = TestsUtils | ||
.mockExecution(flow, ImmutableMap.of()) | ||
.toBuilder() | ||
.labels(List.of(new Label("key", "value"))) | ||
.build(); | ||
|
||
ExecutionLabelsCondition build = ExecutionLabelsCondition.builder() | ||
.labels(List.of(new Label("key", "value"), new Label("key2", "value2"))) | ||
.build(); | ||
|
||
boolean test = conditionService.isValid(build, flow, execution); | ||
|
||
assertThat(test, is(false)); | ||
} | ||
} |