Skip to content

Commit

Permalink
Simplify (some) invocations.
Browse files Browse the repository at this point in the history
  • Loading branch information
mnlipp committed Nov 3, 2024
1 parent b250cad commit e40fed7
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,29 @@ public Map<String, Object> spec() {
return spec;
}

/**
* Get a value from the spec using {@link DataPath#get}.
*
* @param <T> the generic type
* @param selectors the selectors
* @return the value, if found
*/
public <T> Optional<T> fromSpec(Object... selectors) {
return DataPath.get(spec, selectors);
}

/**
* Get a value from the `spec().get("vm")` using {@link DataPath#get}.
*
* @param <T> the generic type
* @param selectors the selectors
* @return the value, if found
*/
public <T> Optional<T> fromVm(Object... selectors) {
return DataPath.get(spec, "vm")
.flatMap(vm -> DataPath.get(vm, selectors));
}

/**
* Sets the spec.
*
Expand Down Expand Up @@ -202,6 +225,17 @@ public Map<String, Object> status() {
return status;
}

/**
* Get a value from the status using {@link DataPath#get}.
*
* @param <T> the generic type
* @param selectors the selectors
* @return the value, if found
*/
public <T> Optional<T> fromStatus(Object... selectors) {
return DataPath.get(status, selectors);
}

