Skip to content

Commit

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

migrate base interfaces

* refactor: migrate Upload

* refactor: migrate Delete and Copy task

* refactor: migrate CreateBucket

* refactor: migrate others
  • Loading branch information
mgabelle authored Dec 11, 2024
1 parent e84802c commit cf5d9f2
Show file tree
Hide file tree
Showing 21 changed files with 218 additions and 261 deletions.
4 changes: 2 additions & 2 deletions src/main/java/io/kestra/plugin/minio/AbstractMinioObject.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.kestra.plugin.minio;

import io.kestra.core.models.annotations.PluginProperty;
import io.kestra.core.models.property.Property;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.EqualsAndHashCode;
import lombok.Getter;
Expand All @@ -18,7 +19,6 @@ public abstract class AbstractMinioObject extends MinioConnection implements Abs
@Schema(
title = "The bucket name."
)
@PluginProperty(dynamic = true)
protected String bucket;
protected Property<String> bucket;

}
25 changes: 11 additions & 14 deletions src/main/java/io/kestra/plugin/minio/Copy.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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.property.Property;
import io.kestra.core.models.tasks.RunnableTask;
import io.kestra.core.runners.RunContext;
import io.kestra.plugin.minio.model.ObjectOutput;
Expand Down Expand Up @@ -84,31 +85,30 @@ public class Copy extends AbstractMinioObject implements RunnableTask<Copy.Outpu
@Schema(
title = "Whether to delete the source file after download."
)
@PluginProperty
@Builder.Default
private Boolean delete = false;
private Property<Boolean> delete = Property.of(false);

