Skip to content

Commit

Permalink
POC of better fix
Browse files Browse the repository at this point in the history
  • Loading branch information
bnussman committed Dec 20, 2024
1 parent 89536f9 commit 9bf1c3a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const AccessKeyRegions = (props: Props) => {
placeholder={
selectedRegion.length > 0 ? '' : 'Select regions or type to search'
}
currentCapability="Object Storage"
currentCapability={undefined} // filtering is handled by the `useObjectStorageRegions` hook
disabled={disabled}
errorText={errorText}
isClearable={false}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const BucketRegions = (props: Props) => {

return (
<RegionSelect
currentCapability="Object Storage"
currentCapability={undefined} // filtering is handled by the `useObjectStorageRegions` hook
disableClearable
disabled={disabled}
errorText={errorText}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { useObjectStorageEndpoints } from 'src/queries/object-storage/queries';
import { useRegionsQuery } from 'src/queries/regions/regions';
import { getRegionsByRegionId } from 'src/utilities/regions';

import { useIsObjectStorageGen2Enabled } from './useIsObjectStorageGen2Enabled';

export const useObjectStorageRegions = () => {
const { data: allRegions, error: allRegionsError } = useRegionsQuery();
const {
Expand All @@ -13,9 +15,16 @@ export const useObjectStorageRegions = () => {
isFetching: isStorageEndpointsLoading,
} = useObjectStorageEndpoints();

const { isObjectStorageGen2Enabled } = useIsObjectStorageGen2Enabled();

const availableStorageRegions = React.useMemo(
() => filterRegionsByEndpoints(allRegions, storageEndpoints),
[allRegions, storageEndpoints]
() =>
filterRegionsByEndpoints(
allRegions,
storageEndpoints,
isObjectStorageGen2Enabled
),
[allRegions, storageEndpoints, isObjectStorageGen2Enabled]
);

const regionsByIdMap =
Expand Down
36 changes: 30 additions & 6 deletions packages/manager/src/features/ObjectStorage/utilities.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { OBJECT_STORAGE_DELIMITER } from 'src/constants';

import type { Region } from '@linode/api-v4';
import type { AccountSettings } from '@linode/api-v4/lib/account';
import type {
ACLType,
Expand Down Expand Up @@ -172,25 +173,48 @@ export const objectACLHelperText: Record<ACLType, string> = {
'public-read-write': 'Public Read/Write ACL',
};

// @TODO: OBJ Gen2: This should be removed once these regions obtain the `Object Storage` capability.
const WHITELISTED_REGIONS = new Set([
'gb-lon',
'au-mel',
'in-bom-2',
'de-fra-2',
'sg-sin-2',
]);

/**
* For OBJ Gen2 users, filter regions based on available Object Storage endpoints.
* Otherwise, we return the regions as is.
*/
export const filterRegionsByEndpoints = <T extends { id: string }>(
regions: T[] | undefined,
objecStorageEndpoints: ObjectStorageEndpoint[] | undefined
): T[] => {
export const filterRegionsByEndpoints = (
regions: Region[] | undefined,
objecStorageEndpoints: ObjectStorageEndpoint[] | undefined,
isObjectStorageGen2Enabled: boolean
): Region[] => {
if (!regions) {
return [];
}

const regionsWithObjectStorage = regions.filter((r) => {
if (isObjectStorageGen2Enabled) {
// If Object Storage Gen2 is enabled, we need to include WHITELISTED_REGIONS
return (
r.capabilities.includes('Object Storage') ||
WHITELISTED_REGIONS.has(r.id)
);
}
return r.capabilities.includes('Object Storage');
});

if (!objecStorageEndpoints) {
return regions;
return regionsWithObjectStorage;
}

const endpointRegions = new Set(
objecStorageEndpoints.map((endpoint) => endpoint.region)
);

return regions.filter((region) => endpointRegions.has(region.id));
return regionsWithObjectStorage.filter((region) =>
endpointRegions.has(region.id)
);
};

0 comments on commit 9bf1c3a

Please sign in to comment.