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 password expiry.
028 */
029public class QmpSetPasswordExpiry extends QmpCommand {
030
031    private final String protocol;
032    private final String expiry;
033
034    /**
035     * Instantiates a new command.
036     *
037     * @param protocol the protocol
038     * @param expiry the expiry time
039     */
040    public QmpSetPasswordExpiry(String protocol, String expiry) {
041        this.protocol = protocol;
042        this.expiry = expiry;
043    }
044
045    @Override
046    public JsonNode toJson() {
047        ObjectNode cmd = mapper.createObjectNode();
048        cmd.put("execute", "expire_password");
049        ObjectNode args = mapper.createObjectNode();
050        cmd.set("arguments", args);
051        args.set("protocol", new TextNode(protocol));
052        args.set("time", new TextNode(expiry));
053        return cmd;
054    }
055
056    @Override
057    public String toString() {
058        try {
059            var json = toJson();
060            return mapper.writeValueAsString(json);
061        } catch (JsonProcessingException e) {
062            return "(no string representation)";
063        }
064    }
065
066}