Search in sources :

Example 1 with LoginOnBehalfParameters

use of org.ovirt.engine.core.common.action.LoginOnBehalfParameters in project ovirt-engine by oVirt.

the class VMConsoleProxyServlet method availableConsoles.

private List<Map<String, String>> availableConsoles(String userIdAsString) {
    List<Map<String, String>> jsonVms = new ArrayList<>();
    Guid userGuid = null;
    try {
        if (StringUtils.isNotEmpty(userIdAsString)) {
            userGuid = Guid.createGuidFromString(userIdAsString);
        }
    } catch (IllegalArgumentException e) {
        log.debug("Could not read User GUID");
    }
    if (userGuid != null) {
        ActionReturnValue loginResult = backend.runInternalAction(ActionType.LoginOnBehalf, new LoginOnBehalfParameters(userGuid));
        if (!loginResult.getSucceeded()) {
            throw new RuntimeException("Unable to create session using LoginOnBehalf");
        }
        String engineSessionId = loginResult.getActionReturnValue();
        try {
            QueryReturnValue retVms = backend.runInternalQuery(QueryType.GetAllVmsForUserAndActionGroup, new GetEntitiesWithPermittedActionParameters(ActionGroup.CONNECT_TO_SERIAL_CONSOLE), new EngineContext().withSessionId(engineSessionId));
            if (retVms != null) {
                List<VM> vmsList = retVms.getReturnValue();
                for (VM vm : vmsList) {
                    Map<String, String> jsonVm = new HashMap<>();
                    if (vm.getRunOnVds() != null) {
                        // TODO: avoid one query per loop. Bulk query?
                        QueryReturnValue retValue = backend.runInternalQuery(QueryType.GetVdsByVdsId, new IdQueryParameters(vm.getRunOnVds()));
                        if (retValue != null && retValue.getReturnValue() != null) {
                            VDS vds = retValue.getReturnValue();
                            jsonVm.put("vmid", vm.getId().toString());
                            jsonVm.put("vmname", vm.getName());
                            jsonVm.put("host", vds.getHostName());
                            /* there is only one serial console, no need and no way to distinguish them */
                            jsonVm.put("console", "default");
                            jsonVms.add(jsonVm);
                        }
                    }
                }
            }
        } finally {
            backend.runInternalAction(ActionType.LogoutSession, new ActionParametersBase(engineSessionId));
        }
    }
    return jsonVms;
}
Also used : GetEntitiesWithPermittedActionParameters(org.ovirt.engine.core.common.queries.GetEntitiesWithPermittedActionParameters) VDS(org.ovirt.engine.core.common.businessentities.VDS) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Guid(org.ovirt.engine.core.compat.Guid) ActionParametersBase(org.ovirt.engine.core.common.action.ActionParametersBase) QueryReturnValue(org.ovirt.engine.core.common.queries.QueryReturnValue) IdQueryParameters(org.ovirt.engine.core.common.queries.IdQueryParameters) ActionReturnValue(org.ovirt.engine.core.common.action.ActionReturnValue) LoginOnBehalfParameters(org.ovirt.engine.core.common.action.LoginOnBehalfParameters) VM(org.ovirt.engine.core.common.businessentities.VM) EngineContext(org.ovirt.engine.core.bll.context.EngineContext) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 EngineContext (org.ovirt.engine.core.bll.context.EngineContext)1 ActionParametersBase (org.ovirt.engine.core.common.action.ActionParametersBase)1 ActionReturnValue (org.ovirt.engine.core.common.action.ActionReturnValue)1 LoginOnBehalfParameters (org.ovirt.engine.core.common.action.LoginOnBehalfParameters)1 VDS (org.ovirt.engine.core.common.businessentities.VDS)1 VM (org.ovirt.engine.core.common.businessentities.VM)1 GetEntitiesWithPermittedActionParameters (org.ovirt.engine.core.common.queries.GetEntitiesWithPermittedActionParameters)1 IdQueryParameters (org.ovirt.engine.core.common.queries.IdQueryParameters)1 QueryReturnValue (org.ovirt.engine.core.common.queries.QueryReturnValue)1 Guid (org.ovirt.engine.core.compat.Guid)1