Skip to content

Commit

Permalink
frontend: Prevent success pop-up on invalid operations
Browse files Browse the repository at this point in the history
Signed-off-by: Evangelos Skopelitis <eskopelitis@microsoft.com>
  • Loading branch information
skoeva committed Jul 12, 2024
1 parent 6d5b487 commit f14db98
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 48 deletions.
78 changes: 39 additions & 39 deletions frontend/src/components/common/Resource/CreateButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,27 @@ export default function CreateButton(props: CreateButtonProps) {
const dispatchCreateEvent = useEventCallback(HeadlampEventType.CREATE_RESOURCE);

const applyFunc = async (newItems: KubeObjectInterface[], clusterName: string) => {
await Promise.allSettled(newItems.map(newItem => apply(newItem, clusterName))).then(
(values: any) => {
values.forEach((value: any, index: number) => {
if (value.status === 'rejected') {
let msg;
const kind = newItems[index].kind;
const name = newItems[index].metadata.name;
const apiVersion = newItems[index].apiVersion;
if (newItems.length === 1) {
msg = t('translation|Failed to create {{ kind }} {{ name }}.', { kind, name });
} else {
msg = t('translation|Failed to create {{ kind }} {{ name }} in {{ apiVersion }}.', {
const results = await Promise.allSettled(newItems.map(newItem => apply(newItem, clusterName)));
let success = true;
results.forEach((result: any, index: number) => {
if (result.status === 'rejected') {
success = false;
const kind = newItems[index].kind;
const name = newItems[index].metadata.name;
const apiVersion = newItems[index].apiVersion;
const msg =
newItems.length === 1
? t('translation|Failed to create {{ kind }} {{ name }}.', { kind, name })
: t('translation|Failed to create {{ kind }} {{ name }} in {{ apiVersion }}.', {
kind,
name,
apiVersion,
});
}
setErrorMessage(msg);
setOpenDialog(true);
throw msg;
}
});
setErrorMessage(msg);
setOpenDialog(true);
}
);
});
return success;
};

function handleSave(newItemDefs: KubeObjectInterface[]) {
Expand Down Expand Up @@ -84,26 +81,29 @@ export default function CreateButton(props: CreateButtonProps) {

const clusterName = getCluster() || '';

dispatch(
clusterAction(() => applyFunc(massagedNewItemDefs, clusterName), {
startMessage: t('translation|Applying {{ newItemName }}…', {
newItemName: resourceNames.join(','),
}),
cancelledMessage: t('translation|Cancelled applying {{ newItemName }}.', {
newItemName: resourceNames.join(','),
}),
successMessage: t('translation|Applied {{ newItemName }}.', {
newItemName: resourceNames.join(','),
}),
errorMessage: t('translation|Failed to apply {{ newItemName }}.', {
newItemName: resourceNames.join(','),
}),
cancelUrl,
})
);

dispatchCreateEvent({
status: EventStatus.CONFIRMED,
applyFunc(massagedNewItemDefs, clusterName).then(success => {
if (success) {
dispatch(
clusterAction(() => Promise.resolve(), {
startMessage: t('translation|Applying {{ newItemName }}…', {
newItemName: resourceNames.join(','),
}),
cancelledMessage: t('translation|Cancelled applying {{ newItemName }}.', {
newItemName: resourceNames.join(','),
}),
successMessage: t('translation|Applied {{ newItemName }}.', {
newItemName: resourceNames.join(','),
}),
errorMessage: t('translation|Failed to apply {{ newItemName }}.', {
newItemName: resourceNames.join(','),
}),
cancelUrl,
})
);
dispatchCreateEvent({
status: EventStatus.CONFIRMED,
});
}
});
}

Expand Down
24 changes: 15 additions & 9 deletions frontend/src/lib/k8s/apiProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1549,16 +1549,22 @@ export async function apply(body: KubeObjectInterface, clusterName?: string): Pr

try {
delete bodyToApply.metadata.resourceVersion;
return await apiEndpoint.post(bodyToApply, {}, cluster);

if (!resourceVersion) {
// Try to create the resource
return await apiEndpoint.post(bodyToApply, {}, cluster);
} else {
// Try to update the resource if resourceVersion is provided
bodyToApply.metadata.resourceVersion = resourceVersion;
return await apiEndpoint.put(bodyToApply, {}, cluster);
}
} catch (err) {
// Check to see if failed because the record already exists.
// If the failure isn't a 409 (i.e. Confilct), just rethrow.
if ((err as ApiError).status !== 409) throw err;

// Preserve the resourceVersion if its an update request
bodyToApply.metadata.resourceVersion = resourceVersion;
// We had a conflict. Try a PUT
return apiEndpoint.put(bodyToApply, {}, cluster);
if (!resourceVersion && (err as ApiError).status === 409) {
// If creation failed because the resource exists, handle it
throw new Error('Resource already exists');
}
console.error(`Error applying the resource ${bodyToApply}: ${err}`);
throw err;
}
}

Expand Down

0 comments on commit f14db98

Please sign in to comment.