Skip to content

Commit

Permalink
feat(core): ExecutionLabelsCondition
Browse files Browse the repository at this point in the history
closes #2720
  • Loading branch information
Skraye committed Mar 14, 2024
1 parent 573d896 commit 163c9d5
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 0 deletions.
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;
}
}
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));
}
}

0 comments on commit 163c9d5

Please sign in to comment.