Add pkg/harvester components + shell portings - 1

Signed-off-by: Francesco Torchia <francesco.torchia@suse.com>
This commit is contained in:
Francesco Torchia committed 2024-10-23 17:00:46 +02:00
1 parent f8408469f7
commit 4f2688f6ab
265 files changed
+42316 -82

No files matched your search

@@ -0,0 +1,498 @@
<script>
import draggable from 'vuedraggable';
import InfoBox from '@shell/components/InfoBox';
import { Banner } from '@components/Banner';
import BadgeStateFormatter from '@shell/components/formatter/BadgeStateFormatter';
import UnitInput from '@shell/components/form/UnitInput';
import { LabeledInput } from '@components/Form/LabeledInput';
import LabeledSelect from '@shell/components/form/LabeledSelect';
import ModalWithCard from '@shell/components/ModalWithCard';
import { PVC, STORAGE_CLASS } from '@shell/config/types';
import { HCI } from '../../../types';
import { clone } from '@shell/utils/object';
import { removeObject } from '@shell/utils/array';
import { randomStr } from '@shell/utils/string';
import { SOURCE_TYPE } from '../../../config/harvester-map';
import { _VIEW, _EDIT, _CREATE } from '@shell/config/query-params';
import { PRODUCT_NAME as HARVESTER_PRODUCT } from '../../../config/harvester';
import { PLUGIN_DEVELOPER, DEV } from '@shell/store/prefs';
export default {
components: {
Banner, BadgeStateFormatter, draggable, InfoBox, LabeledInput, UnitInput, LabeledSelect, ModalWithCard
},
props: {
vm: {
type: Object,
default: () => {
return {};
}
},
mode: {
type: String,
default: _CREATE
},
value: {
type: Array,
default: () => {
return [];
}
},
namespace: {
type: String,
default: null
},
existingVolumeDisabled: {
type: Boolean,
default: false
},
validateRequired: {
type: Boolean,
default: false
},
customVolumeMode: {
type: String,
default: 'Block'
},
customAccessMode: {
type: String,
default: 'ReadWriteMany'
},
resourceType: {
type: String,
default: ''
}
},
async fetch() {
await this.$store.dispatch('harvester/findAll', { type: PVC });
},
data() {
return {
SOURCE_TYPE,
rows: clone(this.value),
nameIdx: 1,
vol: null,
isOpen: false,
};
},
computed: {
dev() {
try {
return this.$store.getters['prefs/get'](PLUGIN_DEVELOPER);
} catch {
return this.$store.getters['prefs/get'](DEV);
}
},
isVirtualType() {
return this.resourceType === HCI.VM;
},
isView() {
return this.mode === _VIEW;
},
isEdit() {
return this.mode === _EDIT;
},
isCreate() {
return this.mode === _CREATE;
},
showVolumeTip() {
const imageName = this.getImageDisplayName(this.rows[0]?.image);
if (this.rows.length === 1 && this.rows[0].type === 'cd-rom' && /.iso$/i.test(imageName)) {
return true;
}
return false;
},
pvcs() {
return this.$store.getters['harvester/all'](PVC) || [];
},
},
watch: {
value: {
handler(neu) {
const rows = neu.map((V) => {
if (!this.isCreate && V.source !== SOURCE_TYPE.CONTAINER && !V.newCreateId) {
V.to = {
name: `${ HARVESTER_PRODUCT }-c-cluster-resource-namespace-id`,
params: {
resource: HCI.VOLUME,
namespace: this.namespace,
id: V.realName
},
query: { mode: _EDIT }
};
V.pvc = this.pvcs.find((pvc) => pvc.metadata.name === V.realName);
}
return V;
});
this.$set(this, 'rows', rows);
},
deep: true,
immediate: true,
},
},
methods: {
addVolume(type) {
const name = this.generateName();
const neu = {
id: randomStr(5),
name,
source: type,
size: '10Gi',
type: 'disk',
accessMode: this.customAccessMode,
volumeMode: this.customVolumeMode,
volumeName: '',
bus: 'virtio',
newCreateId: randomStr(10), // judge whether it is a disk that has been created
};
if (type === SOURCE_TYPE.NEW) {
const inStore = this.$store.getters['currentProduct'].inStore;
const defaultStorage = this.$store.getters[`${ inStore }/all`](STORAGE_CLASS).find( (O) => O.isDefault);
neu.storageClassName = defaultStorage?.metadata?.name || 'longhorn';
}
this.rows.push(neu);
this.update();
},
generateName() {
let name = '';
let hasName = true;
while (hasName) {
name = `disk-${ this.nameIdx }`;
hasName = this.rows.find((O) => O.name === name);
this.nameIdx++;
}
return name;
},
removeVolume(vol) {
this.vol = vol;
if (!vol.newCreateId && this.isEdit && this.isVirtualType) {
this.isOpen = true;
} else {
removeObject(this.rows, vol);
this.update();
}
},
unplugVolume(volume) {
this.vm.unplugVolume(volume.name);
},
componentFor(type) {
switch (type) {
case SOURCE_TYPE.NEW:
return require(`./type/volume.vue`).default;
case SOURCE_TYPE.IMAGE:
return require(`./type/vmImage.vue`).default;
case SOURCE_TYPE.ATTACH_VOLUME:
return require(`./type/existing.vue`).default;
case SOURCE_TYPE.CONTAINER:
return require(`./type/container.vue`).default;
}
},
headerFor(type) {
return {
[SOURCE_TYPE.NEW]: this.$store.getters['i18n/t']('harvester.virtualMachine.volume.title.volume'),
[SOURCE_TYPE.IMAGE]: this.$store.getters['i18n/t']('harvester.virtualMachine.volume.title.vmImage'),
[SOURCE_TYPE.ATTACH_VOLUME]: this.$store.getters['i18n/t']('harvester.virtualMachine.volume.title.existingVolume'),
[SOURCE_TYPE.CONTAINER]: this.$store.getters['i18n/t']('harvester.virtualMachine.volume.title.container'),
}[type];
},
update() {
this.$emit('input', this.rows);
},
deleteVolume() {
removeObject(this.rows, this.vol);
this.update();
this.cancel();
},
cancel() {
this.isOpen = false;
},
changeSort(idx, type) {
// true: down, false: up
this.rows.splice(type ? idx : idx - 1, 1, ...this.rows.splice(type ? idx + 1 : idx, 1, this.rows[type ? idx : idx - 1]));
this.update();
},
getImageDisplayName(id) {
return this.$store.getters['harvester/all'](HCI.IMAGE).find((image) => image.id === id)?.spec?.displayName;
}
},
};
</script>
<template>
<div>
<Banner
v-if="!isView"
color="info"
label-key="harvester.virtualMachine.volume.dragTip"
/>
<draggable
v-model="rows"
:disabled="isView"
@end="update"
>
<transition-group>
<div
v-for="(volume, i) in rows"
:key="volume.id"
>
<InfoBox class="box">
<button
v-if="!isView"
type="button"
class="role-link btn btn-sm remove"
@click="removeVolume(volume)"
>
<i class="icon icon-x" />
</button>
<button
v-if="volume.hotpluggable && isView"
type="button"
class="role-link btn remove"
@click="unplugVolume(volume)"
>
{{ t('harvester.virtualMachine.unplug.detachVolume') }}
</button>
<h3>
<span
v-if="volume.to && isVirtualType"
class="title"
>
<router-link :to="volume.to">
{{ t('harvester.virtualMachine.volume.edit') }} {{ headerFor(volume.source) }}
</router-link>
<BadgeStateFormatter
v-if="volume.pvc"
class="ml-10 state"
:arbitrary="true"
:row="volume.pvc"
:value="volume.pvc.state"
/>
<a
v-if="dev && !!volume.pvc && !!volume.pvc.resourceExternalLink"
v-clean-tooltip="t(volume.pvc.resourceExternalLink.tipsKey || 'generic.resourceExternalLinkTips')"
class="ml-5 resource-external"
rel="nofollow noopener noreferrer"
target="_blank"
:href="volume.pvc.resourceExternalLink.url"
>
<i class="icon icon-external-link" />
</a>
</span>
<span v-else>
{{ headerFor(volume.source) }}
</span>
</h3>
<div>
<component
:is="componentFor(volume.source)"
v-model="rows[i]"
:rows="rows"
:namespace="namespace"
:is-create="isCreate"
:is-edit="isEdit"
:is-view="isView"
:is-virtual-type="isVirtualType"
:mode="mode"
:idx="i"
:validate-required="validateRequired"
@update="update"
/>
</div>
<div class="bootOrder">
<div
v-if="!isView"
class="mr-15"
>
<button
:disabled="i === 0"
class="btn btn-sm role-primary"
@click.prevent="changeSort(i, false)"
>
<i class="icon icon-lg icon-chevron-up"></i>
</button>
<button
:disabled="i === rows.length -1"
class="btn btn-sm role-primary"
@click.prevent="changeSort(i, true)"
>
<i class="icon icon-lg icon-chevron-down"></i>
</button>
</div>
<div class="text-muted">
bootOrder: {{ i + 1 }}
</div>
</div>
<Banner
v-if="volume.volumeStatus && !isCreate"
class="mt-15 volume-status"
color="warning"
:label="volume.volumeStatus"
/>
</InfoBox>
</div>
</transition-group>
</draggable>
<Banner
v-if="showVolumeTip"
color="warning"
:label="t('harvester.virtualMachine.volume.volumeTip')"
/>
<div v-if="!isView">
<button
type="button"
class="btn btn-sm bg-primary mr-15 mb-10"
:disabled="rows.length === 0"
@click="addVolume(SOURCE_TYPE.NEW)"
>
{{ t('harvester.virtualMachine.volume.addVolume') }}
</button>
<button
v-if="!existingVolumeDisabled"
type="button"
class="btn btn-sm bg-primary mr-15 mb-10"
@click="addVolume(SOURCE_TYPE.ATTACH_VOLUME)"
>
{{ t('harvester.virtualMachine.volume.addExistingVolume') }}
</button>
<button
type="button"
class="btn btn-sm bg-primary mr-15 mb-10"
@click="addVolume(SOURCE_TYPE.IMAGE)"
>
{{ t('harvester.virtualMachine.volume.addVmImage') }}
</button>
<button
type="button"
class="btn btn-sm bg-primary mb-10"
@click="addVolume(SOURCE_TYPE.CONTAINER)"
>
{{ t('harvester.virtualMachine.volume.addContainer') }}
</button>
</div>
<ModalWithCard
v-if="isOpen"
name="deleteTip"
:width="400"
>
<template #title>
{{ t('harvester.virtualMachine.volume.unmount.title') }}
</template>
<template #content>
<span>{{ t('harvester.virtualMachine.volume.unmount.message') }}</span>
</template>
<template #footer>
<div class="buttons">
<button
class="btn role-secondary mr-20"
@click.prevent="cancel"
>
{{ t('generic.no') }}
</button>
<button
class="btn bg-primary mr-20"
@click.prevent="deleteVolume"
>
{{ t('generic.yes') }}
</button>
</div>
</template>
</ModalWithCard>
</div>
</template>
<style lang='scss' scoped>
.box {
position: relative;
}
.title {
display: flex;
.state {
font-size: 16px;
}
}
.remove {
position: absolute;
top: 10px;
right: 10px;
padding: 0px;
}
.bootOrder {
display: flex;
align-items: center;
}
.buttons {
width: 100%;
display: flex;
justify-content: flex-end;
}
.volume-status:first-letter {
text-transform: uppercase;
}
.resource-external {
display: flex;
justify-content: center;
align-items: center;
}
</style>
@@ -0,0 +1,40 @@
import { mount } from '@vue/test-utils';
import HarvesterEditContainer from '../container.vue';
import { _EDIT } from '@shell/config/query-params';
describe('component: HarvesterEditContainer', () => {
it('should display all the inputs', () => {
const wrapper = mount(HarvesterEditContainer, { propsData: { mode: _EDIT, value: {} } });
const inputWraps = wrapper.findAll('[data-testid^=input-hec-]');
expect(inputWraps).toHaveLength(4);
});
it.each([
'name',
'container',
])('should emit an update on %p input', (field) => {
const wrapper = mount(HarvesterEditContainer, { propsData: { mode: _EDIT, value: {} } });
const input = wrapper.find(`[data-testid="input-hec-${ field }"]`).find('input');
const newValue = 123;
input.setValue(newValue);
expect(wrapper.emitted('update')).toHaveLength(1);
});
it.each([
'type',
'bus',
])('should emit an update on %p selection change', async(field) => {
const wrapper = mount(HarvesterEditContainer, { propsData: { mode: _EDIT, value: {} } });
const select = wrapper.find(`[data-testid="input-hec-${ field }"]`);
select.find('button').trigger('click');
await wrapper.trigger('keydown.down');
await wrapper.trigger('keydown.enter');
expect(wrapper.emitted('update')).toHaveLength(1);
});
});
@@ -0,0 +1,103 @@
import { mount } from '@vue/test-utils';
import HarvesterEditExisting from '../existing.vue';
import { _EDIT } from '@shell/config/query-params';
describe('component: HarvesterEditExisting', () => {
it('should display all the inputs', () => {
const wrapper = mount(HarvesterEditExisting, {
propsData: {
mode: _EDIT, value: {}, rows: []
},
mocks: {
$store: {
getters: {
'harvester/all': () => [],
'i18n/t': jest.fn()
}
}
}
});
const inputWraps = wrapper.findAll('[data-testid^=input-hee-]');
expect(inputWraps).toHaveLength(5);
});
it.each([
'name',
])('should emit an update on %p input', (field) => {
const wrapper = mount(HarvesterEditExisting, {
propsData: {
mode: _EDIT, value: {}, rows: []
},
mocks: {
$store: {
getters: {
'harvester/all': () => [],
'i18n/t': jest.fn()
}
}
}
});
const input = wrapper.find(`[data-testid="input-hee-${ field }"]`).find('input');
const newValue = 123;
input.setValue(newValue);
expect(wrapper.emitted('update')).toHaveLength(1);
});
// TODO: #6179: Restore this test when the input is allowed to edit
// eslint-disable-next-line jest/no-disabled-tests
it.skip.each([
'size',
])('should emit an update on %p input and blur', (field) => {
const wrapper = mount(HarvesterEditExisting, {
propsData: {
mode: _EDIT, value: {}, rows: []
},
mocks: {
$store: {
getters: {
'harvester/all': () => [],
'i18n/t': jest.fn()
}
}
}
});
const input = wrapper.find(`[data-testid="input-hee-${ field }"]`).find('input');
const newValue = 123;
input.setValue(newValue);
input.trigger('blur');
expect(wrapper.emitted('input')).toHaveLength(1);
});
it.each([
'type',
'volumeName',
// 'bus', // disabled
])('should emit an update on %p selection change', async(field) => {
const wrapper = mount(HarvesterEditExisting, {
propsData: {
mode: _EDIT, value: {}, rows: []
},
mocks: {
$store: {
getters: {
'harvester/all': () => [],
'i18n/t': jest.fn()
}
}
}
});
const select = wrapper.find(`[data-testid="input-hee-${ field }"]`);
select.find('button').trigger('click');
await wrapper.trigger('keydown.down');
await wrapper.trigger('keydown.enter');
expect(wrapper.emitted('update')).toHaveLength(1);
});
});
@@ -0,0 +1,117 @@
import { mount } from '@vue/test-utils';
import HarvesterEditVMImage from '../vmImage.vue';
import { _CREATE } from '@shell/config/query-params';
describe('component: HarvesterEditVMImage', () => {
it('should display all the inputs', () => {
const wrapper = mount(HarvesterEditVMImage, {
propsData: {
mode: _CREATE,
isVirtualType: false,
newCreateId: '123',
validateRequired: true,
idx: 1
},
mocks: {
$store: {
getters: {
'harvester/all': jest.fn(),
'i18n/t': jest.fn(),
}
},
}
});
const inputWraps = wrapper.findAll('[data-testid^=input-hevi-]');
expect(inputWraps).toHaveLength(5);
});
it.each([
'name',
])('should emit an update on %p input', (field) => {
const wrapper = mount(HarvesterEditVMImage, {
propsData: {
mode: _CREATE,
isVirtualType: false,
newCreateId: '123',
validateRequired: true,
idx: 1
},
mocks: {
$store: {
getters: {
'harvester/all': jest.fn(),
'i18n/t': jest.fn(),
}
},
}
});
const input = wrapper.find(`[data-testid="input-hevi-${ field }"]`).find('input');
const newValue = 123;
input.setValue(newValue);
expect(wrapper.emitted('update')).toHaveLength(1);
});
it.each([
'size',
])('should emit an update on %p input blur', (field) => {
const wrapper = mount(HarvesterEditVMImage, {
propsData: {
mode: _CREATE,
isVirtualType: false,
newCreateId: '123',
validateRequired: true,
idx: 1
},
mocks: {
$store: {
getters: {
'harvester/all': jest.fn(),
'i18n/t': jest.fn(),
}
},
}
});
const input = wrapper.find(`[data-testid="input-hevi-${ field }"]`).find('input');
const newValue = 123;
input.setValue(newValue);
input.trigger('blur');
expect(wrapper.emitted('update')).toHaveLength(1);
});
it.each([
'type',
'image',
'bus',
])('should emit an update on %p selection change', async(field) => {
const wrapper = mount(HarvesterEditVMImage, {
propsData: {
mode: _CREATE,
isVirtualType: false,
newCreateId: '123',
validateRequired: true,
idx: 1
},
mocks: {
$store: {
getters: {
'harvester/all': jest.fn(),
'i18n/t': jest.fn(),
}
},
}
});
const select = wrapper.find(`[data-testid="input-hevi-${ field }"]`);
select.find('button').trigger('click');
await wrapper.trigger('keydown.down');
await wrapper.trigger('keydown.enter');
expect(wrapper.emitted('update')).toHaveLength(1);
});
});
@@ -0,0 +1,74 @@
import { mount } from '@vue/test-utils';
import HarvesterEditVolume from '../volume.vue';
import { _EDIT } from '@shell/config/query-params';
describe('component: HarvesterEditVolume', () => {
it('should display all the inputs', () => {
const wrapper = mount(HarvesterEditVolume, {
propsData: { validateRequired: true },
mocks: {
$store: {
getters: {
'harvester/all': jest.fn(),
'i18n/t': jest.fn(),
'i18n/exists': jest.fn(),
}
},
}
});
const inputWraps = wrapper.findAll('[data-testid^=input-hev-]');
expect(inputWraps).toHaveLength(4);
});
it.each([
'name',
'size',
])('should emit an update on %p input blur', (field) => {
const wrapper = mount(HarvesterEditVolume, {
propsData: { mode: _EDIT, validateRequired: true },
mocks: {
$store: {
getters: {
'harvester/all': jest.fn(),
'i18n/t': jest.fn(),
'i18n/exists': jest.fn(),
}
},
}
});
const input = wrapper.find(`[data-testid="input-hev-${ field }"]`).find('input');
const newValue = 123;
input.setValue(newValue);
input.trigger('blur');
expect(wrapper.emitted('update')).toHaveLength(1);
});
it.each([
'type',
'bus'
])('should emit an update on %p selection change', async(field) => {
const wrapper = mount(HarvesterEditVolume, {
propsData: { mode: _EDIT, validateRequired: true },
mocks: {
$store: {
getters: {
'harvester/all': jest.fn(),
'i18n/t': jest.fn(),
'i18n/exists': jest.fn(),
}
},
}
});
const select = wrapper.find(`[data-testid="input-hev-${ field }"]`);
select.find('button').trigger('click');
await wrapper.trigger('keydown.down');
await wrapper.trigger('keydown.enter');
expect(wrapper.emitted('update')).toHaveLength(1);
});
});
@@ -0,0 +1,132 @@
<script>
import { LabeledInput } from '@components/Form/LabeledInput';
import LabeledSelect from '@shell/components/form/LabeledSelect';
import InputOrDisplay from '@shell/components/InputOrDisplay';
import { VOLUME_TYPE, InterfaceOption } from '../../../../config/harvester-map';
export default {
name: 'HarvesterEditContainer',
components: {
LabeledInput, LabeledSelect, InputOrDisplay
},
props: {
mode: {
type: String,
default: 'create'
},
value: {
type: Object,
required: true
},
},
data() {
return {
VOLUME_TYPE,
InterfaceOption
};
},
watch: {
'value.type'(neu) {
if (neu === 'cd-rom') {
this.$set(this.value, 'bus', 'sata');
this.update();
}
},
},
methods: {
update() {
this.$emit('update');
}
}
};
</script>
<template>
<div>
<div class="row mb-20">
<div
data-testid="input-hec-name"
class="col span-6"
>
<InputOrDisplay
:name="t('harvester.fields.name')"
:value="value.name"
:mode="mode"
>
<LabeledInput
v-model="value.name"
:label="t('harvester.fields.name')"
required
:mode="mode"
@input="update"
/>
</InputOrDisplay>
</div>
<div
data-testid="input-hec-type"
class="col span-6"
>
<InputOrDisplay
:name="t('harvester.fields.type')"
:value="value.type"
:mode="mode"
>
<LabeledSelect
v-model="value.type"
:label="t('harvester.fields.type')"
:options="VOLUME_TYPE"
:mode="mode"
required
@input="update"
/>
</InputOrDisplay>
</div>
</div>
<div class="row mb-20">
<div
data-testid="input-hec-container"
class="col span-6"
>
<InputOrDisplay
:name="t('harvester.virtualMachine.volume.dockerImage')"
:value="value.container"
:mode="mode"
>
<LabeledInput
v-model="value.container"
:label="t('harvester.virtualMachine.volume.dockerImage')"
:mode="mode"
required
@input="update"
/>
</InputOrDisplay>
</div>
<div
data-testid="input-hec-bus"
class="col span-3"
>
<InputOrDisplay
:name="t('harvester.virtualMachine.volume.bus')"
:value="value.bus"
:mode="mode"
>
<LabeledSelect
v-model="value.bus"
:label="t('harvester.virtualMachine.volume.bus')"
:options="InterfaceOption"
:mode="mode"
@input="update"
/>
</InputOrDisplay>
</div>
</div>
</div>
</template>
@@ -0,0 +1,298 @@
<script>
import UnitInput from '@shell/components/form/UnitInput';
import { LabeledInput } from '@components/Form/LabeledInput';
import LabeledSelect from '@shell/components/form/LabeledSelect';
import InputOrDisplay from '@shell/components/InputOrDisplay';
import { sortBy } from '@shell/utils/sort';
import { PVC } from '@shell/config/types';
import { HCI } from '../../../../types';
import { _CREATE } from '@shell/config/query-params';
import { VOLUME_TYPE, InterfaceOption } from '../../../../config/harvester-map';
import { HCI as HCI_ANNOTATIONS } from '@pkg/harvester/config/labels-annotations';
export default {
name: 'HarvesterEditExisting',
components: {
UnitInput, LabeledInput, LabeledSelect, InputOrDisplay
},
props: {
mode: {
type: String,
default: _CREATE
},
value: {
type: Object,
required: true
},
isEdit: {
type: Boolean,
default: false
},
namespace: {
type: String,
default: null
},
idx: {
type: Number,
default: 0
},
rows: {
type: Array,
required: true
},
},
data() {
if (this.value.realName) {
this.value.volumeName = this.value.realName;
}
return {
VOLUME_TYPE,
InterfaceOption,
loading: false,
};
},
computed: {
isDisabled() {
return !this.value.newCreateId && this.isEdit;
},
allPVCs() {
const allPVCs = this.$store.getters['harvester/all'](PVC);
return allPVCs.filter((P) => {
return this.namespace ? this.namespace === P.metadata.namespace : true;
}) || [];
},
image() {
const imageResource = this.$store.getters['harvester/all'](HCI.IMAGE).find(I => I.id === this.pvcResource?.metadata?.annotations?.[HCI_ANNOTATIONS.IMAGE_ID]);
if (!imageResource) {
return;
}
return `${ imageResource.metadata.namespace }/${ imageResource.spec.displayName }`;
},
pvcResource() {
return this.allPVCs.find( P => P.metadata.name === this.value.volumeName );
},
volumeOption() {
return sortBy(
this.allPVCs
.filter( (pvc) => {
let isAvailable = true;
let isBeingUsed = false;
this.rows.forEach( (O) => {
if (O.volumeName === pvc.metadata.name) {
isAvailable = false;
}
});
if (this.idx === 0 && !pvc.metadata?.annotations?.[HCI_ANNOTATIONS.IMAGE_ID]) {
return false;
}
if (pvc.attachVM && isAvailable && pvc.attachVM?.id === this.vm?.id && this.isEdit) {
isBeingUsed = false;
} else if (pvc.attachVM) {
isBeingUsed = true;
}
return isAvailable && !isBeingUsed && pvc.isAvailable;
})
.map((pvc) => {
return {
label: pvc.metadata.name,
value: pvc.metadata.name
};
}),
'label'
);
},
},
watch: {
'value.volumeName'(neu) {
const pvcResource = this.allPVCs.find( P => P.metadata.name === neu);
if (!pvcResource) {
return;
}
this.value.accessModes = pvcResource.spec.accessModes[0];
this.value.size = pvcResource.spec.resources.requests.storage;
this.value.storageClassName = pvcResource.spec.storageClassName;
this.value.volumeMode = pvcResource.spec.volumeMode;
},
'value.type'(neu) {
if (neu === 'cd-rom') {
this.$set(this.value, 'bus', 'sata');
this.update();
}
},
pvcResource: {
handler(pvc) {
if (!this.value.volumeName && pvc?.metadata?.name) {
this.value.volumeName = pvc.metadata.name;
}
},
deep: true,
immediate: true
},
},
methods: {
update() {
this.$emit('update');
},
}
};
</script>
<template>
<div>
<div class="row mb-20">
<div
data-testid="input-hee-name"
class="col span-6"
>
<InputOrDisplay
:name="t('harvester.fields.name')"
:value="value.name"
:mode="mode"
>
<LabeledInput
v-model="value.name"
:label="t('harvester.fields.name')"
:mode="mode"
required
@input="update"
/>
</InputOrDisplay>
</div>
<div
data-testid="input-hee-type"
class="col span-6"
>
<InputOrDisplay
:name="t('harvester.fields.type')"
:value="value.type"
:mode="mode"
>
<LabeledSelect
v-model="value.type"
:label="t('harvester.fields.type')"
:mode="mode"
:options="VOLUME_TYPE"
required
@input="update"
/>
</InputOrDisplay>
</div>
</div>
<div class="row mb-20">
<div
data-testid="input-hee-volumeName"
class="col span-6"
>
<InputOrDisplay
:name="t('harvester.fields.volume')"
:value="value.volumeName"
:mode="mode"
>
<LabeledSelect
v-model="value.volumeName"
:disabled="isDisabled"
:label="t('harvester.fields.volume')"
:mode="mode"
:options="volumeOption"
required
@input="update"
/>
</InputOrDisplay>
</div>
<div
data-testid="input-hee-size"
class="col span-6"
>
<InputOrDisplay
:name="t('harvester.fields.size')"
:value="value.size"
:mode="mode"
>
<UnitInput
v-model="value.size"
:output-modifier="true"
:increment="1024"
:input-exponent="3"
:label="t('harvester.fields.size')"
:mode="mode"
:disabled="true"
@input="update"
/>
</InputOrDisplay>
</div>
</div>
<div class="row mb-20">
<div
v-if="!!image"
data-testid="input-hee-image"
class="col span-6"
>
<InputOrDisplay
:name="t('harvester.fields.image')"
:value="image"
:mode="mode"
>
<LabeledInput
v-model="image"
:label="t('harvester.fields.image')"
:mode="mode"
:disabled="true"
@input="update"
/>
</InputOrDisplay>
</div>
<div
data-testid="input-hee-bus"
class="col span-6"
>
<InputOrDisplay
:name="t('harvester.virtualMachine.volume.bus')"
:value="value.bus"
:mode="mode"
>
<LabeledSelect
v-model="value.bus"
:label="t('harvester.virtualMachine.volume.bus')"
:mode="mode"
:options="InterfaceOption"
:disabled="true"
required
@input="update"
/>
</InputOrDisplay>
</div>
</div>
</div>
</template>
@@ -0,0 +1,282 @@
<script>
import { findBy } from '@shell/utils/array';
import UnitInput from '@shell/components/form/UnitInput';
import { LabeledInput } from '@components/Form/LabeledInput';
import LabeledSelect from '@shell/components/form/LabeledSelect';
import InputOrDisplay from '@shell/components/InputOrDisplay';
import { PVC } from '@shell/config/types';
import { HCI } from '../../../../types';
import { formatSi, parseSi } from '@shell/utils/units';
import { VOLUME_TYPE, InterfaceOption } from '../../../../config/harvester-map';
export default {
name: 'HarvesterEditVMImage',
components: {
UnitInput, LabeledInput, LabeledSelect, InputOrDisplay
},
props: {
value: {
type: Object,
default: () => {
return {};
}
},
namespace: {
type: String,
default: null
},
mode: {
type: String,
default: 'create'
},
idx: {
type: Number,
required: true
},
isCreate: {
type: Boolean,
default: true
},
isEdit: {
type: Boolean,
default: false
},
validateRequired: {
type: Boolean,
required: true
},
isVirtualType: {
type: Boolean,
default: true
}
},
data() {
return {
VOLUME_TYPE,
InterfaceOption,
loading: false,
images: [],
};
},
fetch() {
this.images = this.$store.getters['harvester/all'](HCI.IMAGE);
},
computed: {
imagesOption() {
return this.images.filter(c => c.isReady).sort((a, b) => a.creationTimestamp > b.creationTimestamp ? -1 : 1).map( (I) => {
return {
label: `${ I.metadata.namespace }/${ I.spec.displayName }`,
value: I.id
};
});
},
imageName() {
const image = this.imagesOption.find(I => I.value === this.value.image);
return image ? image.label : '-';
},
pvcsResource() {
const allPVCs = this.$store.getters['harvester/all'](PVC) || [];
return allPVCs.find((P) => {
return this.namespace ? P.id === `${ this.namespace }/${ this.value.volumeName }` : true;
});
}
},
watch: {
'value.type'(neu) {
if (neu === 'cd-rom') {
this.$set(this.value, 'bus', 'sata');
this.update();
}
},
'value.image'(neu) {
this.checkImageExists(neu);
},
imagesOption() {
this.checkImageExists(this.value.image);
},
pvcsResource: {
handler(pvc) {
if (pvc?.spec?.resources?.requests?.storage && this.isVirtualType) {
const parseValue = parseSi(pvc.spec.resources.requests.storage);
const formatSize = formatSi(parseValue, {
increment: 1024,
addSuffix: false,
maxExponent: 3,
minExponent: 3,
});
this.value.size = `${ formatSize }Gi`;
}
},
deep: true,
immediate: true
},
},
methods: {
update() {
this.$emit('update');
},
onImageChange() {
const imageResource = this.$store.getters['harvester/all'](HCI.IMAGE)?.find( I => this.value.image === I.id);
if (this.idx === 0) {
if (/iso$/i.test(imageResource?.imageSuffix)) {
this.$set(this.value, 'type', 'cd-rom');
this.$set(this.value, 'bus', 'sata');
} else {
this.$set(this.value, 'type', 'disk');
this.$set(this.value, 'bus', 'virtio');
}
}
this.update();
},
onOpen() {
this.images = this.$store.getters['harvester/all'](HCI.IMAGE);
},
checkImageExists(imageId) {
if (!!imageId && this.imagesOption.length > 0 && !findBy(this.imagesOption, 'value', imageId)) {
this.$store.dispatch('growl/error', {
title: this.$store.getters['i18n/t']('harvester.vmTemplate.tips.notExistImage.title', { name: imageId }),
message: this.$store.getters['i18n/t']('harvester.vmTemplate.tips.notExistImage.message')
}, { root: true });
this.$set(this.value, 'image', '');
}
}
}
};
</script>
<template>
<div>
<div class="row mb-20">
<div
data-testid="input-hevi-name"
class="col span-6"
>
<InputOrDisplay :name="t('harvester.fields.name')" :value="value.name" :mode="mode">
<LabeledInput
v-model="value.name"
:label="t('harvester.fields.name')"
required
:mode="mode"
@input="update"
/>
</InputOrDisplay>
</div>
<div
data-testid="input-hevi-type"
class="col span-6"
>
<InputOrDisplay
:name="t('harvester.fields.type')"
:value="value.type"
:mode="mode"
>
<LabeledSelect
v-model="value.type"
:label="t('harvester.fields.type')"
:options="VOLUME_TYPE"
:mode="mode"
@input="update"
/>
</InputOrDisplay>
</div>
</div>
<div class="row mb-20">
<div
data-testid="input-hevi-image"
class="col span-6"
>
<InputOrDisplay
:name="t('harvester.fields.image')"
:value="imageName"
:mode="mode"
>
<LabeledSelect
v-model="value.image"
:disabled="idx === 0 && !isCreate && !value.newCreateId && isVirtualType"
:label="t('harvester.fields.image')"
:options="imagesOption"
:mode="mode"
:searchable="true"
:required="validateRequired"
@input="onImageChange"
/>
</InputOrDisplay>
</div>
<div
data-testid="input-hevi-size"
class="col span-6"
>
<InputOrDisplay
:name="t('harvester.fields.size')"
:value="value.size"
:mode="mode"
>
<UnitInput
v-model="value.size"
v-int-number
:output-modifier="true"
:increment="1024"
:input-exponent="3"
:label="t('harvester.fields.size')"
:mode="mode"
:required="validateRequired"
suffix="GiB"
@input="update"
/>
</InputOrDisplay>
</div>
</div>
<div class="row mb-20">
<div
data-testid="input-hevi-bus"
class="col span-3"
>
<InputOrDisplay
:name="t('harvester.virtualMachine.volume.bus')"
:value="value.bus"
:mode="mode"
>
<LabeledSelect
v-model="value.bus"
:label="t('harvester.virtualMachine.volume.bus')"
:mode="mode"
:options="InterfaceOption"
@input="update"
/>
</InputOrDisplay>
</div>
</div>
</div>
</template>
@@ -0,0 +1,230 @@
<script>
import Loading from '@shell/components/Loading';
import UnitInput from '@shell/components/form/UnitInput';
import InputOrDisplay from '@shell/components/InputOrDisplay';
import { LabeledInput } from '@components/Form/LabeledInput';
import LabeledSelect from '@shell/components/form/LabeledSelect';
import { PVC, STORAGE_CLASS } from '@shell/config/types';
import { formatSi, parseSi } from '@shell/utils/units';
import { VOLUME_TYPE, InterfaceOption } from '../../../../config/harvester-map';
export default {
name: 'HarvesterEditVolume',
components: {
InputOrDisplay, Loading, LabeledInput, LabeledSelect, UnitInput,
},
props: {
mode: {
type: String,
default: 'create'
},
isEdit: {
type: Boolean,
default: false
},
namespace: {
type: String,
default: null
},
value: {
type: Object,
default: () => {
return {};
}
},
validateRequired: {
type: Boolean,
required: true
},
isVirtualType: {
type: Boolean,
default: true
}
},
data() {
return {
VOLUME_TYPE,
InterfaceOption,
loading: false,
};
},
computed: {
pvcsResource() {
const allPVCs = this.$store.getters['harvester/all'](PVC) || [];
return allPVCs.find(P => P.id === `${ this.namespace }/${ this.value.volumeName }`);
},
isDisabled() {
return !this.value.newCreateId && this.isEdit && this.isVirtualType;
},
storageClassOptions() {
const storages = this.$store.getters[`harvester/all`](STORAGE_CLASS) || [];
const out = storages.filter(s => !s.parameters?.backingImage).map((s) => {
const label = s.isDefault ? `${ s.name } (${ this.t('generic.default') })` : s.name;
return {
label,
value: s.name,
};
}) || [];
return out;
},
},
watch: {
'value.type'(neu) {
if (neu === 'cd-rom') {
this.$set(this.value, 'bus', 'sata');
this.update();
}
},
pvcsResource: {
handler(pvc) {
if (pvc?.spec?.resources?.requests?.storage) {
const parseValue = parseSi(pvc.spec.resources.requests.storage);
const formatSize = formatSi(parseValue, {
increment: 1024,
addSuffix: false,
maxExponent: 3,
minExponent: 3,
});
this.value.size = `${ formatSize }Gi`;
}
},
deep: true,
immediate: true
},
},
methods: {
update() {
this.$emit('update');
},
},
};
</script>
<template>
<div>
<Loading mode="relative" :loading="loading" />
<div class="row mb-20">
<div
class="col span-6"
data-testid="input-hev-name"
>
<InputOrDisplay
:name="t('harvester.fields.name')"
:value="value.name"
:mode="mode"
>
<LabeledInput
v-model="value.name"
:label="t('harvester.fields.name')"
:mode="mode"
required
@input="update"
/>
</InputOrDisplay>
</div>
<div
class="col span-6"
data-testid="input-hev-type"
>
<InputOrDisplay
:name="t('harvester.fields.type')"
:value="value.type"
:mode="mode"
>
<LabeledSelect
v-model="value.type"
:label="t('harvester.fields.type')"
:options="VOLUME_TYPE"
required
:mode="mode"
@input="update"
/>
</InputOrDisplay>
</div>
</div>
<div class="row mb-20">
<div
data-testid="input-hav-storage"
class="col span-6"
>
<InputOrDisplay
:name="t('harvester.storage.storageClass.label')"
:value="value.storageClassName"
:mode="mode"
>
<LabeledSelect
v-model="value.storageClassName"
:options="storageClassOptions"
:label="t('harvester.storage.storageClass.label')"
:mode="mode"
:disabled="isDisabled"
:required="validateRequired"
@input="update"
/>
</InputOrDisplay>
</div>
<div
class="col span-6"
data-testid="input-hev-size"
>
<InputOrDisplay
:name="t('harvester.fields.size')"
:value="value.size"
:mode="mode"
>
<UnitInput
v-model="value.size"
v-int-number
:output-modifier="true"
:increment="1024"
:input-exponent="3"
:mode="mode"
:required="validateRequired"
:label="t('harvester.fields.size')"
@input="update"
/>
</InputOrDisplay>
</div>
</div>
<div class="row mb-20">
<div
data-testid="input-hev-bus"
class="col span-3"
>
<InputOrDisplay :name="t('harvester.virtualMachine.volume.bus')" :value="value.bus" :mode="mode">
<LabeledSelect
v-model="value.bus"
:label="t('harvester.virtualMachine.volume.bus')"
:mode="mode"
:options="InterfaceOption"
required
@input="update"
/>
</InputOrDisplay>
</div>
</div>
</div>
</template>