Skip to content

Commit

Permalink
implement withExecutors for LocalCall
Browse files Browse the repository at this point in the history
  • Loading branch information
mcalmer committed Oct 21, 2024
1 parent aece007 commit 38773fa
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/main/java/com/suse/salt/netapi/calls/LocalCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,36 @@ public class LocalCall<R> extends AbstractCall<R> {
private final Optional<?> metadata;
private final Optional<Integer> timeout;
private final Optional<Integer> gatherJobTimeout;
private final Optional<List<String>> executors;
private final Optional<Map<String, ?>> executorOptions;

public LocalCall(String functionName, Optional<List<?>> arg,
Optional<Map<String, ?>> kwarg, TypeToken<R> returnType,
Optional<?> metadata, Optional<Integer> timeout,
Optional<Integer> gatherJobTimeout) {
Optional<Integer> gatherJobTimeout, Optional<List<String>> executors,
Optional<Map<String, ?>> executorOptions) {
super(functionName, returnType);
this.arg = arg;
this.kwarg = kwarg;
this.metadata = metadata;
this.timeout = timeout;
this.gatherJobTimeout = gatherJobTimeout;
this.executors = executors;
this.executorOptions = executorOptions;
}

public LocalCall(String functionName, Optional<List<?>> arg,
Optional<Map<String, ?>> kwarg, TypeToken<R> returnType,
Optional<Integer> timeout, Optional<Integer> gatherJobTimeout) {
this(functionName, arg, kwarg, returnType, Optional.empty(),
timeout, gatherJobTimeout);
timeout, gatherJobTimeout, Optional.empty(), Optional.empty());
}

public LocalCall(String functionName, Optional<List<?>> arg,
Optional<Map<String, ?>> kwarg, TypeToken<R> returnType,
Optional<?> metadata) {
this(functionName, arg, kwarg, returnType, metadata, Optional.empty(),
Optional.empty());
Optional.empty(), Optional.empty(), Optional.empty());
}

public LocalCall(String functionName, Optional<List<?>> arg,
Expand All @@ -78,23 +83,34 @@ public LocalCall(String functionName, Optional<List<?>> arg,

public LocalCall<R> withMetadata(Object metadata) {
return new LocalCall<>(getFunction(), arg, kwarg, getReturnType(),
Optional.of(metadata), timeout, gatherJobTimeout);
Optional.of(metadata), timeout, gatherJobTimeout, executors, executorOptions);
}

public LocalCall<R> withoutMetadata() {
return new LocalCall<>(getFunction(), arg, kwarg, getReturnType(),
Optional.empty(), timeout, gatherJobTimeout);
Optional.empty(), timeout, gatherJobTimeout, executors, executorOptions);
}

public LocalCall<R> withTimeouts(Optional<Integer> timeout,
Optional<Integer> gatherJobTimeout) {
return new LocalCall<>(getFunction(), arg, kwarg, getReturnType(), metadata,
timeout, gatherJobTimeout);
timeout, gatherJobTimeout, executors, executorOptions);
}

public LocalCall<R> withoutTimeouts() {
return new LocalCall<>(getFunction(), arg, kwarg, getReturnType(), metadata,
Optional.empty(), Optional.empty());
Optional.empty(), Optional.empty(), executors, executorOptions);
}

public LocalCall<R> withExecutors(Optional<List<String>> executors,
Optional<Map<String, ?>> executorOptions) {
return new LocalCall<>(getFunction(), arg, kwarg, getReturnType(), metadata,
timeout, gatherJobTimeout, executors, executorOptions);
}

public LocalCall<R> withoutExecutors() {
return new LocalCall<>(getFunction(), arg, kwarg, getReturnType(), metadata,
timeout, gatherJobTimeout, Optional.empty(), Optional.empty());
}

/**
Expand All @@ -110,6 +126,8 @@ public Map<String, Object> getPayload() {
timeout.ifPresent(timeout -> payload.put("timeout", timeout));
gatherJobTimeout.ifPresent(gatherJobTimeout -> payload.put("gather_job_timeout",
gatherJobTimeout));
executors.ifPresent(exe -> payload.put("module_executors", exe));
executorOptions.ifPresent(opts -> payload.put("executor_opts", opts));
return payload;
}

Expand Down
29 changes: 29 additions & 0 deletions src/test/java/com/suse/salt/netapi/calls/LocalCallTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
Expand Down Expand Up @@ -122,6 +124,33 @@ public void testWithTimeouts() {
assertEquals(runWithTimeouts.getPayload().get("timeout"), 4);
assertEquals(runWithTimeouts.getPayload().get("gather_job_timeout"), 1);
}

@Test
public void testWithExecutors() {
LocalCall<String> run = Cmd.run("echo 'hello world'");
assertFalse(run.getPayload().containsKey("module_executors"));
assertFalse(run.getPayload().containsKey("executor_opts"));

LocalCall<String> runWithExecutors = run.withExecutors(Optional.of(List.of("direct_call")), Optional.empty());
assertTrue(runWithExecutors.getPayload().containsKey("module_executors"));
assertEquals(List.of("direct_call"), runWithExecutors.getPayload().get("module_executors"));
assertFalse(runWithExecutors.getPayload().containsKey("executor_opts"));

runWithExecutors = runWithExecutors.withExecutors(Optional.of(List.of("splay")), Optional.of(Map.of("splaytime", 30)));
assertTrue(runWithExecutors.getPayload().containsKey("module_executors"));
assertEquals(List.of("splay"), runWithExecutors.getPayload().get("module_executors"));
assertTrue(runWithExecutors.getPayload().containsKey("executor_opts"));
assertEquals(Map.of("splaytime", 30), runWithExecutors.getPayload().get("executor_opts"));

LocalCall<String> runWithoutExecutors = runWithExecutors.withoutExecutors();
assertFalse(runWithoutExecutors.getPayload().containsKey("module_executors"));
assertFalse(runWithoutExecutors.getPayload().containsKey("executor_opts"));
assertTrue(runWithExecutors.getPayload().containsKey("module_executors"));
assertEquals(List.of("splay"), runWithExecutors.getPayload().get("module_executors"));
assertTrue(runWithExecutors.getPayload().containsKey("executor_opts"));
assertEquals(Map.of("splaytime", 30), runWithExecutors.getPayload().get("executor_opts"));
}

/**
* Verify that system return the correct module name and function name
*/
Expand Down

0 comments on commit 38773fa

Please sign in to comment.