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.Collections;
022import java.util.List;
023import java.util.Optional;
024import org.jdrupes.vmoperator.common.VmDefinition;
025import org.jgrapes.core.Event;
026
027/**
028 * Gets the known VMs' definitions and channels.
029 */
030public class GetVms extends Event<List<GetVms.VmData>> {
031
032    private String name;
033    private String user;
034    private List<String> roles = Collections.emptyList();
035    private String fromPool;
036    private String toUser;
037
038    /**
039     * Return only the VMs with the given name.
040     *
041     * @param name the name
042     * @return the returns the vms
043     */
044    public GetVms withName(String name) {
045        this.name = name;
046        return this;
047    }
048
049    /**
050     * Return only {@link VmDefinition}s that are accessible by
051     * the given user or roles.
052     *
053     * @param user the user
054     * @param roles the roles
055     * @return the event
056     */
057    public GetVms accessibleFor(String user, List<String> roles) {
058        this.user = user;
059        this.roles = roles;
060        return this;
061    }
062
063    /**
064     * Return only {@link VmDefinition}s that are assigned from the given pool.
065     *
066     * @param pool the pool
067     * @return the returns the vms
068     */
069    public GetVms assignedFrom(String pool) {
070        this.fromPool = pool;
071        return this;
072    }
073
074    /**
075     * Return only {@link VmDefinition}s that are assigned to the given user.
076     *
077     * @param user the user
078     * @return the returns the vms
079     */
080    public GetVms assignedTo(String user) {
081        this.toUser = user;
082        return this;
083    }
084
085    /**
086     * Returns the name filter criterion, if set.
087     *
088     * @return the optional
089     */
090    public Optional<String> name() {
091        return Optional.ofNullable(name);
092    }
093
094    /**
095     * Returns the user filter criterion, if set.
096     *
097     * @return the optional
098     */
099    public Optional<String> user() {
100        return Optional.ofNullable(user);
101    }
102
103    /**
104     * Returns the roles criterion.
105     *
106     * @return the list
107     */
108    public List<String> roles() {
109        return roles;
110    }
111
112    /**
113     * Returns the pool filter criterion, if set.
114     *
115     * @return the optional
116     */
117    public Optional<String> fromPool() {
118        return Optional.ofNullable(fromPool);
119    }
120
121    /**
122     * Returns the user filter criterion, if set.
123     *
124     * @return the optional
125     */
126    public Optional<String> toUser() {
127        return Optional.ofNullable(toUser);
128    }
129
130    /**
131     * Return tuple.
132     *
133     * @param definition the definition
134     * @param channel the channel
135     */
136    public record VmData(VmDefinition definition, VmChannel channel) {
137    }
138}