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