Skip to content

Commit

Permalink
DEVPROD-12004: Send entire public key document when editing spawn host (
Browse files Browse the repository at this point in the history
  • Loading branch information
minnakt authored Nov 8, 2024
1 parent 291d111 commit 6ae4f90
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 48 deletions.
4 changes: 4 additions & 0 deletions apps/spruce/src/components/Spawn/editHostModal/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export const computeDiff = (
);
}

if (mutationParams.publicKey) {
mutationParams.publicKey = currEditState.publicKey;
}

// Always overwrite the whole sleep schedule, no need to update on a per-field basis.
if (mutationParams.sleepSchedule) {
mutationParams.sleepSchedule = currEditState.sleepSchedule;
Expand Down
2 changes: 1 addition & 1 deletion apps/spruce/src/components/Spawn/utils/hostUptime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export const getHostUptimeWarnings = ({
* @returns - object with enabledHoursCount indicating total hours per week and enabledWeekdaysCount indicating number of days per week
*/
export const getEnabledHoursCount = (
hostUptime: HostUptime,
hostUptime?: HostUptime,
): { enabledHoursCount: number; enabledWeekdaysCount: number } => {
if (!hostUptime) {
return {
Expand Down
51 changes: 22 additions & 29 deletions apps/spruce/src/pages/spawn/spawnHost/EditSpawnHostModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ export const EditSpawnHostModal: React.FC<EditSpawnHostModalProps> = ({
const {
disableExpirationCheckbox,
instanceTypesData,
noExpirationCheckboxTooltip,
noExpirationCheckboxTooltip = "",
publicKeysData,
volumesData,
} = useLoadFormData(host);

let instanceTypes = instanceTypesData?.instanceTypes ?? [];
// The list of instance types provided by Evergreen can be out-of-date, so make sure the instance type in use is considered valid by RJSF
// @ts-expect-error: FIXME. This comment was added by an automated script.
if (!instanceTypes.includes(host.instanceType)) {
// @ts-expect-error: FIXME. This comment was added by an automated script.

// The list of instance types provided by Evergreen can be out-of-date,
// so make sure the instance type in use is considered valid by RJSF.
if (host.instanceType && !instanceTypes.includes(host.instanceType)) {
instanceTypes = [...instanceTypes, host.instanceType];
}

Expand All @@ -76,7 +76,7 @@ export const EditSpawnHostModal: React.FC<EditSpawnHostModalProps> = ({
rdpPassword: "",
userTags,
expirationDetails: {
expiration: host.expiration ? host.expiration.toString() : null,
expiration: host.expiration ? host.expiration.toString() : undefined,
noExpiration: host.noExpiration,
hostUptime:
host?.sleepSchedule && !isNullSleepSchedule(host?.sleepSchedule)
Expand All @@ -86,56 +86,49 @@ export const EditSpawnHostModal: React.FC<EditSpawnHostModalProps> = ({
publicKeySection: { useExisting: true, publicKeyNameDropdown: "" },
};

// @ts-expect-error: FIXME. This comment was added by an automated script.
const [formState, setFormState] = useState<FormState>(initialFormState);
const [hasError, setHasError] = useState(false);

const hostUptimeWarnings = useMemo(() => {
const { enabledHoursCount, enabledWeekdaysCount } = getEnabledHoursCount(
// @ts-expect-error: FIXME. This comment was added by an automated script.
formState?.expirationDetails?.hostUptime,
);
const warnings = getHostUptimeWarnings({
enabledHoursCount,
enabledWeekdaysCount,
// @ts-expect-error: FIXME. This comment was added by an automated script.
runContinuously:
formState?.expirationDetails?.hostUptime?.sleepSchedule?.timeSelection
?.runContinuously,
?.runContinuously ?? false,
});
return { enabledHoursCount, warnings };
}, [formState?.expirationDetails?.hostUptime]);

const { schema, uiSchema } = getFormSchema({
canEditInstanceType: host.status === HostStatus.Stopped,
// @ts-expect-error: FIXME. This comment was added by an automated script.
canEditRdpPassword:
// @ts-expect-error: FIXME. This comment was added by an automated script.
host.distro.isWindows && host.status === HostStatus.Running,
(host?.distro?.isWindows && host.status === HostStatus.Running) ?? false,
canEditSshKeys: host.status === HostStatus.Running,
disableExpirationCheckbox,
hostUptimeWarnings,
instanceTypes: instanceTypes ?? [],
myPublicKeys: publicKeys ?? [],
// @ts-expect-error: FIXME. This comment was added by an automated script.
noExpirationCheckboxTooltip,
permanentlyExempt: !!host.sleepSchedule?.permanentlyExempt,
timeZone:
formState?.expirationDetails?.hostUptime?.details?.timeZone || timeZone,
volumes,
});

// EDIT HOST MUTATION
const [editSpawnHostMutation, { loading: loadingSpawnHost }] = useMutation<
EditSpawnHostMutation,
EditSpawnHostMutationVariables
>(EDIT_SPAWN_HOST, {
onCompleted(mutationResult) {
onCompleted: (mutationResult) => {
const { id } = mutationResult?.editSpawnHost ?? {};
dispatchToast.success(`Successfully modified spawned host ${id}`);
onCancel();
},
onError(err) {
onError: (err) => {
dispatchToast.error(
`There was an error while modifying your host: ${err.message}`,
);
Expand All @@ -145,12 +138,10 @@ export const EditSpawnHostModal: React.FC<EditSpawnHostModalProps> = ({
});

const initialEditState = formToGql({
// @ts-expect-error: FIXME. This comment was added by an automated script.
formData: initialFormState,
hostId: host.id,
myPublicKeys: publicKeys,
oldUserTags: userTags,
timeZone,
});

const currEditState = formToGql({
Expand All @@ -171,25 +162,27 @@ export const EditSpawnHostModal: React.FC<EditSpawnHostModalProps> = ({
});
editSpawnHostMutation({
variables: {
// @ts-expect-error: FIXME. This comment was added by an automated script.
hostId: host.id,
...mutationParams,
hostId: host.id,
},
});
};

return (
<ConfirmationModal
buttonText={loadingSpawnHost ? "Saving" : "Save"}
data-cy="edit-spawn-host-modal"
onCancel={() => {
onCancel();
// @ts-expect-error: FIXME. This comment was added by an automated script.
setFormState(initialFormState);
cancelButtonProps={{
onClick: () => {
onCancel();
setFormState(initialFormState);
},
}}
confirmButtonProps={{
onClick: onSubmit,
children: loadingSpawnHost ? "Saving" : "Save",
disabled: !hasChanges || hasError || loadingSpawnHost,
}}
onConfirm={onSubmit}
data-cy="edit-spawn-host-modal"
open={visible}
submitDisabled={!hasChanges || hasError || loadingSpawnHost}
title="Edit Host Details"
>
<SpruceForm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ export const SpawnHostModal: React.FC<SpawnHostModalProps> = ({
SpawnHostMutation,
SpawnHostMutationVariables
>(SPAWN_HOST, {
onCompleted(hostMutation) {
onCompleted: (hostMutation) => {
const { id } = hostMutation?.spawnHost ?? {};
dispatchToast.success(`Successfully spawned host ${id}`);
setOpen(false);
},
onError(err) {
onError: (err) => {
dispatchToast.error(
`There was an error while spawning your host: ${err.message}`,
);
Expand All @@ -86,25 +86,22 @@ export const SpawnHostModal: React.FC<SpawnHostModalProps> = ({
);

useVirtualWorkstationDefaultExpiration({
// @ts-expect-error: FIXME. This comment was added by an automated script.
isVirtualWorkstation: selectedDistro?.isVirtualWorkStation,
isVirtualWorkstation: selectedDistro?.isVirtualWorkStation ?? false,
setFormState,
formState,
disableExpirationCheckbox: formSchemaInput.disableExpirationCheckbox,
});

const hostUptimeWarnings = useMemo(() => {
const { enabledHoursCount, enabledWeekdaysCount } = getEnabledHoursCount(
// @ts-expect-error: FIXME. This comment was added by an automated script.
formState?.expirationDetails?.hostUptime,
);
const warnings = getHostUptimeWarnings({
enabledHoursCount,
enabledWeekdaysCount,
// @ts-expect-error: FIXME. This comment was added by an automated script.
runContinuously:
formState?.expirationDetails?.hostUptime?.sleepSchedule?.timeSelection
?.runContinuously,
?.runContinuously ?? false,
});
return { enabledHoursCount, warnings };
}, [formState?.expirationDetails?.hostUptime]);
Expand All @@ -129,13 +126,10 @@ export const SpawnHostModal: React.FC<SpawnHostModalProps> = ({

const spawnHost = () => {
const mutationInput = formToGql({
// @ts-expect-error: FIXME. This comment was added by an automated script.
isVirtualWorkStation: selectedDistro?.isVirtualWorkStation,
isVirtualWorkStation: selectedDistro?.isVirtualWorkStation ?? false,
formData: formState,
// @ts-expect-error: FIXME. This comment was added by an automated script.
myPublicKeys: formSchemaInput.myPublicKeys,
myPublicKeys: formSchemaInput.myPublicKeys ?? [],
spawnTaskData: spawnTaskData?.task,
timeZone,
});
spawnAnalytics.sendEvent({
name: "Created a spawn host",
Expand All @@ -151,14 +145,16 @@ export const SpawnHostModal: React.FC<SpawnHostModalProps> = ({

return (
<ConfirmationModal
buttonText={loadingSpawnHost ? "Spawning" : "Spawn a host"}
data-cy="spawn-host-modal"
onCancel={() => {
setOpen(false);
cancelButtonProps={{
onClick: () => setOpen(false),
}}
confirmButtonProps={{
children: loadingSpawnHost ? "Spawning" : "Spawn a host",
onClick: spawnHost,
disabled: hasError || loadingSpawnHost,
}}
onConfirm={spawnHost}
data-cy="spawn-host-modal"
open={open}
submitDisabled={hasError || loadingSpawnHost}
title="Spawn New Host"
>
<SpruceForm
Expand Down

0 comments on commit 6ae4f90

Please sign in to comment.