@Override
public Output run(RunContext runContext) throws Exception {
try (MinioClient minioClient = this.client(runContext)) {
CopySource.Builder sourceBuilder = CopySource.builder()
.bucket(runContext.render(this.from.bucket))
.object(runContext.render(this.from.key));
.bucket(runContext.render(this.from.bucket).as(String.class).orElse(null))
.object(runContext.render(this.from.key).as(String.class).orElse(null));

if (this.from.versionId != null) {
sourceBuilder.versionId(runContext.render(this.from.versionId));
sourceBuilder.versionId(runContext.render(this.from.versionId).as(String.class).orElseThrow());
}

CopyObjectArgs.Builder builder = CopyObjectArgs.builder()
.bucket(runContext.render(this.to.bucket != null ? this.to.bucket : this.from.bucket))
.object(runContext.render(this.to.key))
.bucket(runContext.render(this.to.bucket != null ? this.to.bucket : this.from.bucket).as(String.class).orElseThrow())
.object(runContext.render(this.to.key).as(String.class).orElse(null))
.source(sourceBuilder.build());

CopyObjectArgs request = builder.build();

ObjectWriteResponse response = minioClient.copyObject(request);

if (this.delete) {
if (runContext.render(this.delete).as(Boolean.class).orElseThrow()) {
Delete.builder()
.id(this.id)
.type(Delete.class.getName())
Expand Down Expand Up @@ -138,14 +138,12 @@ public static class CopyObject {
@Schema(
title = "The bucket name"
)
@PluginProperty(dynamic = true)
String bucket;
Property<String> bucket;

@Schema(
title = "The bucket key"
)
@PluginProperty(dynamic = true)
String key;
Property<String> key;
}

@SuperBuilder(toBuilder = true)
Expand All @@ -155,8 +153,7 @@ public static class CopyObjectFrom extends CopyObject {
@Schema(
title = "The specific version of the object."
)
@PluginProperty(dynamic = true)
private String versionId;
private Property<String> versionId;
}

@SuperBuilder
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/io/kestra/plugin/minio/CreateBucket.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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.property.Property;
import io.kestra.core.models.tasks.RunnableTask;
import io.kestra.core.runners.RunContext;
import io.minio.BucketExistsArgs;
Expand Down Expand Up @@ -61,12 +62,11 @@ public class CreateBucket extends AbstractMinioObject implements RunnableTask<Cr
@Schema(
title = "Specifies whether you want Object Lock to be enabled for the new bucket."
)
@PluginProperty
private Boolean objectLockEnabledForBucket;
private Property<Boolean> objectLockEnabledForBucket;

@Override
public Output run(RunContext runContext) throws Exception {
String bucket = runContext.render(this.bucket);
String bucket = runContext.render(this.bucket).as(String.class).orElse(null);

try (MinioClient client = this.client(runContext)) {

Expand All @@ -81,7 +81,7 @@ public Output run(RunContext runContext) throws Exception {
MakeBucketArgs.Builder requestBuilder = MakeBucketArgs.builder().bucket(bucket);

if (this.objectLockEnabledForBucket != null) {
requestBuilder.objectLock(objectLockEnabledForBucket);
requestBuilder.objectLock(runContext.render(objectLockEnabledForBucket).as(Boolean.class).orElseThrow());
}

client.makeBucket(requestBuilder.build());
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/io/kestra/plugin/minio/Delete.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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.property.Property;
import io.kestra.core.models.tasks.RunnableTask;
import io.kestra.core.runners.RunContext;
import io.minio.MinioClient;
Expand All @@ -23,7 +24,7 @@
code = """
id: minio_delete
namespace: company.team
tasks:
- id: delete
type: io.kestra.plugin.minio.Delete
Expand Down Expand Up @@ -61,27 +62,26 @@ public class Delete extends AbstractMinioObject implements RunnableTask<Delete.O
@Schema(
title = "The key to delete."
)
@PluginProperty(dynamic = true)
private String key;
private Property<String> key;

@Schema(
title = "Indicates whether Object Lock should bypass Governance-mode restrictions to process this operation."
)
@PluginProperty
private Boolean bypassGovernanceRetention;
private Property<Boolean> bypassGovernanceRetention;

@Override
public Output run(RunContext runContext) throws Exception {
String bucket = runContext.render(this.bucket);
String key = runContext.render(this.key);
String bucket = runContext.render(this.bucket).as(String.class).orElse(null);
String key = runContext.render(this.key).as(String.class).orElse(null);

try (MinioClient minioClient = this.client(runContext)) {
RemoveObjectArgs.Builder builder = RemoveObjectArgs.builder()
.bucket(bucket)
.object(key);

if (this.bypassGovernanceRetention != null) {
builder.bypassGovernanceMode(this.bypassGovernanceRetention);
builder.bypassGovernanceMode(runContext.render(this.bypassGovernanceRetention).as(Boolean.class).orElseThrow());
}

RemoveObjectArgs request = builder.build();
Expand Down
30 changes: 12 additions & 18 deletions src/main/java/io/kestra/plugin/minio/DeleteList.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.kestra.core.models.annotations.Plugin;
import io.kestra.core.models.annotations.PluginProperty;
import io.kestra.core.models.executions.metrics.Counter;
import io.kestra.core.models.property.Property;
import io.kestra.core.models.tasks.RunnableTask;
import io.kestra.core.runners.RunContext;
import io.kestra.plugin.minio.model.MinioObject;
Expand Down Expand Up @@ -36,7 +37,7 @@
code = """
id: minio_delete_objects
namespace: company.team
tasks:
- id: delete_objects
type: io.kestra.plugin.minio.DeleteList
Expand Down Expand Up @@ -74,45 +75,39 @@ public class DeleteList extends AbstractMinioObject implements RunnableTask<Dele
@Schema(
title = "Limits the response to keys that begin with the specified prefix."
)
@PluginProperty(dynamic = true)
private String prefix;
private Property<String> prefix;

@Schema(
title = "A delimiter is a character you use to group keys."
)
@PluginProperty(dynamic = true)
private String delimiter;
private Property<String> delimiter;

@Schema(
title = "Marker is where you want to start listing from.",
description = "Start listing after this specified key. Marker can be any key in the bucket."
)
@PluginProperty(dynamic = true)
private String marker;
private Property<String> marker;

@Schema(
title = "Sets the maximum number of keys returned in the response.",
description = "By default, the action returns up to 1,000 key names. The response might contain fewer keys but will never contain more."
)
@PluginProperty(dynamic = true)
@Builder.Default
private Integer maxKeys = 1000;
private Property<Integer> maxKeys = Property.of(1000);

@Schema(
title = "A regexp to filter on full key.",
description = "ex:\n"+
"`regExp: .*` to match all files\n"+
"`regExp: .*2020-01-0.\\\\.csv` to match files between 01 and 09 of january ending with `.csv`"
)
@PluginProperty(dynamic = true)
protected String regexp;
protected Property<String> regexp;

@Schema(
title = "The type of objects to filter: files, directory, or both."
)
@PluginProperty
@Builder.Default
protected final List.Filter filter = List.Filter.BOTH;
protected final Property<List.Filter> filter = Property.of(List.Filter.BOTH);

@Min(2)
@Schema(
Expand All @@ -124,14 +119,13 @@ public class DeleteList extends AbstractMinioObject implements RunnableTask<Dele
@Schema(
title = "raise an error if the file is not found"
)
@PluginProperty
@Builder.Default
private final Boolean errorOnEmpty = false;
private final Property<Boolean> errorOnEmpty = Property.of(false);

@Override
public Output run(RunContext runContext) throws Exception {
Logger logger = runContext.logger();
String bucket = runContext.render(this.bucket);
String bucket = runContext.render(this.bucket).as(String.class).orElse(null);

try (MinioClient client = this.client(runContext)) {

Expand Down Expand Up @@ -165,10 +159,10 @@ public Output run(RunContext runContext) throws Exception {
runContext.metric(Counter.of("count", finalResult.getLeft()));
runContext.metric(Counter.of("size", finalResult.getRight()));

if (errorOnEmpty && finalResult.getLeft() == 0) {
if (runContext.render(errorOnEmpty).as(Boolean.class).orElseThrow() && finalResult.getLeft() == 0) {
throw new NoSuchElementException(
"Unable to find any files to delete on " +
runContext.render(this.bucket) + " " +
runContext.render(this.bucket).as(String.class).orElse(null) + " " +
"with regexp='" + runContext.render(this.regexp) + "', " +
"prefix='" + runContext.render(this.prefix) + "'"
);
Expand Down
17 changes: 8 additions & 9 deletions src/main/java/io/kestra/plugin/minio/Download.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.kestra.core.models.annotations.Plugin;
import io.kestra.core.models.annotations.PluginProperty;
import io.kestra.core.models.executions.metrics.Counter;
import io.kestra.core.models.property.Property;
import io.kestra.core.models.tasks.RunnableTask;
import io.kestra.core.runners.RunContext;
import io.kestra.core.utils.FileUtils;
Expand Down Expand Up @@ -33,10 +34,10 @@
code = """
id: minio_download
namespace: company.team
tasks:
- id: download_from_storage
type: io.kestra.plugin.minio.Download
type: io.kestra.plugin.minio.Download
accessKeyId: "<access-key>"
secretKeyId: "<secret-key>"
region: "eu-central-1"
Expand Down Expand Up @@ -71,22 +72,20 @@ public class Download extends AbstractMinioObject implements RunnableTask<Downlo
@Schema(
title = "The key of a file to download."
)
@PluginProperty(dynamic = true)
private String key;
private Property<String> key;

@Schema(
title = "The specific version of the object."
)
@PluginProperty(dynamic = true)
protected String versionId;
protected Property<String> versionId;

@Override
public Output run(RunContext runContext) throws Exception {
String bucket = runContext.render(this.bucket);
String key = runContext.render(this.key);
String bucket = runContext.render(this.bucket).as(String.class).orElse(null);
String key = runContext.render(this.key).as(String.class).orElse(null);

try (MinioAsyncClient client = this.asyncClient(runContext)) {
Pair<URI, Long> output = MinioService.download(runContext, client, bucket, key, this.versionId);
Pair<URI, Long> output = MinioService.download(runContext, client, bucket, key, runContext.render(this.versionId).as(String.class).orElse(null));

long length = output.getRight();
URI uri = output.getLeft();
Expand Down
Loading

0 comments on commit cf5d9f2

Please sign in to comment.