Skip to content

Commit

Permalink
feat(pci-block-storage): add extension for volume size
Browse files Browse the repository at this point in the history
ref: TAPC-1609

Signed-off-by: Simon Chaumet <[email protected]>
  • Loading branch information
SimonChaumet committed Oct 8, 2024
1 parent 66217b4 commit 66ea710
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 14 deletions.
32 changes: 32 additions & 0 deletions packages/manager/apps/pci-block-storage/src/api/data/quota.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { v6 } from '@ovh-ux/manager-core-api';
import { useFeatureAvailability } from '@ovh-ux/manager-react-components';
import { useMemo } from 'react';

export interface RegionQuota {
region: string;
Expand All @@ -18,3 +20,33 @@ export const getRegionsQuota = async (
);
return data;
};

function isLocalZone(region: string) {
return region.split('-')[2] === 'LZ';
}

function getVolumeMaxSize(region: string) {
return isLocalZone(region) ? 4 * 1000 : 12 * 1000;
}

export const FA_VOLUME_EXTEND_12TO = 'pci-block-storage:volume-extend-12To';
export const FA_EXTEN_BANNER = 'pci-block-storage:exten-banner';

export function useVolumeMaxSize(region: string) {
const { data, ...restApi } = useFeatureAvailability([
FA_VOLUME_EXTEND_12TO,
FA_EXTEN_BANNER,
]);

const volumeMaxSize = useMemo(() => {
if (data && data[FA_VOLUME_EXTEND_12TO]) {
return getVolumeMaxSize(region);
}
return 4 * 1000;
}, [data]);

return {
...restApi,
volumeMaxSize,
};
}
1 change: 0 additions & 1 deletion packages/manager/apps/pci-block-storage/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export const URL_INFO = {
LOCAL_ZONE: LOCAL_ZONE_INFO_URL,
};

export const VOLUME_MAX_SIZE = 4 * 1000; // Should be 10 * 1024 (but API is wrong;
export const VOLUME_MIN_SIZE = 10; // 10 Gio
export const VOLUME_UNLIMITED_QUOTA = -1; // Should be 10 * 1024 (but API is wrong)
export const ALPHA_CHARACTERS_REGEX = /^[a-zA-Z-]+$/;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ import {
OsdsText,
} from '@ovhcloud/ods-components/react';
import { useProject } from '@ovh-ux/manager-pci-common';
import {
VOLUME_MAX_SIZE,
VOLUME_MIN_SIZE,
VOLUME_UNLIMITED_QUOTA,
} from '@/constants';
import { VOLUME_MIN_SIZE, VOLUME_UNLIMITED_QUOTA } from '@/constants';
import ChipRegion from '@/components/edit/ChipRegion.component';
import { TVolume } from '@/api/data/volume';
import {
Expand All @@ -51,6 +47,7 @@ import {
useVolume,
} from '@/api/hooks/useVolume';
import HidePreloader from '@/core/HidePreloader';
import { useVolumeMaxSize } from '@/api/data/quota';

type TFormState = {
name: string;
Expand Down Expand Up @@ -95,6 +92,8 @@ export default function EditPage() {
isPending: isPendingVolume,
} = useVolume(projectId, volumeId);

const { volumeMaxSize } = useVolumeMaxSize(volume?.region);

const {
data: localRegions,
isPending: isPendingLocal,
Expand All @@ -106,7 +105,7 @@ export default function EditPage() {
size: {
value: volume?.size || VOLUME_MIN_SIZE,
min: VOLUME_MIN_SIZE,
max: VOLUME_MAX_SIZE,
max: volumeMaxSize,
},
bootable: volume?.bootable || false,
isInitialized: false,
Expand Down Expand Up @@ -167,10 +166,10 @@ export default function EditPage() {
) {
const availableGigabytes =
regionQuota.volume.maxGigabytes - regionQuota.volume.usedGigabytes;
return Math.min(_volume.size + availableGigabytes, VOLUME_MAX_SIZE);
return Math.min(_volume.size + availableGigabytes, volumeMaxSize);
}
}
return VOLUME_MAX_SIZE;
return volumeMaxSize;
};

const getVolumePriceEstimationFromCatalog = (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from 'react';
import { useEffect, useMemo, useState } from 'react';
import {
OsdsButton,
OsdsIcon,
Expand Down Expand Up @@ -28,8 +28,8 @@ import { HighSpeedV2Infos } from '@/pages/new/components/HighSpeedV2Infos';
import { TLocalisation } from '@/api/hooks/useRegions';
import { StepState } from '@/pages/new/hooks/useStep';
import { useRegionsQuota } from '@/api/hooks/useQuota';
import { useVolumeMaxSize } from '@/api/data/quota';

export const VOLUME_MAX_SIZE = 4 * 1000; // Should be 10 * 1024 (but API is wrong;
export const VOLUME_MIN_SIZE = 10; // 10 Gio
export const VOLUME_UNLIMITED_QUOTA = -1; // Should be 10 * 1024 (but API is wrong)

Expand All @@ -53,20 +53,29 @@ export function CapacityStep({
const { t: tGlobal } = useTranslation('global');
const [volumeCapacity, setVolumeCapacity] = useState<number>(VOLUME_MIN_SIZE);
const [maxSize, setMaxSize] = useState(0);
const { data: regionQuotas, isLoading } = useRegionsQuota(projectId);
const {
data: regionQuotas,
isLoading: isRegionQuotaLoading,
} = useRegionsQuota(projectId);
const isCapacityValid =
volumeCapacity >= VOLUME_MIN_SIZE && volumeCapacity <= maxSize;

const { volumeMaxSize, isLoading: isVolumeMaxSizeLoading } = useVolumeMaxSize(
region.name,
);

const isLoading = isRegionQuotaLoading && isVolumeMaxSizeLoading;

useEffect(() => {
if (!isLoading) {
const quota = regionQuotas?.find(({ region: r }) => r === region?.name);
let availableGigabytes = VOLUME_MAX_SIZE;
let availableGigabytes = volumeMaxSize;
if (
quota?.volume &&
quota.volume.maxGigabytes !== VOLUME_UNLIMITED_QUOTA
) {
availableGigabytes = Math.min(
VOLUME_MAX_SIZE,
volumeMaxSize,
quota.volume.maxGigabytes - quota.volume.usedGigabytes,
);
}
Expand Down

0 comments on commit 66ea710

Please sign in to comment.