Skip to content

Commit

Permalink
Add route to accept a direct package upload.
Browse files Browse the repository at this point in the history
  • Loading branch information
skh committed Sep 9, 2020
1 parent a127766 commit f921fb9
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 12 deletions.
3 changes: 2 additions & 1 deletion x-pack/plugins/ingest_manager/common/constants/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export const EPM_API_ROUTES = {
LIST_PATTERN: EPM_PACKAGES_MANY,
LIMITED_LIST_PATTERN: `${EPM_PACKAGES_MANY}/limited`,
INFO_PATTERN: EPM_PACKAGES_ONE,
INSTALL_PATTERN: EPM_PACKAGES_ONE,
INSTALL_FROM_REGISTRY_PATTERN: EPM_PACKAGES_ONE,
INSTALL_BY_UPLOAD_PATTERN: EPM_PACKAGES_MANY,
DELETE_PATTERN: EPM_PACKAGES_ONE,
FILEPATH_PATTERN: `${EPM_PACKAGES_FILE}/{filePath*}`,
CATEGORIES_PATTERN: `${EPM_API_ROOT}/categories`,
Expand Down
5 changes: 4 additions & 1 deletion x-pack/plugins/ingest_manager/common/services/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ export const epmRouteService = {
},

getInstallPath: (pkgkey: string) => {
return EPM_API_ROUTES.INSTALL_PATTERN.replace('{pkgkey}', pkgkey).replace(/\/$/, ''); // trim trailing slash
return EPM_API_ROUTES.INSTALL_FROM_REGISTRY_PATTERN.replace('{pkgkey}', pkgkey).replace(
/\/$/,
''
); // trim trailing slash
},

getRemovePath: (pkgkey: string) => {
Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/ingest_manager/common/types/rest_spec/epm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export interface InstallPackageResponse {
response: AssetReference[];
}

export interface InstallTmpPackageResponse {
response: string;
}

export interface DeletePackageRequest {
params: {
pkgkey: string;
Expand Down
21 changes: 17 additions & 4 deletions x-pack/plugins/ingest_manager/server/routes/epm/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { appContextService } from '../../services';
import {
GetInfoResponse,
InstallPackageResponse,
InstallTmpPackageResponse,
DeletePackageResponse,
GetCategoriesResponse,
GetPackagesResponse,
Expand All @@ -19,7 +20,8 @@ import {
GetPackagesRequestSchema,
GetFileRequestSchema,
GetInfoRequestSchema,
InstallPackageRequestSchema,
InstallPackageFromRegistryRequestSchema,
InstallPackageByUploadRequestSchema,
DeletePackageRequestSchema,
} from '../../types';
import {
Expand Down Expand Up @@ -129,10 +131,10 @@ export const getInfoHandler: RequestHandler<TypeOf<typeof GetInfoRequestSchema.p
}
};

export const installPackageHandler: RequestHandler<
TypeOf<typeof InstallPackageRequestSchema.params>,
export const installPackageFromRegistryHandler: RequestHandler<
TypeOf<typeof InstallPackageFromRegistryRequestSchema.params>,
undefined,
TypeOf<typeof InstallPackageRequestSchema.body>
TypeOf<typeof InstallPackageFromRegistryRequestSchema.body>
> = async (context, request, response) => {
const logger = appContextService.getLogger();
const savedObjectsClient = context.core.savedObjects.client;
Expand Down Expand Up @@ -183,6 +185,17 @@ export const installPackageHandler: RequestHandler<
}
};

export const installPackageByUploadHandler: RequestHandler<
undefined,
undefined,
TypeOf<typeof InstallPackageByUploadRequestSchema.body>
> = async (context, request, response) => {
const body: InstallTmpPackageResponse = {
response: 'package upload was received ok, but not installed (not implemented yet)',
};
return response.ok({ body });
};

export const deletePackageHandler: RequestHandler<TypeOf<
typeof DeletePackageRequestSchema.params
>> = async (context, request, response) => {
Expand Down
29 changes: 24 additions & 5 deletions x-pack/plugins/ingest_manager/server/routes/epm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@ import {
getLimitedListHandler,
getFileHandler,
getInfoHandler,
installPackageHandler,
installPackageFromRegistryHandler,
installPackageByUploadHandler,
deletePackageHandler,
} from './handlers';
import {
GetCategoriesRequestSchema,
GetPackagesRequestSchema,
GetFileRequestSchema,
GetInfoRequestSchema,
InstallPackageRequestSchema,
InstallPackageFromRegistryRequestSchema,
InstallPackageByUploadRequestSchema,
DeletePackageRequestSchema,
} from '../../types';

const MAX_FILE_SIZE_BYTES = 104857600; // 100MB

export const registerRoutes = (router: IRouter) => {
router.get(
{
Expand Down Expand Up @@ -71,11 +75,26 @@ export const registerRoutes = (router: IRouter) => {

router.post(
{
path: EPM_API_ROUTES.INSTALL_PATTERN,
validate: InstallPackageRequestSchema,
path: EPM_API_ROUTES.INSTALL_FROM_REGISTRY_PATTERN,
validate: InstallPackageFromRegistryRequestSchema,
options: { tags: [`access:${PLUGIN_ID}-all`] },
},
installPackageHandler
installPackageFromRegistryHandler
);

router.post(
{
path: EPM_API_ROUTES.INSTALL_BY_UPLOAD_PATTERN,
validate: InstallPackageByUploadRequestSchema,
options: {
tags: [`access:${PLUGIN_ID}-all`],
body: {
accepts: ['application/octet-stream'],
maxBytes: MAX_FILE_SIZE_BYTES,
},
},
},
installPackageByUploadHandler
);

router.delete(
Expand Down
6 changes: 5 additions & 1 deletion x-pack/plugins/ingest_manager/server/types/rest_spec/epm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const GetInfoRequestSchema = {
}),
};

export const InstallPackageRequestSchema = {
export const InstallPackageFromRegistryRequestSchema = {
params: schema.object({
pkgkey: schema.string(),
}),
Expand All @@ -43,6 +43,10 @@ export const InstallPackageRequestSchema = {
),
};

export const InstallPackageByUploadRequestSchema = {
body: schema.buffer(),
};

export const DeletePackageRequestSchema = {
params: schema.object({
pkgkey: schema.string(),
Expand Down

0 comments on commit f921fb9

Please sign in to comment.