001/*
002 * VM-Operator
003 * Copyright (C) 2023 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.runner.qemu.commands;
020
021import com.fasterxml.jackson.core.JsonProcessingException;
022import com.fasterxml.jackson.databind.JsonNode;
023import com.fasterxml.jackson.databind.node.ObjectNode;
024import com.fasterxml.jackson.databind.node.TextNode;
025
026/**
027 * A {@link QmpCommand} that sets the display password.
028 */
029public class QmpSetDisplayPassword extends QmpCommand {
030
031    private final String password;
032    private final String protocol;
033
034    /**
035     * Instantiates a new command.
036     *
037     * @param protocol the protocol
038     * @param password the password
039     */
040    public QmpSetDisplayPassword(String protocol, String password) {
041        this.protocol = protocol;
042        this.password = password;
043    }
044
045    @Override
046    public JsonNode toJson() {
047        ObjectNode cmd = mapper.createObjectNode();
048        cmd.put("execute", "set_password");
049        ObjectNode args = mapper.createObjectNode();
050        cmd.set("arguments", args);
051        args.set("protocol", new TextNode(protocol));
052        args.set("password", new TextNode(password));
053        return cmd;
054    }
055
056    @Override
057    public String toString() {
058        try {
059            var json = toJson();
060            ((ObjectNode) json.get("arguments")).set("password",
061                new TextNode("********"));
062            return mapper.writeValueAsString(json);
063        } catch (JsonProcessingException e) {
064            return "(no string representation)";
065        }
066    }
067
068}