001/* 002 * VM-Operator 003 * Copyright (C) 2024 Michael N. Lipp 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU Affero General Public License as 007 * published by the Free Software Foundation, either version 3 of the 008 * License, or (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Affero General Public License for more details. 014 * 015 * You should have received a copy of the GNU Affero General Public License 016 * along with this program. If not, see <https://www.gnu.org/licenses/>. 017 */ 018 019package org.jdrupes.vmoperator.manager.events; 020 021import org.jdrupes.vmoperator.common.VmDefinition; 022import org.jgrapes.core.Event; 023 024/** 025 * Gets the current display secret and optionally updates it. 026 */ 027@SuppressWarnings("PMD.DataClass") 028public class PrepareConsole extends Event<String> { 029 030 private final VmDefinition vmDef; 031 private final String user; 032 private final boolean loginUser; 033 034 /** 035 * Instantiates a new request for the display secret. 036 * After handling the event, a result of `null` means that 037 * no password is needed. No result means that the console 038 * is not accessible. 039 * 040 * @param vmDef the vm name 041 * @param user the requesting user 042 * @param loginUser login the user 043 */ 044 public PrepareConsole(VmDefinition vmDef, String user, 045 boolean loginUser) { 046 this.vmDef = vmDef; 047 this.user = user; 048 this.loginUser = loginUser; 049 } 050 051 /** 052 * Instantiates a new request for the display secret. 053 * After handling the event, a result of `null` means that 054 * no password is needed. No result means that the console 055 * is not accessible. 056 * 057 * @param vmDef the vm name 058 * @param user the requesting user 059 */ 060 public PrepareConsole(VmDefinition vmDef, String user) { 061 this(vmDef, user, false); 062 } 063 064 /** 065 * Gets the vm definition. 066 * 067 * @return the vm definition 068 */ 069 public VmDefinition vmDefinition() { 070 return vmDef; 071 } 072 073 /** 074 * Return the id of the user who has requested the password. 075 * 076 * @return the string 077 */ 078 public String user() { 079 return user; 080 } 081 082 /** 083 * Checks if the user should be logged in before allowing access. 084 * 085 * @return the loginUser 086 */ 087 public boolean loginUser() { 088 return loginUser; 089 } 090 091 /** 092 * Returns `true` if a password is available. May only be called 093 * when the event is completed. Note that the password returned 094 * by {@link #password()} may be `null`, indicating that no password 095 * is needed. 096 * 097 * @return true, if successful 098 */ 099 public boolean passwordAvailable() { 100 if (!isDone()) { 101 throw new IllegalStateException("Event is not done."); 102 } 103 return !currentResults().isEmpty(); 104 } 105 106 /** 107 * Return the password. May only be called when the event has been 108 * completed with a valid result (see {@link #passwordAvailable()}). 109 * 110 * @return the password. A value of `null` means that no password 111 * is required. 112 */ 113 public String password() { 114 if (!isDone() || currentResults().isEmpty()) { 115 throw new IllegalStateException("Event is not done."); 116 } 117 return currentResults().get(0); 118 } 119}