Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent custom config keys being removed when updating resources [WD-9348] #691

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/pages/storage/forms/StorageVolumeEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const StorageVolumeEdit: FC<Props> = ({ volume }) => {
initialValues: getStorageVolumeEditValues(volume),
validationSchema: StorageVolumeSchema,
onSubmit: (values) => {
const saveVolume = volumeFormToPayload(values, project);
const saveVolume = volumeFormToPayload(values, project, volume);
updateStorageVolume(values.pool, project, {
...saveVolume,
etag: volume.etag,
Expand Down
21 changes: 20 additions & 1 deletion src/pages/storage/forms/StorageVolumeForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import StorageVolumeFormZFS from "pages/storage/forms/StorageVolumeFormZFS";
import { FormikProps } from "formik/dist/types";
import {
getFilesystemVolumeFormFields,
getVolumeConfigKeys,
getVolumeKey,
getZfsVolumeFormFields,
} from "util/storageVolume";
Expand All @@ -29,6 +30,7 @@ import {
} from "types/storage";
import { slugify } from "util/slugify";
import { driversWithFilesystemSupport } from "util/storageOptions";
import { getUnhandledKeyValues } from "util/formFields";

export interface StorageVolumeFormValues {
name: string;
Expand Down Expand Up @@ -59,9 +61,14 @@ export interface StorageVolumeFormValues {
export const volumeFormToPayload = (
values: StorageVolumeFormValues,
project: string,
volume?: LxdStorageVolume,
): LxdStorageVolume => {
const hasValidSize = values.size?.match(/^\d/);
return {
const unhandledVolumeConfigs = getUnhandledKeyValues(
volume?.config || {},
getVolumeConfigKeys(),
);
const payload = {
name: values.name,
config: {
size: hasValidSize ? values.size : undefined,
Expand All @@ -79,6 +86,7 @@ export const volumeFormToPayload = (
[getVolumeKey("zfs_remove_snapshots")]: values.zfs_remove_snapshots,
[getVolumeKey("zfs_use_refquota")]: values.zfs_use_refquota,
[getVolumeKey("zfs_reserve_space")]: values.zfs_reserve_space,
...unhandledVolumeConfigs,
},
project,
edlerd marked this conversation as resolved.
Show resolved Hide resolved
type: values.volumeType,
Expand All @@ -88,6 +96,17 @@ export const volumeFormToPayload = (
created_at: "",
pool: values.pool,
};

const allPayloadKeys = Object.keys(payload);
const unhandledVolumeMainConfigs = getUnhandledKeyValues(
volume || {},
new Set(allPayloadKeys),
);

return {
...payload,
...unhandledVolumeMainConfigs,
};
};

export const getFormProps = (
Expand Down
4 changes: 3 additions & 1 deletion src/types/storage.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { LxdConfigPair } from "./config";

export interface LxdStoragePool {
config: {
size?: string;
Expand Down Expand Up @@ -38,7 +40,7 @@ export interface LxdStorageVolume {
"zfs.use_refquota"?: string;
"zfs.reserve_space"?: string;
size?: string;
};
} & LxdConfigPair;
content_type: LxdStorageVolumeContentType;
created_at: string;
description: string;
Expand Down
8 changes: 7 additions & 1 deletion src/util/formFields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ import { LxdInstance } from "types/instance";
import { OptionHTMLAttributes } from "react";
import { LxdConfigPair } from "types/config";
import { LxdProject } from "types/project";
import { LxdStorageVolume } from "types/storage";

export const getUnhandledKeyValues = (
item: LxdConfigPair | LxdInstance | LxdProfile | LxdProject,
item:
| LxdConfigPair
| LxdInstance
| LxdProfile
| LxdProject
| LxdStorageVolume,
handledKeys: Set<string>,
) => {
return Object.fromEntries(
Expand Down
7 changes: 6 additions & 1 deletion src/util/storageVolume.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const testDuplicateStorageVolumeName = (
};

const storageVolumeFormFieldToPayloadName: Record<string, string> = {
size: "size",
security_shifted: "security.shifted",
security_unmapped: "security.unmapped",
snapshots_expiry: "snapshots.expiry",
Expand Down Expand Up @@ -62,12 +63,16 @@ export const getZfsVolumeFormFields = (): (keyof StorageVolumeFormValues)[] => {
};

export const getVolumeKey = (key: string): string => {
if (Object.keys(storageVolumeFormFieldToPayloadName).includes(key)) {
if (key in storageVolumeFormFieldToPayloadName) {
edlerd marked this conversation as resolved.
Show resolved Hide resolved
return storageVolumeFormFieldToPayloadName[key];
}
return key;
};

export const getVolumeConfigKeys = (): Set<string> => {
return new Set(Object.values(storageVolumeFormFieldToPayloadName));
};

export const renderVolumeType = (volume: LxdStorageVolume): string => {
return volume.type === "virtual-machine"
? "VM"
Expand Down
Loading