Skip to content

Commit

Permalink
Added Ephemeral Execute with Tag option
Browse files Browse the repository at this point in the history
Co-authored-by: Petra Scherer <[email protected]>
Co-authored-by: Timo Klenk <[email protected]>
Signed-off-by: Johannes Graf <[email protected]>
  • Loading branch information
2 people authored and Johannes Graf committed Mar 15, 2022
1 parent 4595165 commit e48949b
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.carbynestack.cli.exceptions.CsCliRunnerException;
import io.carbynestack.ephemeral.client.ActivationError;
import io.carbynestack.ephemeral.client.ActivationResult;
import io.vavr.concurrent.Future;
import io.vavr.control.Either;
import io.vavr.control.Try;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -42,15 +43,28 @@ private Either<Throwable, String> readCodeFromStdIn() {
public void run() throws CsCliRunnerException {
System.out.println("Provide program to execute. Press Ctrl+D to submit.");
ExecuteEphemeralClientCliCommandConfig c = this.getConfig();

//Both input options provided
if(c.getTagFilters().size() != 0 && c.getInputs().size() != 0) {
throw new CsCliRunnerException(getMessages().getString("execute.failure.both-inputs-provided"));
}

String code =
readCodeFromStdIn()
.getOrElseThrow(
e ->
new CsCliRunnerException(
getMessages().getString("execute.failure.read-code"), e));
Either<ActivationError, List<ActivationResult>> result =
client
.execute(code, c.getInputs())


Future<Either<ActivationError, List<ActivationResult>>> execute;
if (c.getTagFilters().size() != 0) {
execute = client.executeWithTags(code, c.getTagFilters());
} else {
execute = client.execute(code, c.getInputs());
}

Either<ActivationError, List<ActivationResult>> result = execute
.toTry()
.getOrElseThrow(
t ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public class ExecuteEphemeralClientCliCommandConfig extends EphemeralClientCliCo
order = 1)
private List<UUID> inputs = Lists.newArrayList();

@Parameter(
names = {"-f", "--tagfilter"},
descriptionKey = "execute.option.tag-filter-description",
order = 2)
private List<String> tagFilters = Lists.newArrayList();

@Override
public String getCommandName() {
return COMMAND_NAME;
Expand Down
8 changes: 7 additions & 1 deletion src/main/resources/EphemeralMessageBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ execute.option.input-description=UUID of an Amphora Secret used as secret input
\t\t\texecution.\n\
\t\t\tThis option can be defined multiple times in order to to use\n\
\t\t\tmultiple secrets as input for the execution.
execute.option.tag-filter-description=Tag filters that match the Amphora secrets for the inputs\n\
\t\t\tCan be in one of the following formats:\n\
\t\t\t\t- key:value EQUAL Match\n\
\t\t\t\t- key>value GREATER THAN (Assumes tag value is a numeric value)\n\
\t\t\t\t- key<value LESS THAN (Assumes tag value is a numeric value)
execute.log.success=Function successfully executed (result: {0}).
execute.failure.read-code=Failed to read the function code.
execute.failure.invoke=Failed to execute the backend Ephemeral service.
execute.failure.invoke-status-code=Failed to trigger the backend Ephemeral service: status code: {0}, message: {1}.
execute.failure.invoke-different-results=Clients returned different results.
execute.failure.invoke-different-results=Clients returned different results.
execute.failure.both-inputs-provided=Must provide either UUID inputs (-i/--input) or TagFilter inputs (-f/--tagfilter), not both!
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,29 @@ public void givenInputOptionWithInvalidUUID_whenParsing_thenExceptionWithCorrect
}
}

@Test
public void givenBothTagFiltersAndInputsProvided_whenUsingEphemeralClient_thenExceptionWithCorrectMessageIsThrows()
throws CsCliLoginException, CsCliException {
String uuid = UUID.randomUUID().toString();
String tagFilter = "key:value";

try {
getCliWithArgs(
ExecuteEphemeralClientCliCommandConfig.COMMAND_NAME,
"-i",
uuid,
"-f",
tagFilter,
"app.example.com")
.parse();
Assert.fail("Expected Exception");
} catch (CsCliRunnerException e) {
Assert.assertThat(
e.getMessage(),
CoreMatchers.containsString(EPHEMERAL_CLI_MESSAGES.getString("execute.failure.both-inputs-provided")));
}
}

@Test
public void givenUnreachableEphemeralUris_whenParsing_thenExceptionIsThrown()
throws CsCliException, CsCliLoginException {
Expand Down Expand Up @@ -194,6 +217,34 @@ public void givenAllRequiredArguments_whenParsing_thenCallClientWithCorrectArgum
CoreMatchers.containsString(Arrays.toString(results.get(0).getResponse().toArray())));
}

@Test
public void givenAllRequiredArgumentsWithTagFilter_whenParsing_thenCallTheClientWithCorrectArguments()
throws Exception{
String code = "a = sint(1)";
String application = "app.svc.example.com";
String tagFilter = "key:value";
List<String> tagFilters = Collections.singletonList(tagFilter);

UUID outputId = UUID.randomUUID();
List<ActivationResult> results =
Lists.newArrayList(
new ActivationResult(Collections.singletonList(outputId)),
new ActivationResult(Collections.singletonList(outputId)));
when(client.executeWithTags(eq(code), eq(tagFilters))).thenReturn(Future.successful(Either.right(results)));
System.setIn(new ReaderInputStream(new StringReader(code), StandardCharsets.UTF_8));
getCliWithArgs(
true,
ExecuteEphemeralClientCliCommandConfig.COMMAND_NAME,
"-f",
tagFilter,
application)
.parse();
verify(client).executeWithTags(eq(code), eq(tagFilters));
Assert.assertThat(
systemOutRule.getLog(),
CoreMatchers.containsString(Arrays.toString(results.get(0).getResponse().toArray())));
}

@Test
public void givenPlayersReturnDifferentResults_whenInvokingFunction_thenThrowException() {
String code = "a = sint(1)";
Expand Down

0 comments on commit e48949b

Please sign in to comment.