Skip to content

Commit

Permalink
refactor: dynamic properties (#54)
Browse files Browse the repository at this point in the history
* refactor: dynamic properties

* refactor: dynamic properties
  • Loading branch information
mgabelle authored Dec 9, 2024
1 parent e8e9303 commit 22f6828
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
15 changes: 6 additions & 9 deletions src/main/java/io/kestra/plugin/soda/AbstractSoda.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import io.kestra.core.exceptions.IllegalVariableEvaluationException;
import io.kestra.core.models.annotations.PluginProperty;
import io.kestra.core.models.property.Property;
import io.kestra.core.models.tasks.Task;
import io.kestra.core.models.tasks.runners.PluginUtilsService;
import io.kestra.core.models.tasks.runners.ScriptService;
Expand Down Expand Up @@ -98,18 +99,13 @@ public void setDockerOptions(DockerOptions dockerOptions) {
title = "List of python dependencies to add to the python execution process",
description = "Python dependencies list to setup in the virtualenv, in the same format than requirements.txt. It must at least provides dbt."
)
@PluginProperty(dynamic = true)
@NotNull
protected List<String> requirements;
protected Property<List<String>> requirements;

@Schema(
title = "Additional environment variables for the current process."
)
@PluginProperty(
additionalProperties = String.class,
dynamic = true
)
protected Map<String, String> env;
protected Property<Map<String, String>> env;

@Schema(
title = "The configuration file"
Expand All @@ -128,8 +124,9 @@ protected Map<String, String> finalInputFiles(RunContext runContext, Path workin
}

public CommandsWrapper start(RunContext runContext) throws Exception {
var env = runContext.render(this.getEnv()).asMap(String.class, String.class);
CommandsWrapper commandsWrapper = new CommandsWrapper(runContext)
.withEnv(this.getEnv())
.withEnv(env.isEmpty() ? new HashMap<>() : env)
.withRunnerType(this.getRunner())
.withTaskRunner(this.taskRunner)
.withContainerImage(this.getContainerImage())
Expand All @@ -139,7 +136,7 @@ public CommandsWrapper start(RunContext runContext) throws Exception {

List<String> commands = new ArrayList<>();
if (this.requirements != null) {
commands.add(this.virtualEnvCommand(runContext, workingDirectory, this.requirements));
commands.add(this.virtualEnvCommand(runContext, workingDirectory, runContext.render(this.requirements).asList(String.class)));
commands.add("./bin/python {{workingDir}}/main.py");
} else {
commands.add("python {{workingDir}}/main.py");
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/io/kestra/plugin/soda/Scan.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.kestra.core.models.annotations.PluginProperty;
import io.kestra.core.models.executions.metrics.Counter;
import io.kestra.core.models.flows.State;
import io.kestra.core.models.property.Property;
import io.kestra.core.models.tasks.RunnableTask;
import io.kestra.core.runners.RunContext;
import io.kestra.core.serializers.JacksonMapper;
Expand Down Expand Up @@ -39,7 +40,7 @@
code = """
id: soda_scan
namespacae: company.team
tasks:
- id: scan
type: io.kestra.plugin.soda.Scan
Expand Down Expand Up @@ -80,15 +81,13 @@ public class Scan extends AbstractSoda implements RunnableTask<Scan.Output> {
@Schema(
title = "The variables to pass"
)
@PluginProperty(dynamic = true)
Map<String, Object> variables;
Property<Map<String, Object>> variables;

@Schema(
title = "Whether to enable verbose logging"
)
@PluginProperty
@Builder.Default
Boolean verbose = false;
Property<Boolean> verbose = Property.of(false);

@Override
protected Map<String, String> finalInputFiles(RunContext runContext, Path workingDirectory) throws IOException, IllegalVariableEvaluationException {
Expand All @@ -111,12 +110,12 @@ protected Map<String, String> finalInputFiles(RunContext runContext, Path workin
"scan.add_sodacl_yaml_file(\"{{workingDir}}/checks.yml\")\n" +
"\n";

if (verbose) {
if (runContext.render(verbose).as(Boolean.class).orElseThrow()) {
main += "scan.set_verbose()";
}

if (variables != null) {
main += "scan.add_variables(" + JacksonMapper.ofJson().writeValueAsString(runContext.render(variables)) + ")";
main += "scan.add_variables(" + JacksonMapper.ofJson().writeValueAsString(runContext.render(variables).asMap(String.class, Object.class)) + ")";
}

main += "\n" +
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/io/kestra/plugin/soda/ScanTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.collect.ImmutableMap;
import io.kestra.core.models.flows.State;
import io.kestra.core.models.property.Property;
import io.kestra.core.runners.RunContext;
import io.kestra.core.runners.RunContextFactory;
import io.kestra.core.serializers.JacksonMapper;
Expand Down Expand Up @@ -118,7 +119,7 @@ void error() throws Exception {
" - row_count > 0",
TYPE_REFERENCE
))
.requirements(List.of("soda-core-bigquery"))
.requirements(Property.of(List.of("soda-core-bigquery")))
.build();

RunContext runContext = TestsUtils.mockRunContext(runContextFactory, task, ImmutableMap.of());
Expand Down

0 comments on commit 22f6828

Please sign in to comment.