Skip to content

Commit

Permalink
feat(volumes) use fetch all volumes endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: David Edler <david.edler@canonical.com>
  • Loading branch information
edlerd committed Mar 28, 2024
1 parent 47c1f06 commit 34b6155
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 26 deletions.
13 changes: 13 additions & 0 deletions src/api/storage-pools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,19 @@ export const fetchStorageVolumes = (
});
};

export const fetchAllStorageVolumes = (
project: string,
): Promise<LxdStorageVolume[]> => {
return new Promise((resolve, reject) => {
fetch(`/1.0/storage-volumes?recursion=1&project=${project}`)
.then(handleResponse)
.then((data: LxdApiResponse<LxdStorageVolume[]>) =>
resolve(data.metadata),
)
.catch(reject);
});
};

export const fetchStorageVolume = (
pool: string,
project: string,
Expand Down
26 changes: 2 additions & 24 deletions src/context/loadIsoVolumes.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { fetchStoragePools, fetchStorageVolumes } from "api/storage-pools";
import { fetchAllStorageVolumes } from "api/storage-pools";
import { isoToRemoteImage } from "util/images";
import { RemoteImage } from "types/image";
import { LxdStorageVolume } from "types/storage";

export const loadIsoVolumes = async (
project: string,
): Promise<RemoteImage[]> => {
const remoteImages: RemoteImage[] = [];
const allVolumes = await loadVolumes(project);
const allVolumes = await fetchAllStorageVolumes(project);
allVolumes.forEach((volume) => {
if (volume.content_type === "iso") {
const image = isoToRemoteImage(volume);
Expand All @@ -17,24 +16,3 @@ export const loadIsoVolumes = async (

return remoteImages;
};

export const loadVolumes = async (
project: string,
): Promise<LxdStorageVolume[]> => {
const allVolumes: LxdStorageVolume[] = [];
const pools = await fetchStoragePools(project);

const poolVolumes = await Promise.allSettled(
pools.map(async (pool) => fetchStorageVolumes(pool.name, project)),
);

poolVolumes.forEach((result) => {
if (result.status === "fulfilled") {
allVolumes.push(...result.value);
} else {
throw new Error("Failed to load iso images");
}
});

return allVolumes;
};
4 changes: 2 additions & 2 deletions src/pages/storage/StorageVolumes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
} from "@canonical/react-components";
import Loader from "components/Loader";
import { isoTimeToString } from "util/helpers";
import { loadVolumes } from "context/loadIsoVolumes";
import ScrollableTable from "components/ScrollableTable";
import CreateVolumeBtn from "pages/storage/actions/CreateVolumeBtn";
import StorageVolumesFilter, {
Expand Down Expand Up @@ -49,6 +48,7 @@ import useEventListener from "@use-it/event-listener";
import { useProject } from "context/project";
import { isSnapshotsDisabled } from "util/snapshots";
import useSortTableData from "util/useSortTableData";
import { fetchAllStorageVolumes } from "api/storage-pools";

const StorageVolumes: FC = () => {
const docBaseLink = useDocs();
Expand Down Expand Up @@ -84,7 +84,7 @@ const StorageVolumes: FC = () => {
isLoading,
} = useQuery({
queryKey: [queryKeys.volumes, project],
queryFn: () => loadVolumes(project),
queryFn: () => fetchAllStorageVolumes(project),
});

if (error) {
Expand Down

0 comments on commit 34b6155

Please sign in to comment.