1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Fix integration test due to incorrect casting

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2022-11-29 12:16:18 -05:00
parent 97bf4ae129
commit 883bd36028
6 changed files with 118 additions and 41 deletions

View File

@ -5,25 +5,44 @@
import { getInjectable } from "@ogre-tools/injectable";
import type { Patch } from "rfc6902";
import apiBaseInjectable from "../../api-base.injectable";
import type { AsyncResult } from "../../../utils/async-result";
import type { KubeJsonApiData } from "../../kube-json-api";
export type RequestKubeObjectPatch = (name: string, kind: string, ns: string | undefined, patch: Patch) => Promise<KubeJsonApiData>;
export type RequestKubeObjectPatch = (name: string, kind: string, ns: string | undefined, patch: Patch) => Promise<AsyncResult<KubeJsonApiData, string>>;
const requestKubeObjectPatchInjectable = getInjectable({
id: "request-kube-object-patch",
instantiate: (di): RequestKubeObjectPatch => {
const apiBase = di.inject(apiBaseInjectable);
return (name, kind, ns, patch) => (
apiBase.patch("/stack", {
return async (name, kind, ns, patch) => {
const result = await apiBase.patch("/stack", {
data: {
name,
kind,
ns,
patch,
},
})
);
}) as AsyncResult<string, string>;
if (!result.callWasSuccessful) {
return result;
}
try {
const response = JSON.parse(result.response);
return {
callWasSuccessful: true,
response,
};
} catch (error) {
return {
callWasSuccessful: false,
error: String(error),
};
}
};
},
});

View File

@ -4,16 +4,37 @@
*/
import { getInjectable } from "@ogre-tools/injectable";
import apiBaseInjectable from "../../api-base.injectable";
import type { AsyncResult } from "../../../utils/async-result";
import type { KubeJsonApiData } from "../../kube-json-api";
export type RequestKubeObjectCreation = (resourceDescriptor: string) => Promise<KubeJsonApiData>;
export type RequestKubeObjectCreation = (resourceDescriptor: string) => Promise<AsyncResult<KubeJsonApiData, string>>;
const requestKubeObjectCreationInjectable = getInjectable({
id: "request-kube-object-creation",
instantiate: (di): RequestKubeObjectCreation => {
const apiBase = di.inject(apiBaseInjectable);
return (data) => apiBase.post("/stack", { data });
return async (data) => {
const result = await apiBase.post("/stack", { data }) as AsyncResult<string, string>;
if (!result.callWasSuccessful) {
return result;
}
try {
const response = JSON.parse(result.response);
return {
callWasSuccessful: true,
response,
};
} catch (error) {
return {
callWasSuccessful: false,
error: String(error),
};
}
};
},
});

View File

@ -645,8 +645,13 @@ export class KubeObject<
}
const requestKubeObjectPatch = asLegacyGlobalFunctionForExtensionApi(requestKubeObjectPatchInjectable);
const result = await requestKubeObjectPatch(this.getName(), this.kind, this.getNs(), patch);
return requestKubeObjectPatch(this.getName(), this.kind, this.getNs(), patch);
if (!result.callWasSuccessful) {
throw new Error(result.error);
}
return result.response;
}
/**
@ -665,7 +670,13 @@ export class KubeObject<
...data,
});
return requestKubeObjectCreation(descriptor);
const result = await requestKubeObjectCreation(descriptor);
if (!result.callWasSuccessful) {
throw new Error(result.error);
}
return result.response;
}
/**

View File

@ -9,13 +9,23 @@ import { loggerTransportInjectionToken } from "./logger/transports";
const loggerInjectable = getInjectable({
id: "logger",
instantiate: (di): Logger => createLogger({
format: format.combine(
format.splat(),
format.simple(),
),
transports: di.injectMany(loggerTransportInjectionToken),
}),
instantiate: (di): Logger => {
const baseLogger = createLogger({
format: format.combine(
format.splat(),
format.simple(),
),
transports: di.injectMany(loggerTransportInjectionToken),
});
return {
debug: (message, ...data) => baseLogger.debug(message, ...data),
info: (message, ...data) => baseLogger.info(message, ...data),
warn: (message, ...data) => baseLogger.warn(message, ...data),
error: (message, ...data) => baseLogger.error(message, ...data),
silly: (message, ...data) => baseLogger.silly(message, ...data),
};
},
});
export default loggerInjectable;

View File

@ -17,4 +17,6 @@ export interface Logger {
/**
* @deprecated use `di.inject(loggerInjectable)` instead
*/
export default asLegacyGlobalForExtensionApi(loggerInjectable);
const logger = asLegacyGlobalForExtensionApi(loggerInjectable);
export default logger;

