From 79451a8d1d00bed1c0db8be02d66d5b81f09872a Mon Sep 17 00:00:00 2001 From: Jorge Miguel Lobo Escalona Date: Mon, 20 Nov 2023 10:32:29 +0100 Subject: [PATCH] B 6334: Fix inputs kernel and ramdisk (#2823) --- .../booting/kernelSchema.js | 45 +++++++-- .../booting/ramdiskSchema.js | 96 +++++++++++++++---- 2 files changed, 112 insertions(+), 29 deletions(-) diff --git a/src/fireedge/src/client/components/Forms/VmTemplate/CreateForm/Steps/ExtraConfiguration/booting/kernelSchema.js b/src/fireedge/src/client/components/Forms/VmTemplate/CreateForm/Steps/ExtraConfiguration/booting/kernelSchema.js index f3c4688b95..8106b88958 100644 --- a/src/fireedge/src/client/components/Forms/VmTemplate/CreateForm/Steps/ExtraConfiguration/booting/kernelSchema.js +++ b/src/fireedge/src/client/components/Forms/VmTemplate/CreateForm/Steps/ExtraConfiguration/booting/kernelSchema.js @@ -15,13 +15,17 @@ * ------------------------------------------------------------------------- */ import { boolean, string } from 'yup' -import { useGetImagesQuery } from 'client/features/OneApi/image' +import { HYPERVISORS, IMAGE_TYPES_STR, INPUT_TYPES, T } from 'client/constants' +import { useGetAllImagesQuery } from 'client/features/OneApi/image' import { getType } from 'client/models/Image' import { Field, clearNames } from 'client/utils' -import { T, INPUT_TYPES, HYPERVISORS, IMAGE_TYPES_STR } from 'client/constants' const { vcenter, lxc } = HYPERVISORS +export const KERNEL_PATH_ENABLED_NAME = 'OS.KERNEL_PATH_ENABLED' +export const KERNEL_DS_NAME = 'OS.KERNEL_DS' +export const KERNEL_NAME = 'OS.KERNEL' + const kernelValidation = string() .trim() .notRequired() @@ -29,10 +33,16 @@ const kernelValidation = string() /** @type {Field} Kernel path field */ export const KERNEL_PATH_ENABLED = { - name: 'OS.KERNEL_PATH_ENABLED', + name: KERNEL_PATH_ENABLED_NAME, label: T.CustomPath, notOnHypervisors: [vcenter, lxc], type: INPUT_TYPES.SWITCH, + fieldProps: (context, form) => { + if (context?.extra?.OS?.KERNEL) { + // first render! + form?.setValue(`extra.${KERNEL_PATH_ENABLED_NAME}`, true) + } + }, validation: boolean() .strip() .default(() => false), @@ -40,14 +50,15 @@ export const KERNEL_PATH_ENABLED = { /** @type {Field} Kernel DS field */ export const KERNEL_DS = { - name: 'OS.KERNEL_DS', + name: KERNEL_DS_NAME, label: T.Kernel, notOnHypervisors: [vcenter, lxc], type: INPUT_TYPES.AUTOCOMPLETE, dependOf: KERNEL_PATH_ENABLED.name, htmlType: (enabled) => enabled && INPUT_TYPES.HIDDEN, + fieldProps: (enabled) => (enabled ? { value: null } : {}), values: () => { - const { data: images = [] } = useGetImagesQuery() + const { data: images = [] } = useGetAllImagesQuery() return images ?.filter((image) => getType(image) === IMAGE_TYPES_STR.KERNEL) @@ -65,20 +76,34 @@ export const KERNEL_DS = { clearNames(KERNEL_PATH_ENABLED.name), (enabled, schema) => (enabled ? schema.strip() : schema) ), + value: (_, form) => { + if ( + form?.getValues(`extra.${KERNEL_PATH_ENABLED_NAME}`) && + form?.setValue + ) { + form?.setValue(`extra.${KERNEL_DS_NAME}`, undefined) + } + }, } /** @type {Field} Kernel path field */ export const KERNEL = { - name: 'OS.KERNEL', + name: KERNEL_NAME, label: T.KernelPath, notOnHypervisors: [vcenter, lxc], type: INPUT_TYPES.TEXT, dependOf: KERNEL_PATH_ENABLED.name, htmlType: (enabled) => !enabled && INPUT_TYPES.HIDDEN, - validation: kernelValidation.when( - clearNames(KERNEL_PATH_ENABLED.name), - (enabled, schema) => (enabled ? schema : schema.strip()) - ), + fieldProps: (enabled) => (!enabled ? { value: '' } : {}), + validation: kernelValidation, + value: (_, form) => { + const typeKernel = form?.getValues(`extra.${KERNEL_PATH_ENABLED_NAME}`) + const currentValue = form?.getValues(`extra.${KERNEL_NAME}`) + + if (typeKernel === false && currentValue && form?.setValue) { + form?.setValue(`extra.${KERNEL_NAME}`, '') + } + }, } /** @type {Field[]} List of Kernel fields */ diff --git a/src/fireedge/src/client/components/Forms/VmTemplate/CreateForm/Steps/ExtraConfiguration/booting/ramdiskSchema.js b/src/fireedge/src/client/components/Forms/VmTemplate/CreateForm/Steps/ExtraConfiguration/booting/ramdiskSchema.js index 1700065ff6..6d2599576e 100644 --- a/src/fireedge/src/client/components/Forms/VmTemplate/CreateForm/Steps/ExtraConfiguration/booting/ramdiskSchema.js +++ b/src/fireedge/src/client/components/Forms/VmTemplate/CreateForm/Steps/ExtraConfiguration/booting/ramdiskSchema.js @@ -13,14 +13,19 @@ * See the License for the specific language governing permissions and * * limitations under the License. * * ------------------------------------------------------------------------- */ -import { string, boolean } from 'yup' +import { boolean, string } from 'yup' -import { useGetImagesQuery } from 'client/features/OneApi/image' +import { HYPERVISORS, IMAGE_TYPES_STR, INPUT_TYPES, T } from 'client/constants' +import { useGetAllImagesQuery } from 'client/features/OneApi/image' import { getType } from 'client/models/Image' import { Field, clearNames } from 'client/utils' -import { T, INPUT_TYPES, HYPERVISORS, IMAGE_TYPES_STR } from 'client/constants' +import { KERNEL_DS_NAME, KERNEL_NAME } from './kernelSchema' -const { vcenter, lxc, firecracker } = HYPERVISORS +const { vcenter, lxc } = HYPERVISORS + +export const RAMDISK_PATH_ENABLED_NAME = 'OS.RAMDISK_PATH_ENABLED' +export const RAMDISK_DS_NAME = 'OS.INITRD_DS' +export const RAMDISK_NAME = 'OS.INITRD' const ramdiskValidation = string() .trim() @@ -29,10 +34,22 @@ const ramdiskValidation = string() /** @type {Field} Ramdisk path field */ export const RAMDISK_PATH_ENABLED = { - name: 'OS.RAMDISK_PATH_ENABLED', + name: RAMDISK_PATH_ENABLED_NAME, label: T.CustomPath, - notOnHypervisors: [vcenter, lxc, firecracker], + notOnHypervisors: [vcenter, lxc], type: INPUT_TYPES.SWITCH, + dependOf: [`$extra.${KERNEL_DS_NAME}`, `$extra.${KERNEL_NAME}`], + fieldProps: (_, form) => { + const ds = form?.getValues(`extra.${KERNEL_DS_NAME}`) + const path = form?.getValues(`extra.${KERNEL_NAME}`) + const ramdisk = form?.getValues(`extra.${RAMDISK_NAME}`) + const options = {} + + !(ds || path) && (options.disabled = true) + ramdisk && form?.setValue(`extra.${RAMDISK_PATH_ENABLED_NAME}`, true) + + return options + }, validation: boolean() .strip() .default(() => false), @@ -40,14 +57,30 @@ export const RAMDISK_PATH_ENABLED = { /** @type {Field} Ramdisk DS field */ export const RAMDISK_DS = { - name: 'OS.INITRD_DS', + name: RAMDISK_DS_NAME, label: T.Ramdisk, - notOnHypervisors: [vcenter, lxc, firecracker], + notOnHypervisors: [vcenter, lxc], type: INPUT_TYPES.AUTOCOMPLETE, - dependOf: RAMDISK_PATH_ENABLED.name, - htmlType: (enabled) => enabled && INPUT_TYPES.HIDDEN, + dependOf: [ + RAMDISK_PATH_ENABLED.name, + `$extra.${KERNEL_DS_NAME}`, + `$extra${KERNEL_NAME}`, + ], + htmlType: ([enabled = false] = []) => enabled && INPUT_TYPES.HIDDEN, + fieldProps: (_, form) => { + const ds = form?.getValues(`extra.${KERNEL_DS_NAME}`) + const path = form?.getValues(`extra.${KERNEL_NAME}`) + const ramdisk = form?.getValues(`extra.${RAMDISK_PATH_ENABLED_NAME}`) + + const options = {} + + !(ds || path) && (options.disabled = true) + ramdisk && (options.value = null) + + return options + }, values: () => { - const { data: images = [] } = useGetImagesQuery() + const { data: images = [] } = useGetAllImagesQuery() return images ?.filter((image) => getType(image) === IMAGE_TYPES_STR.RAMDISK) @@ -65,20 +98,45 @@ export const RAMDISK_DS = { clearNames(RAMDISK_PATH_ENABLED.name), (enabled, schema) => (enabled ? schema.strip() : schema) ), + value: (_, form) => { + if ( + form?.getValues(`extra.${RAMDISK_PATH_ENABLED_NAME}`) && + form?.setValue + ) { + form?.setValue(`extra.${RAMDISK_DS_NAME}`, undefined) + } + }, } /** @type {Field} Ramdisk path field */ export const RAMDISK = { - name: 'OS.INITRD', + name: RAMDISK_NAME, label: T.RamdiskPath, - notOnHypervisors: [vcenter, lxc, firecracker], + notOnHypervisors: [vcenter, lxc], type: INPUT_TYPES.TEXT, - dependOf: RAMDISK_PATH_ENABLED.name, - htmlType: (enabled) => !enabled && INPUT_TYPES.HIDDEN, - validation: ramdiskValidation.when( - clearNames(RAMDISK_PATH_ENABLED.name), - (enabled, schema) => (enabled ? schema : schema.strip()) - ), + dependOf: [ + RAMDISK_PATH_ENABLED.name, + `$extra.${KERNEL_DS_NAME}`, + `$extra${KERNEL_NAME}`, + ], + fieldProps: (_, form) => { + const ds = form?.getValues(`extra.${KERNEL_DS_NAME}`) + const path = form?.getValues(`extra.${KERNEL_NAME}`) + const ramdisk = form?.getValues(`extra.${RAMDISK_PATH_ENABLED_NAME}`) + const currentValue = form?.getValues(`extra.${RAMDISK_NAME}`) + + if ( + (ramdisk === false || !(ds || path)) && + currentValue && + form?.setValue + ) { + form?.setValue(`extra.${RAMDISK_NAME}`) + } + + return ds || path ? {} : { disabled: true } + }, + htmlType: ([enabled = false] = []) => !enabled && INPUT_TYPES.HIDDEN, + validation: ramdiskValidation, } /** @type {Field[]} List of Ramdisk fields */