mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
chore: Simplify requestPatchKubeResource impl
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
b90e04e02d
commit
bc149f0627
@ -227,7 +227,7 @@ metadata:
|
||||
|
||||
it("calls for save with just the adding version label", () => {
|
||||
expect(requestPatchKubeResourceMock).toHaveBeenCalledWith(
|
||||
someNamespace,
|
||||
"/apis/some-api-version/namespaces/some-uid",
|
||||
[{
|
||||
op: "add",
|
||||
path: "/metadata/labels",
|
||||
@ -527,7 +527,7 @@ metadata:
|
||||
|
||||
it("calls for save with changed configuration", () => {
|
||||
expect(requestPatchKubeResourceMock).toHaveBeenCalledWith(
|
||||
someNamespace,
|
||||
"/apis/some-api-version/namespaces/some-uid",
|
||||
[
|
||||
{
|
||||
op: "remove",
|
||||
@ -594,7 +594,7 @@ metadata:
|
||||
fireEvent.click(saveButton);
|
||||
|
||||
expect(requestPatchKubeResourceMock).toHaveBeenCalledWith(
|
||||
someNamespace,
|
||||
"/apis/some-api-version/namespaces/some-uid",
|
||||
[
|
||||
{
|
||||
op: "add",
|
||||
@ -782,7 +782,7 @@ metadata:
|
||||
fireEvent.click(saveButton);
|
||||
|
||||
expect(requestPatchKubeResourceMock).toHaveBeenCalledWith(
|
||||
someOtherNamespace,
|
||||
"/apis/some-api-version/namespaces/some-other-uid",
|
||||
[{
|
||||
op: "add",
|
||||
path: "/metadata/labels",
|
||||
@ -855,7 +855,7 @@ metadata:
|
||||
fireEvent.click(saveButton);
|
||||
|
||||
expect(requestPatchKubeResourceMock).toHaveBeenCalledWith(
|
||||
someNamespace,
|
||||
"/apis/some-api-version/namespaces/some-uid",
|
||||
[ {
|
||||
op: "add",
|
||||
path: "/metadata/labels",
|
||||
|
||||
@ -205,7 +205,7 @@ export class EditResourceModel {
|
||||
return null;
|
||||
}
|
||||
|
||||
const result = await this.dependencies.callForPatchResource(this.resource, patches);
|
||||
const result = await this.dependencies.callForPatchResource(selfLink, patches);
|
||||
|
||||
if (!result.callWasSuccessful) {
|
||||
this.dependencies.showErrorNotification((
|
||||
|
||||
@ -4,42 +4,36 @@
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import type { AsyncResult } from "@k8slens/utilities";
|
||||
import apiManagerInjectable from "../../../../../common/k8s-api/api-manager/manager.injectable";
|
||||
import type { JsonPatch } from "../../../../../common/k8s-api/kube-object.store";
|
||||
import type { KubeObject } from "../../../../../common/k8s-api/kube-object";
|
||||
import assert from "assert";
|
||||
import { getErrorMessage } from "../../../../../common/utils/get-error-message";
|
||||
import apiKubeInjectable from "../../../../k8s/api-kube.injectable";
|
||||
import { patchTypeHeaders } from "../../../../../common/k8s-api/kube-api";
|
||||
|
||||
export type RequestPatchKubeResource = (
|
||||
item: KubeObject,
|
||||
patch: JsonPatch
|
||||
) => AsyncResult<{ name: string; kind: string }>;
|
||||
export type RequestPatchKubeResource = (selfLink: string, patch: JsonPatch) => AsyncResult<{ name: string; kind: string }>;
|
||||
|
||||
const requestPatchKubeResourceInjectable = getInjectable({
|
||||
id: "request-patch-kube-resource",
|
||||
instantiate: (di): RequestPatchKubeResource => {
|
||||
const apiManager = di.inject(apiManagerInjectable);
|
||||
|
||||
return async (item, patch) => {
|
||||
const store = apiManager.getStore(item.selfLink);
|
||||
|
||||
assert(store);
|
||||
|
||||
let kubeObject: KubeObject;
|
||||
const apiKube = di.inject(apiKubeInjectable);
|
||||
|
||||
return async (selfLink, patch) => {
|
||||
try {
|
||||
kubeObject = await store.patch(item, patch);
|
||||
} catch (e: any) {
|
||||
const kubeObject = await apiKube.patch(selfLink, { data: patch }, {
|
||||
headers: {
|
||||
"content-type": patchTypeHeaders.json,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
callWasSuccessful: true,
|
||||
response: { name: kubeObject.metadata.name, kind: kubeObject.kind },
|
||||
};
|
||||
} catch (e) {
|
||||
return {
|
||||
callWasSuccessful: false,
|
||||
error: getErrorMessage(e),
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
callWasSuccessful: true,
|
||||
response: { name: kubeObject.getName(), kind: kubeObject.kind },
|
||||
};
|
||||
};
|
||||
},
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user