-
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): add a randomPort pebble function
- Loading branch information
1 parent
01a77af
commit 4d074cb
Showing
3 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
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
32 changes: 32 additions & 0 deletions
32
core/src/main/java/io/kestra/core/runners/pebble/functions/RandomPortFunction.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,32 @@ | ||
package io.kestra.core.runners.pebble.functions; | ||
|
||
import io.pebbletemplates.pebble.error.PebbleException; | ||
import io.pebbletemplates.pebble.extension.Function; | ||
import io.pebbletemplates.pebble.template.EvaluationContext; | ||
import io.pebbletemplates.pebble.template.PebbleTemplate; | ||
|
||
import java.io.IOException; | ||
import java.net.ServerSocket; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class RandomPortFunction implements Function { | ||
@Override | ||
public Object execute(Map<String, Object> args, PebbleTemplate self, EvaluationContext context, int lineNumber) { | ||
try (ServerSocket tempSocket = new ServerSocket(0)) { | ||
return tempSocket.getLocalPort(); | ||
} catch (IOException e) { | ||
throw new PebbleException( | ||
e, | ||
"Unable to get random port", | ||
lineNumber, | ||
self.getName() | ||
); | ||
} | ||
} | ||
|
||
@Override | ||
public List<String> getArgumentNames() { | ||
return List.of(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
core/src/test/java/io/kestra/core/runners/pebble/functions/RandomPortFunctionTest.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,23 @@ | ||
package io.kestra.core.runners.pebble.functions; | ||
|
||
import io.kestra.core.exceptions.IllegalVariableEvaluationException; | ||
import io.kestra.core.junit.annotations.KestraTest; | ||
import io.kestra.core.runners.VariableRenderer; | ||
import jakarta.inject.Inject; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Collections; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.greaterThanOrEqualTo; | ||
|
||
@KestraTest | ||
class RandomPortFunctionTest { | ||
@Inject VariableRenderer variableRenderer; | ||
|
||
@Test | ||
void checkIsDefined() throws IllegalVariableEvaluationException { | ||
String rendered = variableRenderer.render("{{ randomPort() }}", Collections.emptyMap()); | ||
assertThat(Integer.parseInt(rendered), greaterThanOrEqualTo(0)); | ||
} | ||
} |