/**
* Sets the status.
*
Expand Down Expand Up @@ -259,7 +293,7 @@ public String namespace() {
*/
public RequestedVmState vmState() {
// TODO
return DataPath.get(this, "spec", "vm", "state")
return fromVm("state")
.map(s -> "Running".equals(s) ? RequestedVmState.RUNNING
: RequestedVmState.STOPPED)
.orElse(RequestedVmState.STOPPED);
Expand All @@ -274,8 +308,7 @@ public RequestedVmState vmState() {
*/
public Set<Permission> permissionsFor(String user,
Collection<String> roles) {
return DataPath
.<List<Map<String, Object>>> get(this, "spec", "permissions")
return this.<List<Map<String, Object>>> fromSpec("permissions")
.orElse(Collections.emptyList()).stream()
.filter(p -> DataPath.get(p, "user").map(u -> u.equals(user))
.orElse(false)
Expand All @@ -293,7 +326,7 @@ public Set<Permission> permissionsFor(String user,
* @return the optional
*/
public Optional<Long> displayPasswordSerial() {
return DataPath.<Number> get(status(), "displayPasswordSerial")
return this.<Number> fromStatus("displayPasswordSerial")
.map(Number::longValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ public void reconcile(VmDefChanged event,
Map<String, Object> model, VmChannel channel)
throws IOException, TemplateException, ApiException {
// Secret needed at all?
var display = DataPath
.get(event.vmDefinition().spec(), "vm", "display").get();
var display = event.vmDefinition().fromVm("display").get();
if (!DataPath.<Boolean> get(display, "spice", "generateSecret")
.orElse(true)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.jdrupes.vmoperator.common.VmDefinition;
import org.jdrupes.vmoperator.manager.events.VmChannel;
import org.jdrupes.vmoperator.manager.events.VmDefChanged;
import org.jdrupes.vmoperator.util.DataPath;
import org.jdrupes.vmoperator.util.GsonPtr;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
Expand Down Expand Up @@ -97,8 +96,9 @@ public void reconcile(VmDefChanged event,

// Load balancer can also be turned off for VM
var vmDef = event.vmDefinition();
if (DataPath.<Map<String, Map<String, String>>> get(vmDef, "spec",
LOAD_BALANCER_SERVICE).map(m -> m.isEmpty()).orElse(false)) {
if (vmDef
.<Map<String, Map<String, String>>> fromSpec(LOAD_BALANCER_SERVICE)
.map(m -> m.isEmpty()).orElse(false)) {
return;
}

Expand Down Expand Up @@ -130,9 +130,8 @@ private void mergeMetadata(Gson gson, DynamicKubernetesObject svcDef,
Map<String, Map<String, String>> defaults,
VmDefinition vmDefinition) {
// Get specific load balancer metadata from VM definition
var vmLbMeta = DataPath
.<Map<String, Map<String, String>>> get(vmDefinition, "spec",
LOAD_BALANCER_SERVICE)
var vmLbMeta = vmDefinition
.<Map<String, Map<String, String>>> fromSpec(LOAD_BALANCER_SERVICE)
.orElse(Collections.emptyMap());

// Merge
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ public void reconcile(VmDefChanged event, Map<String, Object> model,
reconcileRunnerDataPvc(event, model, channel, knownPvcs);

// Reconcile pvcs for defined disks
var diskDefs = DataPath.<List<Map<String, Object>>> get(vmDef.spec(),
"vm", "disks").orElse(List.of());
var diskDefs = vmDef.<List<Map<String, Object>>> fromVm("disks")
.orElse(List.of());
var diskCounter = 0;
for (var diskDef : diskDefs) {
if (!diskDef.containsKey("volumeClaimTemplate")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,14 @@ private void addDynamicData(K8sClient client, VmDefinition vmDef,
// This results in pod information being shown for a stopped
// VM which is irritating. So check condition first.
@SuppressWarnings("PMD.LambdaCanBeMethodReference")
var isRunning = DataPath
.<List<Map<String, Object>>> get(vmDef.status(), "conditions")
.orElse(Collections.emptyList()).stream()
.filter(cond -> DataPath.get(cond, "type")
.map(t -> "Running".equals(t)).orElse(false))
.findFirst().map(cond -> DataPath.get(cond, "status")
.map(s -> "True".equals(s)).orElse(false))
.orElse(false);
var isRunning
= vmDef.<List<Map<String, Object>>> fromStatus("conditions")
.orElse(Collections.emptyList()).stream()
.filter(cond -> DataPath.get(cond, "type")
.map(t -> "Running".equals(t)).orElse(false))
.findFirst().map(cond -> DataPath.get(cond, "status")
.map(s -> "True".equals(s)).orElse(false))
.orElse(false);
if (!isRunning) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,21 +322,20 @@ private Summary evaluateSummary(boolean force) {
Summary summary = new Summary();
for (var vmDef : channelTracker.associated()) {
summary.totalVms += 1;
var status = vmDef.status();
summary.usedCpus += DataPath.<Number> get(status, "cpus")
summary.usedCpus += vmDef.<Number> fromStatus("cpus")
.map(Number::intValue).orElse(0);
summary.usedRam = summary.usedRam
.add(DataPath.<String> get(status, "ram")
.add(vmDef.<String> fromStatus("ram")
.map(r -> Quantity.fromString(r).getNumber().toBigInteger())
.orElse(BigInteger.ZERO));
summary.runningVms = DataPath
.<List<Map<String, Object>>> get(vmDef.status(), "conditions")
.orElse(Collections.emptyList()).stream()
.filter(cond -> DataPath.get(cond, "type")
.map(t -> "Running".equals(t)).orElse(false)
&& DataPath.get(cond, "status")
.map(s -> "True".equals(s)).orElse(false))
.count();
summary.runningVms
= vmDef.<List<Map<String, Object>>> fromStatus("conditions")
.orElse(Collections.emptyList()).stream()
.filter(cond -> DataPath.get(cond, "type")
.map(t -> "Running".equals(t)).orElse(false)
&& DataPath.get(cond, "status")
.map(s -> "True".equals(s)).orElse(false))
.count();
}
cachedSummary = summary;
return summary;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,8 +560,7 @@ private void openConsole(String vmName, ConsoleConnection connection,
logger.severe(() -> "Failed to find display IP for " + vmName);
return;
}
var port = DataPath
.<Number> get(vmDef.spec(), "vm", "display", "spice", "port")
var port = vmDef.<Number> fromVm("display", "spice", "port")
.map(Number::longValue);
if (port.isEmpty()) {
logger.severe(() -> "No port defined for display of " + vmName);
Expand All @@ -575,8 +574,7 @@ private void openConsole(String vmName, ConsoleConnection connection,
if (password != null) {
data.append("password=").append(password).append('\n');
}
DataPath
.<String> get(vmDef.spec(), "vm", "display", "spice", "proxyUrl")
vmDef.<String> fromVm("display", "spice", "proxyUrl")
.ifPresent(u -> {
if (!Strings.isNullOrEmpty(u)) {
data.append("proxy=").append(u).append('\n');
Expand All @@ -591,8 +589,7 @@ private void openConsole(String vmName, ConsoleConnection connection,
}

private Optional<InetAddress> displayIp(VmDefinition vmDef) {
Optional<String> server = DataPath.get(vmDef.spec(),
"vm", "display", "spice", "server");
Optional<String> server = vmDef.fromVm("display", "spice", "server");
if (server.isPresent()) {
var srv = server.get();
try {
Expand Down

0 comments on commit e40fed7

Please sign in to comment.