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 */
027public class GetDisplaySecret extends Event<String> {
028
029    private final VmDefinition vmDef;
030    private final String user;
031
032    /**
033     * Instantiates a new request for the display secret.
034     * After handling the event, a result of `null` means that
035     * no secret is needed. No result means that the console
036     * is not accessible.
037     * 
038     * @param vmDef the vm name
039     * @param user the requesting user
040     */
041    public GetDisplaySecret(VmDefinition vmDef, String user) {
042        this.vmDef = vmDef;
043        this.user = user;
044    }
045
046    /**
047     * Gets the VM definition.
048     *
049     * @return the VM definition
050     */
051    public VmDefinition vmDefinition() {
052        return vmDef;
053    }
054
055    /**
056     * Return the id of the user who has requested the password.
057     *
058     * @return the string
059     */
060    public String user() {
061        return user;
062    }
063
064    /**
065     * Returns `true` if a password is available. May only be called
066     * when the event is completed. Note that the password returned
067     * by {@link #secret()} may be `null`, indicating that no password
068     * is needed.
069     *
070     * @return true, if successful
071     */
072    public boolean secretAvailable() {
073        if (!isDone()) {
074            throw new IllegalStateException("Event is not done.");
075        }
076        return !currentResults().isEmpty();
077    }
078
079    /**
080     * Return the secret. May only be called when the event has been
081     * completed with a valid result (see {@link #secretAvailable()}).
082     *
083     * @return the password. A value of `null` means that no password
084     * is required.
085     */
086    public String secret() {
087        if (!isDone() || currentResults().isEmpty()) {
088            throw new IllegalStateException("Event is not done.");
089        }
090        return currentResults().get(0);
091    }
092}