Skip to content

Commit

Permalink
Don't duplicate pool management.
Browse files Browse the repository at this point in the history
  • Loading branch information
mnlipp committed Jan 18, 2025
1 parent 76be59a commit 9b47ad3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,21 @@
@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.
Expand All @@ -47,6 +59,15 @@ public GetPools accessibleFor(String user, List<String> roles) {
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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ public void onVmDefChanged(VmDefChanged event) {
@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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -452,7 +449,9 @@ private Set<RenderMode> renderPreview(RenderConletRequestBase<?> event,
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(
Expand Down Expand Up @@ -588,12 +587,6 @@ public void onVmDefChanged(VmDefChanged event, VmChannel channel)
@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();
Expand Down

0 comments on commit 9b47ad3

Please sign in to comment.