forked from kubevirt-ui/kubevirt-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kubevirt-ui#2149 from metalice/CNV-46825-change-it-1
CNV-46825: Editing Instancetype series and size
- Loading branch information
Showing
13 changed files
with
360 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
src/utils/components/InstanceTypeModal/InstanceTypeModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import React, { FC, useMemo, useState } from 'react'; | ||
|
||
import { V1VirtualMachine } from '@kubevirt-ui/kubevirt-api/kubevirt'; | ||
import { useKubevirtTranslation } from '@kubevirt-utils/hooks/useKubevirtTranslation'; | ||
import { Flex, FlexItem, SelectList, SelectOption, Text } from '@patternfly/react-core'; | ||
import { InstanceTypeUnion } from '@virtualmachines/details/tabs/configuration/utils/types'; | ||
|
||
import FormPFSelect from '../FormPFSelect/FormPFSelect'; | ||
import TabModal from '../TabModal/TabModal'; | ||
|
||
import { | ||
getInstanceTypeFromSeriesAndSize, | ||
getInstanceTypeSeriesAndSize, | ||
getInstanceTypeSeriesDisplayName, | ||
getInstanceTypesPrettyDisplaySize, | ||
getInstanceTypesSizes, | ||
mappedInstanceTypesToSelectOptions, | ||
} from './utils/util'; | ||
|
||
type InstanceTypeModalProps = { | ||
allInstanceTypes: InstanceTypeUnion[]; | ||
instanceType: InstanceTypeUnion; | ||
instanceTypeVM: V1VirtualMachine; | ||
isOpen: boolean; | ||
onClose: () => void; | ||
onSubmit: ( | ||
updatedVM: V1VirtualMachine, | ||
instanceType: InstanceTypeUnion, | ||
) => Promise<V1VirtualMachine>; | ||
}; | ||
|
||
const InstanceTypeModal: FC<InstanceTypeModalProps> = ({ | ||
allInstanceTypes, | ||
instanceType, | ||
instanceTypeVM, | ||
isOpen, | ||
onClose, | ||
onSubmit, | ||
}) => { | ||
const { t } = useKubevirtTranslation(); | ||
const mappedInstanceTypes = useMemo( | ||
() => mappedInstanceTypesToSelectOptions(allInstanceTypes), | ||
[allInstanceTypes], | ||
); | ||
const { series: instanceTypeSeries, size: instanceTypeSize } = useMemo( | ||
() => getInstanceTypeSeriesAndSize(instanceType), | ||
[instanceType], | ||
); | ||
|
||
const [series, setSeries] = useState<string>( | ||
getInstanceTypeSeriesDisplayName(mappedInstanceTypes, instanceTypeSeries), | ||
); | ||
|
||
const [size, setSize] = useState<string>( | ||
getInstanceTypesPrettyDisplaySize(mappedInstanceTypes, instanceTypeSeries, instanceTypeSize), | ||
); | ||
|
||
const handleSubmit = (selectedInstanceType: InstanceTypeUnion) => | ||
onSubmit(instanceTypeVM, selectedInstanceType); | ||
|
||
return ( | ||
<TabModal | ||
headerText={t('Edit Instancetype')} | ||
isOpen={isOpen} | ||
obj={getInstanceTypeFromSeriesAndSize(mappedInstanceTypes, series, size)} | ||
onClose={onClose} | ||
onSubmit={handleSubmit} | ||
> | ||
<Flex | ||
spaceItems={{ | ||
default: 'spaceItemsXl', | ||
}} | ||
direction={{ default: 'column' }} | ||
spacer={{ default: 'spacer4xl' }} | ||
> | ||
<FlexItem> | ||
<Text component="h5">{t('Series')}</Text> | ||
<FormPFSelect | ||
onSelect={(_, value) => { | ||
if (value !== series) { | ||
setSeries(value as string); | ||
setSize(null); | ||
} | ||
}} | ||
selected={series} | ||
toggleProps={{ isFullWidth: true }} | ||
> | ||
<SelectList> | ||
{Object.entries(mappedInstanceTypes).map(([key, value]) => ( | ||
<SelectOption | ||
description={value.descriptionSeries} | ||
key={key} | ||
value={value.displayNameSeries} | ||
> | ||
{value.displayNameSeries} | ||
</SelectOption> | ||
))} | ||
</SelectList> | ||
</FormPFSelect> | ||
</FlexItem> | ||
<FlexItem> | ||
<Text component="h5">{t('Size')}</Text> | ||
<FormPFSelect | ||
onSelect={(_, value) => { | ||
setSize(value as string); | ||
}} | ||
selected={size} | ||
toggleProps={{ isFullWidth: true }} | ||
> | ||
{getInstanceTypesSizes(mappedInstanceTypes, series)?.map((item) => ( | ||
<SelectOption key={item.prettyDisplaySize} value={item.prettyDisplaySize}> | ||
{item?.prettyDisplaySize} | ||
</SelectOption> | ||
))} | ||
</FormPFSelect> | ||
</FlexItem> | ||
</Flex> | ||
</TabModal> | ||
); | ||
}; | ||
|
||
export default InstanceTypeModal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { InstanceTypeUnion } from '@virtualmachines/details/tabs/configuration/utils/types'; | ||
|
||
export type InstanceTypesSeries = | ||
| 'cx1' | ||
| 'gn1' | ||
| 'highperformance' | ||
| 'm1' | ||
| 'n1' | ||
| 'o1' | ||
| 'rt1' | ||
| 'u1'; | ||
|
||
export type InstanceTypesSizes = | ||
| '2xlarge' | ||
| '4xlarge' | ||
| '8xlarge' | ||
| 'large' | ||
| 'medium' | ||
| 'micro' | ||
| 'nano' | ||
| 'small' | ||
| 'xlarge'; | ||
|
||
export type MappedInstanceTypes = Record< | ||
InstanceTypesSeries, | ||
{ | ||
sizes: { | ||
[key in InstanceTypesSizes]?: { | ||
instanceType: InstanceTypeUnion; | ||
prettyDisplaySize: string; | ||
series: string; | ||
seriesDisplayName: string; | ||
size: string; | ||
}; | ||
}; | ||
} & { descriptionSeries?: string; displayNameSeries?: string } | ||
>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { InstanceTypeSize } from '@catalog/CreateFromInstanceTypes/components/SelectInstanceTypeSection/utils/types'; | ||
import { | ||
INSTANCETYPE_CLASS_DISPLAY_NAME, | ||
INSTANCETYPE_DESCRIPTION_ANNOTATION, | ||
REDHAT_COM, | ||
} from '@kubevirt-utils/components/AddBootableVolumeModal/components/VolumeMetadata/components/InstanceTypeDrilldownSelect/utils/constants'; | ||
import { VENDOR_LABEL } from '@kubevirt-utils/constants/constants'; | ||
import { t } from '@kubevirt-utils/hooks/useKubevirtTranslation'; | ||
import { getAnnotation, getLabel } from '@kubevirt-utils/resources/shared'; | ||
import { InstanceTypeUnion } from '@virtualmachines/details/tabs/configuration/utils/types'; | ||
|
||
import { InstanceTypesSeries, InstanceTypesSizes, MappedInstanceTypes } from './types'; | ||
|
||
export const getInstanceTypeItemSizePrettyDisplay = (it: InstanceTypeUnion): string => | ||
`${it?.metadata.name.split('.').pop()}: ${it?.spec?.cpu?.guest} ${t('CPUs')}, ${ | ||
it?.spec?.memory?.guest | ||
} ${t('Memory')}`; | ||
|
||
export const getInstanceTypeClassDisplayAnnotation = (instanceType: InstanceTypeUnion): string => { | ||
return getAnnotation(instanceType, INSTANCETYPE_CLASS_DISPLAY_NAME); | ||
}; | ||
|
||
export const getInstanceTypeDescriptionAnnotation = (instanceType: InstanceTypeUnion): string => { | ||
return getAnnotation(instanceType, INSTANCETYPE_DESCRIPTION_ANNOTATION); | ||
}; | ||
|
||
export const getInstanceTypeSeriesAndSize = ( | ||
instanceType: InstanceTypeUnion, | ||
): { series: InstanceTypesSeries; size: InstanceTypesSizes } => { | ||
const [series, size] = instanceType?.metadata?.name?.split('.'); | ||
return { series: series as InstanceTypesSeries, size: size as InstanceTypesSizes }; | ||
}; | ||
|
||
export const mappedInstanceTypesToSelectOptions = ( | ||
instanceTypes: InstanceTypeUnion[], | ||
): MappedInstanceTypes => | ||
instanceTypes.reduce((acc, it) => { | ||
if (getLabel(it, VENDOR_LABEL) === REDHAT_COM) { | ||
const { series, size } = getInstanceTypeSeriesAndSize(it); | ||
acc[series] = { | ||
...(acc[series] || {}), | ||
descriptionSeries: getInstanceTypeDescriptionAnnotation(it), | ||
displayNameSeries: getInstanceTypeClassDisplayAnnotation(it), | ||
sizes: { | ||
...(acc?.[series]?.sizes || {}), | ||
[size]: { | ||
instanceType: it, | ||
prettyDisplaySize: getInstanceTypeItemSizePrettyDisplay(it), | ||
series, | ||
seriesDisplayName: getInstanceTypeClassDisplayAnnotation(it), | ||
size, | ||
}, | ||
}, | ||
}; | ||
} | ||
return acc; | ||
}, {} as MappedInstanceTypes); | ||
|
||
export const getInstanceTypesPrettyDisplaySize = ( | ||
mappedInstanceTypes: MappedInstanceTypes, | ||
instanceTypeSeries: InstanceTypesSeries, | ||
instanceTypeSize: InstanceTypeSize, | ||
) => mappedInstanceTypes?.[instanceTypeSeries]?.sizes[instanceTypeSize]?.prettyDisplaySize; | ||
|
||
export const getInstanceTypesSizes = (mappedInstanceTypes: MappedInstanceTypes, series: string) => { | ||
const matchedSeries = Object.values(mappedInstanceTypes).find( | ||
(it) => it.displayNameSeries === series, | ||
); | ||
return Object.values(matchedSeries?.sizes); | ||
}; | ||
|
||
export const getInstanceTypeSeriesDisplayName = ( | ||
mappedInstanceTypes: MappedInstanceTypes, | ||
instanceTypeSeries: InstanceTypesSeries, | ||
) => mappedInstanceTypes?.[instanceTypeSeries]?.displayNameSeries; | ||
|
||
export const getInstanceTypeFromSeriesAndSize = ( | ||
mappedInstanceTypes: MappedInstanceTypes, | ||
instanceTypeSeries: string, | ||
instanceTypeSize: string, | ||
): InstanceTypeUnion => { | ||
const instanceTypesSeries = Object.values(mappedInstanceTypes); | ||
|
||
const matchedSeries = instanceTypesSeries.find( | ||
(series) => series.displayNameSeries === instanceTypeSeries, | ||
); | ||
const matchedSize = Object.values(matchedSeries?.sizes).find( | ||
(size) => size.prettyDisplaySize === instanceTypeSize, | ||
); | ||
|
||
return matchedSize?.instanceType; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.