From 7e650bf9806d4e05fa8ccd76f9dc522a2e0ed7a8 Mon Sep 17 00:00:00 2001 From: "Michael N. Lipp" Date: Wed, 5 Mar 2025 10:18:16 +0100 Subject: [PATCH] Use more constants. --- .../jdrupes/vmoperator/common/Constants.java | 32 ++++++++++++++----- .../vmoperator/runner/qemu/StatusUpdater.java | 23 +++++++------ 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/org.jdrupes.vmoperator.common/src/org/jdrupes/vmoperator/common/Constants.java b/org.jdrupes.vmoperator.common/src/org/jdrupes/vmoperator/common/Constants.java index 0d9657a..c011a24 100644 --- a/org.jdrupes.vmoperator.common/src/org/jdrupes/vmoperator/common/Constants.java +++ b/org.jdrupes.vmoperator.common/src/org/jdrupes/vmoperator/common/Constants.java @@ -78,17 +78,33 @@ public class Constants { /** The Constant COND_RUNNING. */ public static final String COND_RUNNING = "Running"; - /** The Constant COND_BOOTED. */ - public static final String COND_BOOTED = "Booted"; + /** + * Conditions used in Status. + */ + public static class Condition { + /** The Constant COND_BOOTED. */ + public static final String BOOTED = "Booted"; - /** The Constant COND_VMOP_AGENT. */ - public static final String COND_VMOP_AGENT = "VmopAgentConnected"; + /** The Constant COND_VMOP_AGENT. */ + public static final String VMOP_AGENT = "VmopAgentConnected"; - /** The Constant COND_USER_LOGGED_IN. */ - public static final String COND_USER_LOGGED_IN = "UserLoggedIn"; + /** The Constant COND_USER_LOGGED_IN. */ + public static final String USER_LOGGED_IN = "UserLoggedIn"; - /** The Constant COND_CONSOLE. */ - public static final String COND_CONSOLE = "ConsoleConnected"; + /** The Constant COND_CONSOLE. */ + public static final String CONSOLE_CONNECTED = "ConsoleConnected"; + + /** + * Reasons used in conditions. + */ + public static class Reason { + /** The Constant NOT_REQUESTED. */ + public static final String NOT_REQUESTED = "NotRequested"; + + /** The Constant USER_LOGGED_IN. */ + public static final String LOGGED_IN = "LoggedIn"; + } + } } /** diff --git a/org.jdrupes.vmoperator.runner.qemu/src/org/jdrupes/vmoperator/runner/qemu/StatusUpdater.java b/org.jdrupes.vmoperator.runner.qemu/src/org/jdrupes/vmoperator/runner/qemu/StatusUpdater.java index d983311..d9c32c4 100644 --- a/org.jdrupes.vmoperator.runner.qemu/src/org/jdrupes/vmoperator/runner/qemu/StatusUpdater.java +++ b/org.jdrupes.vmoperator.runner.qemu/src/org/jdrupes/vmoperator/runner/qemu/StatusUpdater.java @@ -35,6 +35,8 @@ import java.util.logging.Level; import static org.jdrupes.vmoperator.common.Constants.APP_NAME; import org.jdrupes.vmoperator.common.Constants.Crd; import org.jdrupes.vmoperator.common.Constants.Status; +import org.jdrupes.vmoperator.common.Constants.Status.Condition; +import org.jdrupes.vmoperator.common.Constants.Status.Condition.Reason; import org.jdrupes.vmoperator.common.K8s; import org.jdrupes.vmoperator.common.VmDefinition; import org.jdrupes.vmoperator.common.VmDefinitionStub; @@ -200,7 +202,7 @@ public class StatusUpdater extends VmDefUpdater { boolean running = event.runState().vmRunning(); updateCondition(vmDef, Status.COND_RUNNING, running, event.reason(), event.message()); - JsonObject status = updateCondition(vmDef, Status.COND_BOOTED, + JsonObject status = updateCondition(vmDef, Condition.BOOTED, event.runState() == RunState.BOOTED, event.reason(), event.message()); if (event.runState() == RunState.STARTING) { @@ -216,11 +218,12 @@ public class StatusUpdater extends VmDefUpdater { if (!running) { // In case console connection was still present status.addProperty(Status.CONSOLE_CLIENT, ""); - updateCondition(from, Status.COND_CONSOLE, false, "VmStopped", + updateCondition(from, Condition.CONSOLE_CONNECTED, false, + "VmStopped", "The VM is not running"); // In case we had an irregular shutdown - updateCondition(from, Status.COND_USER_LOGGED_IN, false, + updateCondition(from, Condition.USER_LOGGED_IN, false, "VmStopped", "The VM is not running"); status.remove(Status.OSINFO); updateCondition(vmDef, "VmopAgentConnected", false, "VmStopped", @@ -253,22 +256,22 @@ public class StatusUpdater extends VmDefUpdater { private void updateUserLoggedIn(VmDefinition from) { if (loggedInUser == null) { - updateCondition(from, Status.COND_USER_LOGGED_IN, false, - "NotRequested", "No user to be logged in"); + updateCondition(from, Condition.USER_LOGGED_IN, false, + Reason.NOT_REQUESTED, "No user to be logged in"); return; } - if (!from.conditionStatus(Status.COND_VMOP_AGENT).orElse(false)) { - updateCondition(from, Status.COND_USER_LOGGED_IN, false, + if (!from.conditionStatus(Condition.VMOP_AGENT).orElse(false)) { + updateCondition(from, Condition.USER_LOGGED_IN, false, "VmopAgentDisconnected", "Waiting for VMOP agent to connect"); return; } if (!from.fromStatus(Status.LOGGED_IN_USER).map(loggedInUser::equals) .orElse(false)) { - updateCondition(from, Status.COND_USER_LOGGED_IN, false, + updateCondition(from, Condition.USER_LOGGED_IN, false, "Processing", "Waiting for user to be logged in"); } - updateCondition(from, Status.COND_USER_LOGGED_IN, true, - "UserLoggedIn", "User is logged in"); + updateCondition(from, Condition.USER_LOGGED_IN, true, + Reason.LOGGED_IN, "User is logged in"); } /**