Don't duplicate pool management.

This commit is contained in:
Michael Lipp 2025-01-18 17:16:54 +01:00
parent 76be59a5b3
commit 9b47ad3136
3 changed files with 26 additions and 10 deletions

View file

@ -30,9 +30,21 @@ import org.jgrapes.core.Event;
@SuppressWarnings("PMD.DataClass")
public class GetPools extends Event<List<VmPool>> {
private String name;
private String user;
private List<String> roles = Collections.emptyList();
/**
* Return only the pool with the given name.
*
* @param name the name
* @return the returns the vms
*/
public GetPools withName(String name) {
this.name = name;
return this;
}
/**
* Return only {@link VmPool}s that are accessible by
* the given user or roles.
@ -47,6 +59,15 @@ public class GetPools extends Event<List<VmPool>> {
return this;
}
/**
* Returns the name filter criterion, if set.
*
* @return the optional
*/
public Optional<String> name() {
return Optional.ofNullable(name);
}
/**
* Returns the user filter criterion, if set.
*

View file

@ -171,6 +171,8 @@ public class PoolMonitor extends
@Handler
public void onGetPools(GetPools event) {
event.setResult(pools.values().stream()
.filter(p -> event.name().isEmpty()
|| p.name().equals(event.name().get()))
.filter(p -> event.forUser().isEmpty() && event.forRoles().isEmpty()
|| !p.permissionsFor(event.forUser().orElse(null),
event.forRoles()).isEmpty())

View file

@ -38,7 +38,6 @@ import java.time.Duration;
import java.util.Base64;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
@ -133,8 +132,6 @@ public class VmAccess extends FreeMarkerConlet<VmAccess.ResourceModel> {
private Set<String> syncUsers = Collections.emptySet();
private Set<String> syncRoles = Collections.emptySet();
private boolean deleteConnectionFile = true;
@SuppressWarnings("PMD.UseConcurrentHashMap")
private final Map<String, VmPool> vmPools = new HashMap<>();
/**
* The periodically generated update event.
@ -452,7 +449,9 @@ public class VmAccess extends FreeMarkerConlet<VmAccess.ResourceModel> {
if (model.mode() == ResourceModel.Mode.POOL && model.name() != null) {
// Remove conlet if pool definition has been removed
// or user has not at least one permission
VmPool pool = vmPools.get(model.name());
VmPool pool = appPipeline
.fire(new GetPools().withName(model.name())).get()
.stream().findFirst().orElse(null);
if (pool == null
|| poolPermissions(pool, channel.session()).isEmpty()) {
channel.respond(
@ -588,12 +587,6 @@ public class VmAccess extends FreeMarkerConlet<VmAccess.ResourceModel> {
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
public void onVmPoolChanged(VmPoolChanged event) {
var poolName = event.vmPool().name();
if (event.deleted()) {
vmPools.remove(poolName);
} else {
vmPools.put(poolName, event.vmPool());
}
// Update known conlets
for (var entry : conletIdsByConsoleConnection().entrySet()) {
var connection = entry.getKey();