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 java.util.Optional;
022import org.jdrupes.vmoperator.common.VmDefinition;
023import org.jgrapes.core.Event;
024
025/**
026 * Gets the current display secret and optionally updates it.
027 */
028@SuppressWarnings("PMD.DataClass")
029public class GetDisplayPassword extends Event<String> {
030
031    private final VmDefinition vmDef;
032    private final String user;
033
034    /**
035     * Instantiates a new request for the display secret.
036     *
037     * @param vmDef the vm name
038     * @param user the requesting user
039     */
040    public GetDisplayPassword(VmDefinition vmDef, String user) {
041        this.vmDef = vmDef;
042        this.user = user;
043    }
044
045    /**
046     * Gets the vm definition.
047     *
048     * @return the vm definition
049     */
050    public VmDefinition vmDefinition() {
051        return vmDef;
052    }
053
054    /**
055     * Return the id of the user who has requested the password.
056     *
057     * @return the string
058     */
059    public String user() {
060        return user;
061    }
062
063    /**
064     * Return the password. May only be called when the event is completed.
065     *
066     * @return the optional
067     */
068    public Optional<String> password() {
069        if (!isDone()) {
070            throw new IllegalStateException("Event is not done.");
071        }
072        return currentResults().stream().findFirst();
073    }
074}