Skip to content

Commit

Permalink
Fixing the generator to use bulk api to install endpoint package (#10…
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-buttner authored Jun 23, 2021
1 parent 5b0d325 commit 2816281
Showing 1 changed file with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@ import { KbnClient } from '@kbn/test';
import { AxiosResponse } from 'axios';
import { indexHostsAndAlerts } from '../../common/endpoint/index_data';
import { ANCESTRY_LIMIT, EndpointDocGenerator } from '../../common/endpoint/generate_data';
import { AGENTS_SETUP_API_ROUTES, SETUP_API_ROUTE } from '../../../fleet/common/constants';
import {
AGENTS_SETUP_API_ROUTES,
EPM_API_ROUTES,
SETUP_API_ROUTE,
} from '../../../fleet/common/constants';
import {
BulkInstallPackageInfo,
BulkInstallPackagesResponse,
CreateFleetSetupResponse,
IBulkInstallPackageHTTPError,
PostIngestSetupResponse,
} from '../../../fleet/common/types/rest_spec';
import { KbnClientWithApiKeySupport } from './kbn_client_with_api_key_support';
Expand All @@ -44,6 +51,12 @@ async function deleteIndices(indices: string[], client: Client) {
}
}

function isFleetBulkInstallError(
installResponse: BulkInstallPackageInfo | IBulkInstallPackageHTTPError
): installResponse is IBulkInstallPackageHTTPError {
return 'error' in installResponse && installResponse.error !== undefined;
}

async function doIngestSetup(kbnClient: KbnClient) {
// Setup Ingest
try {
Expand Down Expand Up @@ -76,6 +89,35 @@ async function doIngestSetup(kbnClient: KbnClient) {
console.error(error);
throw error;
}

// Install/upgrade the endpoint package
try {
const installEndpointPackageResp = (await kbnClient.request({
path: EPM_API_ROUTES.BULK_INSTALL_PATTERN,
method: 'POST',
body: {
packages: ['endpoint'],
},
})) as AxiosResponse<BulkInstallPackagesResponse>;

const bulkResp = installEndpointPackageResp.data.response;
if (bulkResp.length <= 0) {
throw new Error('Installing the Endpoint package failed, response was empty, existing');
}

if (isFleetBulkInstallError(bulkResp[0])) {
if (bulkResp[0].error instanceof Error) {
throw new Error(
`Installing the Endpoint package failed: ${bulkResp[0].error.message}, exiting`
);
}

throw new Error(bulkResp[0].error);
}
} catch (error) {
console.error(error);
throw error;
}
}

async function main() {
Expand Down

0 comments on commit 2816281

Please sign in to comment.