Skip to content

Commit

Permalink
Update GUI on pool permission changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mnlipp committed Jan 23, 2025
1 parent 6d5ba88 commit 1cb90b0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,11 @@ private Set<Permission> permissions(ResourceModel model, Session session,
result.addAll(pool.permissionsFor(user, roles));
}
}
if (vmDef == null) {
vmDef = appPipeline.fire(new GetVms().assignedFrom(model.name())
.assignedTo(user)).get().stream().map(VmData::definition)
.findFirst().orElse(null);
}
if (vmDef != null) {
result.addAll(vmDef.permissionsFor(user, roles));
}
Expand All @@ -567,32 +572,36 @@ private Set<Permission> permissions(ResourceModel model, Session session,

private void updatePreview(ConsoleConnection channel, ResourceModel model,
VmDefinition vmDef) throws InterruptedException {
channel.respond(new NotifyConletView(type(),
model.getConletId(), "updateConfig", model.mode(), model.name()));
updateConfig(channel, model, vmDef);
updateVmDef(channel, model, vmDef);
}

private void updateConfig(ConsoleConnection channel, ResourceModel model,
VmDefinition vmDef) throws InterruptedException {
channel.respond(new NotifyConletView(type(),
model.getConletId(), "updateConfig", model.mode(), model.name(),
permissions(model, channel.session(), null, vmDef).stream()
.map(VmDefinition.Permission::toString).toList()));
}

private void updateVmDef(ConsoleConnection channel, ResourceModel model,
VmDefinition vmDef) throws InterruptedException {
Map<String, Object> data = null;
if (vmDef != null) {
if (vmDef == null) {
model.setAssignedVm(null);
} else {
model.setAssignedVm(vmDef.name());
try {
data = Map.of("metadata",
Map.of("namespace", vmDef.namespace(),
"name", vmDef.name()),
"spec", vmDef.spec(),
"status", vmDef.getStatus(),
"userPermissions",
permissions(model, channel.session(), null, vmDef).stream()
.map(VmDefinition.Permission::toString).toList());
"status", vmDef.getStatus());
} catch (JsonSyntaxException e) {
logger.log(Level.SEVERE, e,
() -> "Failed to serialize VM definition");
return;
}
} else {
model.setAssignedVm(null);
}
channel.respond(new NotifyConletView(type(),
model.getConletId(), "updateVmDefinition", data));
Expand Down Expand Up @@ -677,10 +686,12 @@ public void onVmDefChanged(VmDefChanged event, VmChannel channel)
*
* @param event the event
* @param channel the channel
* @throws InterruptedException
*/
@Handler(namedChannels = "manager")
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
public void onVmPoolChanged(VmPoolChanged event) {
public void onVmPoolChanged(VmPoolChanged event)
throws InterruptedException {
var poolName = event.vmPool().name();
// Update known conlets
for (var entry : conletIdsByConsoleConnection().entrySet()) {
Expand All @@ -697,7 +708,9 @@ public void onVmPoolChanged(VmPoolChanged event) {
.isEmpty()) {
connection.respond(
new DeleteConlet(conletId, Collections.emptySet()));
continue;
}
updateConfig(connection, model.get(), null);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ interface Api {
vmName: string;
vmDefinition: any;
poolName: string | null;
permissions: string[];
}

const localize = (key: string) => {
Expand All @@ -64,7 +65,8 @@ window.orgJDrupesVmOperatorVmAccess.initPreview = (previewDom: HTMLElement,
const previewApi: Api = reactive({
vmName: "",
vmDefinition: {},
poolName: null
poolName: null,
permissions: []
});
const poolName = computed(() => previewApi.poolName);
const vmName = computed(() => previewApi.vmDefinition.name);
Expand All @@ -77,15 +79,14 @@ window.orgJDrupesVmOperatorVmAccess.initPreview = (previewDom: HTMLElement,
const startable = computed(() => previewApi.vmDefinition.spec
&& previewApi.vmDefinition.spec.vm.state !== 'Running'
&& !previewApi.vmDefinition.running
&& previewApi.vmDefinition.userPermissions.includes('start')
&& previewApi.permissions.includes('start')
|| previewApi.poolName !== null && !previewApi.vmDefinition.name);
const stoppable = computed(() => previewApi.vmDefinition.spec &&
previewApi.vmDefinition.spec.vm.state !== 'Stopped'
&& previewApi.vmDefinition.running);
const running = computed(() => previewApi.vmDefinition.running);
const inUse = computed(() => previewApi.vmDefinition.usedBy != '');
const permissions = computed(() => previewApi.vmDefinition.spec
? previewApi.vmDefinition.userPermissions : []);
const permissions = computed(() => previewApi.permissions);

watch(previewApi, (api: Api) => {
JGConsole.instance.updateConletTitle(conletId,
Expand Down Expand Up @@ -150,7 +151,8 @@ window.orgJDrupesVmOperatorVmAccess.initPreview = (previewDom: HTMLElement,

JGConsole.registerConletFunction("org.jdrupes.vmoperator.vmaccess.VmAccess",
"updateConfig",
function(conletId: string, type: string, resource: string) {
function(conletId: string, type: string, resource: string,
permissions: []) {
const conlet = JGConsole.findConletPreview(conletId);
if (!conlet) {
return;
Expand All @@ -164,6 +166,7 @@ JGConsole.registerConletFunction("org.jdrupes.vmoperator.vmaccess.VmAccess",
api.poolName = resource;
api.vmName = "";
}
api.permissions = permissions;
});

JGConsole.registerConletFunction("org.jdrupes.vmoperator.vmaccess.VmAccess",
Expand Down

0 comments on commit 1cb90b0

Please sign in to comment.