Skip to content

Commit

Permalink
add api endpoint to handle images
Browse files Browse the repository at this point in the history
  • Loading branch information
neptunian committed Oct 18, 2019
1 parent 4fe8bcf commit f4d07c8
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { SAVED_OBJECT_TYPE } from '../../common/constants';
import { InstallationAttributes } from '../../common/types';
import * as Registry from '../registry';
import { createInstallableFrom } from './index';
import { ImageRequestParams } from '../types';

export { SearchParams } from '../registry';

Expand Down Expand Up @@ -68,6 +69,8 @@ export async function getIntegrationInfo(options: {
return createInstallableFrom(updated, savedObject);
}

export const getImage = async (options: ImageRequestParams) => Registry.fetchImage(options);

export async function getInstallationObject(options: {
savedObjectsClient: SavedObjectsClientContract;
pkgkey: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import {
getCategories,
getClusterAccessor,
getIntegrationInfo,
getImage,
getIntegrations,
installIntegration,
removeInstallation,
} from './index';
import { ImageRequestParams } from '../types';

interface Extra extends ResponseToolkit {
context: PluginContext;
Expand All @@ -31,6 +33,10 @@ interface PackageRequest extends Request {
};
}

interface ImageRequest extends Request {
params: ImageRequestParams;
}

interface InstallAssetRequest extends Request {
params: AssetRequestParams;
}
Expand Down Expand Up @@ -65,6 +71,8 @@ export async function handleGetInfo(req: PackageRequest, extra: Extra) {
return integrationInfo;
}

export const handleGetImage = async (req: ImageRequest) => getImage(req.params);

export async function handleRequestInstall(req: InstallAssetRequest, extra: Extra) {
const { pkgkey, asset } = req.params;
if (!asset) throw new Error('Unhandled empty/default asset case');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { ArchiveEntry, untarBuffer } from './extract';
import { fetchUrl, getResponseStream } from './requests';
import { streamToBuffer } from './streams';
import { integrationsManagerConfigStore } from '../config';
import { ImageRequestParams } from '../types';

export { ArchiveEntry } from './extract';

Expand All @@ -40,6 +41,12 @@ export async function fetchInfo(key: string): Promise<RegistryPackage> {
return fetchUrl(`${registryUrl}/package/${key}`).then(JSON.parse);
}

export async function fetchImage(params: ImageRequestParams): Promise<NodeJS.ReadableStream> {
const { registryUrl } = integrationsManagerConfigStore.getConfig();
const { pkgkey, imgPath } = params;
return getResponseStream(`${registryUrl}/package/${pkgkey}/img/${imgPath}`);
}

export async function fetchCategories(): Promise<CategorySummaryList> {
const { registryUrl } = integrationsManagerConfigStore.getConfig();
return fetchUrl(`${registryUrl}/categories`).then(JSON.parse);
Expand Down
9 changes: 9 additions & 0 deletions x-pack/legacy/plugins/integrations_manager/server/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { ServerRoute } from '../common/types';
import * as CommonRoutes from '../common/routes';
import * as Integrations from './integrations/handlers';

export const API_ROOT = `/api/${PLUGIN.ID}`;
export const API_IMG_PATTERN = `${API_ROOT}/package/{pkgkey}/img/{imgPath*}`;

// Manager public API paths
export const routes: ServerRoute[] = [
{
Expand All @@ -22,6 +25,12 @@ export const routes: ServerRoute[] = [
options: { tags: [`access:${PLUGIN.ID}`], json: { space: 2 } },
handler: Integrations.handleGetList,
},
{
method: 'GET',
path: API_IMG_PATTERN,
options: { tags: [`access:${PLUGIN.ID}`], json: { space: 2 } },
handler: Integrations.handleGetImage,
},
{
method: 'GET',
path: CommonRoutes.API_INFO_PATTERN,
Expand Down
11 changes: 11 additions & 0 deletions x-pack/legacy/plugins/integrations_manager/server/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

export interface ImageRequestParams {
readonly [key: string]: string;
readonly pkgkey: string;
readonly imgPath: string;
}

0 comments on commit f4d07c8

Please sign in to comment.