Skip to content

Commit

Permalink
Lift .map lambda into named outer function
Browse files Browse the repository at this point in the history
  • Loading branch information
John Schulz committed Sep 27, 2020
1 parent ee4c774 commit c906aea
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 23 deletions.
34 changes: 21 additions & 13 deletions x-pack/plugins/ingest_manager/server/routes/epm/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import {
GetCategoriesResponse,
GetPackagesResponse,
GetLimitedPackagesResponse,
BulkInstallPackageInfo,
BulkInstallPackagesResponse,
IBulkInstallPackageError,
} from '../../../common';
import {
GetCategoriesRequestSchema,
Expand All @@ -26,13 +28,15 @@ import {
BulkUpgradePackagesFromRegistryRequestSchema,
} from '../../types';
import {
BulkInstallResponse,
bulkInstallPackages,
getCategories,
getPackages,
getFile,
getPackageInfo,
handleInstallPackageFailure,
installPackage,
isBulkInstallError,
removeInstallation,
getLimitedPackages,
getInstallationObject,
Expand Down Expand Up @@ -169,30 +173,34 @@ export const installPackageFromRegistryHandler: RequestHandler<
}
};

const bulkInstallServiceResponseToHttpEntry = (
result: BulkInstallResponse
): BulkInstallPackageInfo | IBulkInstallPackageError => {
if (isBulkInstallError(result)) {
const { statusCode, body } = ingestErrorToResponseOptions(result.error);
return {
name: result.name,
statusCode,
error: body.message,
};
} else {
return result;
}
};

export const bulkInstallPackagesFromRegistryHandler: RequestHandler<
undefined,
undefined,
TypeOf<typeof BulkUpgradePackagesFromRegistryRequestSchema.body>
> = async (context, request, response) => {
const savedObjectsClient = context.core.savedObjects.client;
const callCluster = context.core.elasticsearch.legacy.client.callAsCurrentUser;
const bulkInstalledResults = await bulkInstallPackages({
const bulkInstalledResponses = await bulkInstallPackages({
savedObjectsClient,
callCluster,
packagesToUpgrade: request.body.packages,
});
const payload = bulkInstalledResults.map((result) => {
if ('error' in result) {
const { statusCode, body } = ingestErrorToResponseOptions(result.error);
return {
name: result.name,
statusCode,
error: body.message,
};
} else {
return result;
}
});
const payload = bulkInstalledResponses.map(bulkInstallServiceResponseToHttpEntry);
const body: BulkInstallPackagesResponse = {
response: payload,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import { SavedObjectsClientContract } from 'src/core/server';
import { CallESAsCurrentUser } from '../../../types';
import * as Registry from '../registry';
import { getInstallationObject } from './index';
import { BulkInstallResponse, upgradePackage } from './install';
import { BulkInstallResponse, IBulkInstallPackageError, upgradePackage } from './install';

interface BulkInstallPackagesParams {
savedObjectsClient: SavedObjectsClientContract;
packagesToUpgrade: string[];
callCluster: CallESAsCurrentUser;
}

export async function bulkInstallPackages({
savedObjectsClient,
packagesToUpgrade,
Expand Down Expand Up @@ -54,3 +55,7 @@ export async function bulkInstallPackages({

return installResponses;
}

export function isBulkInstallError(test: any): test is IBulkInstallPackageError {
return 'error' in test && test.error instanceof Error;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
KibanaAssetType,
} from '../../../types';

export { bulkInstallPackages } from './bulk_install_packages';
export { bulkInstallPackages, isBulkInstallError } from './bulk_install_packages';
export {
getCategories,
getFile,
Expand All @@ -25,7 +25,13 @@ export {
SearchParams,
} from './get';

export { handleInstallPackageFailure, installPackage, ensureInstalledPackage } from './install';
export {
BulkInstallResponse,
handleInstallPackageFailure,
installPackage,
IBulkInstallPackageError,
ensureInstalledPackage,
} from './install';
export { removeInstallation } from './remove';

type RequiredPackage = 'system' | 'endpoint';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ import {
} from '../../../types';
import { installIndexPatterns } from '../kibana/index_pattern/install';
import * as Registry from '../registry';
import { getInstallation, getInstallationObject, isRequiredPackage } from './index';
import {
getInstallation,
getInstallationObject,
isRequiredPackage,
bulkInstallPackages,
isBulkInstallError,
} from './index';
import { installTemplates } from '../elasticsearch/template/install';
import { generateESIndexPatterns } from '../elasticsearch/template/template';
import { installPipelines, deletePreviousPipelines } from '../elasticsearch/ingest_pipeline/';
Expand All @@ -40,7 +46,6 @@ import { IngestManagerError, PackageOutdatedError } from '../../../errors';
import { getPackageSavedObjects } from './get';
import { installTransformForDataset } from '../elasticsearch/transform/install';
import { appContextService } from '../../app_context';
import { bulkInstallPackages } from './bulk_install_packages';

export async function installLatestPackage(options: {
savedObjectsClient: SavedObjectsClientContract;
Expand All @@ -60,10 +65,6 @@ export async function installLatestPackage(options: {
}
}

function isBulkInstallError(test: any): test is IBulkInstallPackageError {
return 'error' in test && test.error instanceof Error;
}

export async function ensureInstalledDefaultPackages(
savedObjectsClient: SavedObjectsClientContract,
callCluster: CallESAsCurrentUser
Expand Down Expand Up @@ -165,7 +166,7 @@ export async function handleInstallPackageFailure({
}
}

interface IBulkInstallPackageError {
export interface IBulkInstallPackageError {
name: string;
error: Error;
}
Expand Down

0 comments on commit c906aea

Please sign in to comment.