View File

@ -13,8 +13,8 @@ import { observer } from "mobx-react";
import type { CreateResourceTabStore } from "./store";
import { EditorPanel } from "../editor-panel";
import { InfoPanel } from "../info-panel";
import { Notifications } from "../../notifications";
import logger from "../../../../common/logger";
import type { ShowNotification } from "../../notifications";
import type { Logger } from "../../../../common/logger";
import type { ApiManager } from "../../../../common/k8s-api/api-manager";
import { isObject, prevDefault } from "../../../utils";
import { withInjectables } from "@ogre-tools/injectable-react";
@ -29,6 +29,10 @@ import getDetailsUrlInjectable from "../../kube-detail-params/get-details-url.in
import navigateInjectable from "../../../navigation/navigate.injectable";
import type { RequestKubeObjectCreation } from "../../../../common/k8s-api/endpoints/resource-applier.api/request-update.injectable";
import requestKubeObjectCreationInjectable from "../../../../common/k8s-api/endpoints/resource-applier.api/request-update.injectable";
import loggerInjectable from "../../../../common/logger.injectable";
import type { ShowCheckedErrorNotification } from "../../notifications/show-checked-error.injectable";
import showSuccessNotificationInjectable from "../../notifications/show-success-notification.injectable";
import showCheckedErrorNotificationInjectable from "../../notifications/show-checked-error.injectable";
export interface CreateResourceProps {
tabId: string;
@ -38,9 +42,12 @@ interface Dependencies {
createResourceTemplates: IComputedValue<GroupBase<{ label: string; value: string }>[]>;
createResourceTabStore: CreateResourceTabStore;
apiManager: ApiManager;
logger: Logger;
navigate: Navigate;
getDetailsUrl: GetDetailsUrl;
requestKubeObjectCreation: RequestKubeObjectCreation;
showSuccessNotification: ShowNotification;
showCheckedErrorNotification: ShowCheckedErrorNotification;
}
@observer
@ -81,34 +88,38 @@ class NonInjectedCreateResource extends React.Component<CreateResourceProps & De
const resources = yaml.loadAll(this.data).filter(isObject);
if (resources.length === 0) {
return void logger.info("Nothing to create");
return this.props.logger.info("Nothing to create");
}
const creatingResources = resources.map(async (resource) => {
try {
const data = await requestKubeObjectCreation(dump(resource));
const { kind, apiVersion, metadata: { name, namespace }} = data;
const result = await requestKubeObjectCreation(dump(resource));
const showDetails = () => {
const resourceLink = apiManager.lookupApiLink({ kind, apiVersion, name, namespace });
if (!result.callWasSuccessful) {
this.props.logger.warn("Failed to create resource", { resource }, result.error);
this.props.showCheckedErrorNotification(result.error, "Unknown error occured while creating resources");
navigate(getDetailsUrl(resourceLink));
close();
};
const close = Notifications.ok(
<p>
{kind}
{" "}
<a onClick={prevDefault(showDetails)}>
{name}
</a>
{" successfully created."}
</p>,
);
} catch (error) {
Notifications.checkedError(error, "Unknown error occured while creating resources");
return;
}
const { kind, apiVersion, metadata: { name, namespace }} = result.response;
const close = this.props.showSuccessNotification((
<p>
{kind}
{" "}
<a
onClick={prevDefault(() => {
const resourceLink = apiManager.lookupApiLink({ kind, apiVersion, name, namespace });
navigate(getDetailsUrl(resourceLink));
close();
})}
>
{name}
</a>
{" successfully created."}
</p>
));
});
await Promise.allSettled(creatingResources);
@ -168,8 +179,11 @@ export const CreateResource = withInjectables<Dependencies, CreateResourceProps>
createResourceTabStore: di.inject(createResourceTabStoreInjectable),
createResourceTemplates: await di.inject(createResourceTemplatesInjectable),
apiManager: di.inject(apiManagerInjectable),
logger: di.inject(loggerInjectable),
getDetailsUrl: di.inject(getDetailsUrlInjectable),
navigate: di.inject(navigateInjectable),
requestKubeObjectCreation: di.inject(requestKubeObjectCreationInjectable),
showSuccessNotification: di.inject(showSuccessNotificationInjectable),
showCheckedErrorNotification: di.inject(showCheckedErrorNotificationInjectable),
}),
});