diff --git a/org.jdrupes.vmoperator.vmaccess/src/org/jdrupes/vmoperator/vmaccess/VmAccess.java b/org.jdrupes.vmoperator.vmaccess/src/org/jdrupes/vmoperator/vmaccess/VmAccess.java index d38d379ba..4ac339796 100644 --- a/org.jdrupes.vmoperator.vmaccess/src/org/jdrupes/vmoperator/vmaccess/VmAccess.java +++ b/org.jdrupes.vmoperator.vmaccess/src/org/jdrupes/vmoperator/vmaccess/VmAccess.java @@ -559,6 +559,11 @@ private Set 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)); } @@ -567,32 +572,36 @@ private Set 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 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)); @@ -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()) { @@ -697,7 +708,9 @@ public void onVmPoolChanged(VmPoolChanged event) { .isEmpty()) { connection.respond( new DeleteConlet(conletId, Collections.emptySet())); + continue; } + updateConfig(connection, model.get(), null); } } } diff --git a/org.jdrupes.vmoperator.vmaccess/src/org/jdrupes/vmoperator/vmaccess/browser/VmAccess-functions.ts b/org.jdrupes.vmoperator.vmaccess/src/org/jdrupes/vmoperator/vmaccess/browser/VmAccess-functions.ts index f00d87690..de65216cb 100644 --- a/org.jdrupes.vmoperator.vmaccess/src/org/jdrupes/vmoperator/vmaccess/browser/VmAccess-functions.ts +++ b/org.jdrupes.vmoperator.vmaccess/src/org/jdrupes/vmoperator/vmaccess/browser/VmAccess-functions.ts @@ -45,6 +45,7 @@ interface Api { vmName: string; vmDefinition: any; poolName: string | null; + permissions: string[]; } const localize = (key: string) => { @@ -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); @@ -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, @@ -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; @@ -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",