forked from opensearch-project/dashboards-observability
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request opensearch-project#547 from Swiddis/osints/main
Stub router for integrations project Signed-off-by: Simeon Widdis <[email protected]>
Showing
9 changed files
with
789 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/bin/sh | ||
if [ -z "$husky_skip_init" ]; then | ||
debug () { | ||
[ "$HUSKY_DEBUG" = "1" ] && echo "husky (debug) - $1" | ||
} | ||
|
||
readonly hook_name="$(basename "$0")" | ||
debug "starting $hook_name..." | ||
|
||
if [ "$HUSKY" = "0" ]; then | ||
debug "HUSKY env variable is set to 0, skipping hook" | ||
exit 0 | ||
fi | ||
|
||
if [ -f ~/.huskyrc ]; then | ||
debug "sourcing ~/.huskyrc" | ||
. ~/.huskyrc | ||
fi | ||
|
||
export readonly husky_skip_init=1 | ||
sh -e "$0" "$@" | ||
exitCode="$?" | ||
|
||
if [ $exitCode != 0 ]; then | ||
echo "husky - $hook_name hook exited with code $exitCode (error)" | ||
exit $exitCode | ||
fi | ||
|
||
exit 0 | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
export interface IntegrationsAdaptor { | ||
getIntegrationTemplates: ( | ||
query?: IntegrationTemplateQuery | ||
) => Promise<IntegrationTemplateSearchResult>; | ||
|
||
getIntegrationInstances: ( | ||
query?: IntegrationInstanceQuery | ||
) => Promise<IntegrationInstancesSearchResult>; | ||
|
||
getIntegrationInstance: (query?: IntegrationInstanceQuery) => Promise<IntegrationInstanceResult>; | ||
|
||
loadIntegrationInstance: ( | ||
templateName: string, | ||
name: string, | ||
dataSource: string | ||
) => Promise<IntegrationInstance>; | ||
|
||
deleteIntegrationInstance: (id: string) => Promise<unknown>; | ||
|
||
getStatic: (templateName: string, path: string) => Promise<Buffer>; | ||
|
||
getSchemas: ( | ||
templateName: string | ||
) => Promise<{ mappings: { [key: string]: unknown }; schemas: { [key: string]: unknown } }>; | ||
|
||
getAssets: (templateName: string) => Promise<{ savedObjects?: unknown }>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
interface IntegrationTemplate { | ||
name: string; | ||
version: string; | ||
displayName?: string; | ||
integrationType: string; | ||
license: string; | ||
type: string; | ||
author?: string; | ||
description?: string; | ||
sourceUrl?: string; | ||
statics?: { | ||
logo?: StaticAsset; | ||
gallery?: StaticAsset[]; | ||
darkModeLogo?: StaticAsset; | ||
darkModeGallery?: StaticAsset[]; | ||
}; | ||
components: IntegrationComponent[]; | ||
assets: { | ||
savedObjects?: { | ||
name: string; | ||
version: string; | ||
}; | ||
}; | ||
} | ||
|
||
interface StaticAsset { | ||
annotation?: string; | ||
path: string; | ||
} | ||
|
||
interface IntegrationComponent { | ||
name: string; | ||
version: string; | ||
} | ||
|
||
interface DisplayAsset { | ||
body: string; | ||
} | ||
|
||
interface IntegrationTemplateSearchResult { | ||
hits: IntegrationTemplate[]; | ||
} | ||
|
||
interface IntegrationTemplateQuery { | ||
name?: string; | ||
} | ||
|
||
interface IntegrationInstance { | ||
name: string; | ||
templateName: string; | ||
dataSource: { | ||
sourceType: string; | ||
dataset: string; | ||
namespace: string; | ||
}; | ||
creationDate: string; | ||
assets: AssetReference[]; | ||
} | ||
|
||
interface IntegrationInstanceResult extends IntegrationInstance { | ||
id: string; | ||
status: string; | ||
} | ||
|
||
interface AssetReference { | ||
assetType: string; | ||
assetId: string; | ||
isDefaultAsset: boolean; | ||
description: string; | ||
} | ||
|
||
interface IntegrationInstancesSearchResult { | ||
hits: IntegrationInstanceResult[]; | ||
} | ||
|
||
interface IntegrationInstanceQuery { | ||
added?: boolean; | ||
id?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
server/routes/integrations/__tests__/integrations_router.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { OpenSearchDashboardsResponseFactory } from '../../../../../../src/core/server/http/router'; | ||
import { handleWithCallback } from '../integrations_router'; | ||
import { IntegrationsAdaptor } from 'server/adaptors/integrations/integrations_adaptor'; | ||
|
||
describe('handleWithCallback', () => { | ||
let adaptorMock: jest.Mocked<IntegrationsAdaptor>; | ||
let responseMock: jest.Mocked<OpenSearchDashboardsResponseFactory>; | ||
|
||
beforeEach(() => { | ||
adaptorMock = {} as any; | ||
responseMock = { | ||
custom: jest.fn((data) => data), | ||
ok: jest.fn((data) => data), | ||
} as any; | ||
}); | ||
|
||
it('retrieves data from the callback method', async () => { | ||
const callback = jest.fn((_) => { | ||
return { test: 'data' }; | ||
}); | ||
|
||
const result = await handleWithCallback( | ||
adaptorMock as IntegrationsAdaptor, | ||
responseMock as OpenSearchDashboardsResponseFactory, | ||
callback | ||
); | ||
|
||
expect(callback).toHaveBeenCalled(); | ||
expect(responseMock.ok).toHaveBeenCalled(); | ||
expect(result.body.data).toEqual({ test: 'data' }); | ||
}); | ||
|
||
it('passes callback errors through', async () => { | ||
const callback = jest.fn((_) => { | ||
throw new Error('test error'); | ||
}); | ||
|
||
const result = await handleWithCallback( | ||
adaptorMock as IntegrationsAdaptor, | ||
responseMock as OpenSearchDashboardsResponseFactory, | ||
callback | ||
); | ||
|
||
expect(callback).toHaveBeenCalled(); | ||
expect(responseMock.custom).toHaveBeenCalled(); | ||
expect(result.body).toEqual('test error'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,233 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { schema } from '@osd/config-schema'; | ||
import * as mime from 'mime'; | ||
import { IRouter, RequestHandlerContext } from '../../../../../src/core/server'; | ||
import { INTEGRATIONS_BASE } from '../../../common/constants/shared'; | ||
import { IntegrationsAdaptor } from '../../adaptors/integrations/integrations_adaptor'; | ||
import { | ||
OpenSearchDashboardsRequest, | ||
OpenSearchDashboardsResponseFactory, | ||
} from '../../../../../src/core/server/http/router'; | ||
|
||
/** | ||
* Handle an `OpenSearchDashboardsRequest` using the provided `callback` function. | ||
* This is a convenience method that handles common error handling and response formatting. | ||
* The callback must accept a `IntegrationsAdaptor` as its first argument. | ||
* | ||
* If the callback throws an error, | ||
* the `OpenSearchDashboardsResponse` will be formatted using the error's `statusCode` and `message` properties. | ||
* Otherwise, the callback's return value will be formatted in a JSON object under the `data` field. | ||
* | ||
* @param {IntegrationsAdaptor} adaptor The adaptor instance to use for making requests. | ||
* @param {OpenSearchDashboardsResponseFactory} response The factory to use for creating responses. | ||
* @callback callback A callback that will invoke a request on a provided adaptor. | ||
* @returns {Promise<OpenSearchDashboardsResponse>} An `OpenSearchDashboardsResponse` with the return data from the callback. | ||
*/ | ||
export const handleWithCallback = async ( | ||
adaptor: IntegrationsAdaptor, | ||
response: OpenSearchDashboardsResponseFactory, | ||
callback: (a: IntegrationsAdaptor) => any | ||
): Promise<any> => { | ||
try { | ||
const data = await callback(adaptor); | ||
return response.ok({ | ||
body: { | ||
data, | ||
}, | ||
}); | ||
} catch (err: any) { | ||
console.error(`handleWithCallback: callback failed with error "${err.message}"`); | ||
return response.custom({ | ||
statusCode: err.statusCode || 500, | ||
body: err.message, | ||
}); | ||
} | ||
}; | ||
|
||
const getAdaptor = ( | ||
context: RequestHandlerContext, | ||
_request: OpenSearchDashboardsRequest | ||
): IntegrationsAdaptor => { | ||
// Stub | ||
return {} as IntegrationsAdaptor; | ||
}; | ||
|
||
export function registerIntegrationsRoute(router: IRouter) { | ||
router.get( | ||
{ | ||
path: `${INTEGRATIONS_BASE}/repository`, | ||
validate: false, | ||
}, | ||
async (context, request, response): Promise<any> => { | ||
const adaptor = getAdaptor(context, request); | ||
return handleWithCallback(adaptor, response, async (a: IntegrationsAdaptor) => | ||
a.getIntegrationTemplates() | ||
); | ||
} | ||
); | ||
|
||
router.post( | ||
{ | ||
path: `${INTEGRATIONS_BASE}/store/{templateName}`, | ||
validate: { | ||
params: schema.object({ | ||
templateName: schema.string(), | ||
}), | ||
body: schema.object({ | ||
name: schema.string(), | ||
dataSource: schema.string(), | ||
}), | ||
}, | ||
}, | ||
async (context, request, response): Promise<any> => { | ||
const adaptor = getAdaptor(context, request); | ||
return handleWithCallback(adaptor, response, async (a: IntegrationsAdaptor) => { | ||
return a.loadIntegrationInstance( | ||
request.params.templateName, | ||
request.body.name, | ||
request.body.dataSource | ||
); | ||
}); | ||
} | ||
); | ||
|
||
router.get( | ||
{ | ||
path: `${INTEGRATIONS_BASE}/repository/{name}`, | ||
validate: { | ||
params: schema.object({ | ||
name: schema.string(), | ||
}), | ||
}, | ||
}, | ||
async (context, request, response): Promise<any> => { | ||
const adaptor = getAdaptor(context, request); | ||
return handleWithCallback( | ||
adaptor, | ||
response, | ||
async (a: IntegrationsAdaptor) => | ||
( | ||
await a.getIntegrationTemplates({ | ||
name: request.params.name, | ||
}) | ||
).hits[0] | ||
); | ||
} | ||
); | ||
|
||
router.get( | ||
{ | ||
path: `${INTEGRATIONS_BASE}/repository/{id}/static/{path}`, | ||
validate: { | ||
params: schema.object({ | ||
id: schema.string(), | ||
path: schema.string(), | ||
}), | ||
}, | ||
}, | ||
async (context, request, response): Promise<any> => { | ||
const adaptor = getAdaptor(context, request); | ||
try { | ||
const result = await adaptor.getStatic(request.params.id, request.params.path); | ||
return response.ok({ | ||
headers: { | ||
'Content-Type': mime.getType(request.params.path), | ||
}, | ||
body: result, | ||
}); | ||
} catch (err: any) { | ||
return response.custom({ | ||
statusCode: err.statusCode ? err.statusCode : 500, | ||
body: err.message, | ||
}); | ||
} | ||
} | ||
); | ||
|
||
router.get( | ||
{ | ||
path: `${INTEGRATIONS_BASE}/repository/{id}/schema`, | ||
validate: { | ||
params: schema.object({ | ||
id: schema.string(), | ||
}), | ||
}, | ||
}, | ||
async (context, request, response): Promise<any> => { | ||
const adaptor = getAdaptor(context, request); | ||
return handleWithCallback(adaptor, response, async (a: IntegrationsAdaptor) => | ||
a.getSchemas(request.params.id) | ||
); | ||
} | ||
); | ||
|
||
router.get( | ||
{ | ||
path: `${INTEGRATIONS_BASE}/repository/{id}/assets`, | ||
validate: { | ||
params: schema.object({ | ||
id: schema.string(), | ||
}), | ||
}, | ||
}, | ||
async (context, request, response): Promise<any> => { | ||
const adaptor = getAdaptor(context, request); | ||
return handleWithCallback(adaptor, response, async (a: IntegrationsAdaptor) => | ||
a.getAssets(request.params.id) | ||
); | ||
} | ||
); | ||
|
||
router.get( | ||
{ | ||
path: `${INTEGRATIONS_BASE}/store`, | ||
validate: false, | ||
}, | ||
async (context, request, response): Promise<any> => { | ||
const adaptor = getAdaptor(context, request); | ||
return handleWithCallback(adaptor, response, async (a: IntegrationsAdaptor) => | ||
a.getIntegrationInstances({}) | ||
); | ||
} | ||
); | ||
|
||
router.delete( | ||
{ | ||
path: `${INTEGRATIONS_BASE}/store/{id}`, | ||
validate: { | ||
params: schema.object({ | ||
id: schema.string(), | ||
}), | ||
}, | ||
}, | ||
async (context, request, response): Promise<any> => { | ||
const adaptor = getAdaptor(context, request); | ||
return handleWithCallback(adaptor, response, async (a: IntegrationsAdaptor) => | ||
a.deleteIntegrationInstance(request.params.id) | ||
); | ||
} | ||
); | ||
|
||
router.get( | ||
{ | ||
path: `${INTEGRATIONS_BASE}/store/{id}`, | ||
validate: { | ||
params: schema.object({ | ||
id: schema.string(), | ||
}), | ||
}, | ||
}, | ||
async (context, request, response): Promise<any> => { | ||
const adaptor = getAdaptor(context, request); | ||
return handleWithCallback(adaptor, response, async (a: IntegrationsAdaptor) => | ||
a.getIntegrationInstance({ | ||
id: request.params.id, | ||
}) | ||
); | ||
} | ||
); | ||
} |