mirror of
https://github.com/harvester/harvester-ui-extension.git
synced 2026-10-01 03:36:04 +08:00
Refactoring USB Device list component; add USBDevice model; add usbclaim type
Signed-off-by: Francesco Torchia <francesco.torchia@suse.com>
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
import SteveModel from '@shell/plugins/steve/steve-class';
|
||||
import { escapeHtml } from '@shell/utils/string';
|
||||
import { HCI } from '../types';
|
||||
|
||||
const STATUS_DISPLAY = {
|
||||
enabled: {
|
||||
displayKey: 'generic.enabled',
|
||||
color: 'bg-success'
|
||||
},
|
||||
pending: {
|
||||
displayKey: 'generic.inProgress',
|
||||
color: 'bg-info'
|
||||
},
|
||||
disabled: {
|
||||
displayKey: 'generic.disabled',
|
||||
color: 'bg-warning'
|
||||
},
|
||||
error: {
|
||||
displayKey: 'generic.disabled',
|
||||
color: 'bg-warning'
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Class representing USB Device resource.
|
||||
* @extends SteveModal
|
||||
*/
|
||||
export default class USBDevice extends SteveModel {
|
||||
get _availableActions() {
|
||||
const out = super._availableActions;
|
||||
|
||||
out.push(
|
||||
{
|
||||
action: 'enablePassthroughBulk',
|
||||
enabled: !this.isEnabling,
|
||||
icon: 'icon icon-fw icon-dot',
|
||||
label: 'Enable Passthrough',
|
||||
bulkable: true,
|
||||
bulkAction: 'enablePassthroughBulk'
|
||||
},
|
||||
{
|
||||
action: 'disablePassthrough',
|
||||
enabled: this.isEnabling && this.claimedByMe,
|
||||
icon: 'icon icon-fw icon-dot-open',
|
||||
label: 'Disable Passthrough',
|
||||
bulkable: true
|
||||
},
|
||||
);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
get canYaml() {
|
||||
return false;
|
||||
}
|
||||
|
||||
get canDelete() {
|
||||
return false;
|
||||
}
|
||||
|
||||
goToDetail() {
|
||||
return false;
|
||||
}
|
||||
|
||||
goToEdit() {
|
||||
return false;
|
||||
}
|
||||
|
||||
get passthroughClaim() {
|
||||
const passthroughClaims = this.$getters['all'](HCI.USB_CLAIM) || [];
|
||||
|
||||
return !!this.status && passthroughClaims.find(req => req?.spec?.nodeName === this.status?.nodeName && req?.spec?.address === this.status?.address);
|
||||
}
|
||||
|
||||
// this is an id for each 'type' of device - there may be multiple instances of device CRs
|
||||
get uniqueId() {
|
||||
return `${ this.status?.vendorId }:${ this.status?.deviceId }`;
|
||||
}
|
||||
|
||||
get claimedBy() {
|
||||
return this.passthroughClaim?.spec?.userName;
|
||||
}
|
||||
|
||||
get claimedByMe() {
|
||||
if (!this.passthroughClaim) {
|
||||
return false;
|
||||
}
|
||||
const isSingleProduct = this.$rootGetters['isSingleProduct'];
|
||||
let userName = 'admin';
|
||||
|
||||
// if this is imported Harvester, there may be users other than admin
|
||||
if (!isSingleProduct) {
|
||||
const user = this.$rootGetters['auth/v3User'];
|
||||
|
||||
userName = user?.username || user?.id;
|
||||
}
|
||||
|
||||
return this.claimedBy === userName;
|
||||
}
|
||||
|
||||
// isEnabled controls visibility in vm create page & ability to delete claim
|
||||
// isEnabling controls ability to add claim
|
||||
// there will be a brief period where isEnabling === true && isEnabled === false
|
||||
get isEnabled() {
|
||||
return !!this.passthroughClaim?.status?.passthroughEnabled;
|
||||
}
|
||||
|
||||
get isEnabling() {
|
||||
return !!this.passthroughClaim;
|
||||
}
|
||||
|
||||
// map status.passthroughEnabled to disabled/enabled & overwrite default dash colors
|
||||
get claimStatusDisplay() {
|
||||
if (!this.passthroughClaim) {
|
||||
return STATUS_DISPLAY.disabled;
|
||||
}
|
||||
if (this.isEnabled) {
|
||||
return STATUS_DISPLAY.enabled;
|
||||
}
|
||||
|
||||
return STATUS_DISPLAY.pending;
|
||||
}
|
||||
|
||||
get stateDisplay() {
|
||||
const t = this.$rootGetters['i18n/t'];
|
||||
|
||||
return t(this.claimStatusDisplay.displayKey);
|
||||
}
|
||||
|
||||
get stateBackground() {
|
||||
return this.claimStatusDisplay.color;
|
||||
}
|
||||
|
||||
// 'enable' passthrough creates the passthrough claim CRD -
|
||||
enablePassthroughBulk(resources = this) {
|
||||
this.$dispatch('promptModal', {
|
||||
resources,
|
||||
component: 'EnablePassthrough'
|
||||
});
|
||||
}
|
||||
|
||||
// 'disable' passthrough deletes claim
|
||||
// backend should return error if device is in use
|
||||
async disablePassthrough() {
|
||||
try {
|
||||
if (!this.claimedByMe) {
|
||||
throw new Error(this.$rootGetters['i18n/t']('harvester.usb.cantUnclaim', { name: escapeHtml(this.metadata.name) }));
|
||||
} else {
|
||||
await this.passthroughClaim.remove();
|
||||
}
|
||||
} catch (err) {
|
||||
this.$dispatch('growl/fromError', {
|
||||
title: this.$rootGetters['i18n/t']('harvester.usb.unclaimError', { name: escapeHtml(this.metadata.name) }),
|
||||
err,
|
||||
}, { root: true });
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user