Skip to content

Commit

Permalink
[Cases] Migrate to new Connectors APIs (#102662)
Browse files Browse the repository at this point in the history
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
cnasikas and kibanamachine committed Jul 29, 2021
1 parent fe1c272 commit 82e0ce1
Show file tree
Hide file tree
Showing 21 changed files with 126 additions and 79 deletions.
4 changes: 2 additions & 2 deletions api_docs/cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -4749,7 +4749,7 @@
"label": "ACTION_TYPES_URL",
"description": [],
"signature": [
"\"/api/actions/list_action_types\""
"\"/api/actions/connector_types\""
],
"source": {
"path": "x-pack/plugins/cases/common/constants.ts",
Expand Down Expand Up @@ -25001,4 +25001,4 @@
}
]
}
}
}
4 changes: 3 additions & 1 deletion x-pack/plugins/cases/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ export const CASE_DETAILS_ALERTS_URL = `${CASE_DETAILS_URL}/alerts`;
*/

export const ACTION_URL = '/api/actions';
export const ACTION_TYPES_URL = '/api/actions/list_action_types';
export const ACTION_TYPES_URL = `${ACTION_URL}/connector_types`;
export const CONNECTORS_URL = `${ACTION_URL}/connectors`;

export const SUPPORTED_CONNECTORS = [
`${ConnectorTypes.serviceNowITSM}`,
`${ConnectorTypes.serviceNowSIR}`,
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/cases/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
export * from './constants';
export * from './api';
export * from './ui/types';
export * from './utils/connectors_api';
38 changes: 38 additions & 0 deletions x-pack/plugins/cases/common/utils/connectors_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

/**
* Actions and connectors API endpoint helpers
*/

import { ACTION_URL, ACTION_TYPES_URL, CONNECTORS_URL } from '../../common';

/**
*
* @returns {string} Connector types endpoint
*/
export const getAllConnectorTypesUrl = (): string => ACTION_TYPES_URL;

/**
*
* @param connectorId
* @returns {string} Execute connector endpoint
*/
export const getExecuteConnectorUrl = (connectorId: string): string =>
`${ACTION_URL}/connector/${connectorId}/_execute`;

/**
*
* @returns {string} Create connector endpoint
*/
export const getCreateConnectorUrl = (): string => `${ACTION_URL}/connector`;

/**
*
* @returns {string} All connectors endpoint
*/
export const getAllConnectorsUrl = (): string => CONNECTORS_URL;
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ describe('Jira API', () => {
const res = await getIssueTypes({ http, signal: abortCtrl.signal, connectorId: 'test' });

expect(res).toEqual(issueTypesResponse);
expect(http.post).toHaveBeenCalledWith('/api/actions/action/test/_execute', {
expect(http.post).toHaveBeenCalledWith('/api/actions/connector/test/_execute', {
body: '{"params":{"subAction":"issueTypes","subActionParams":{}}}',
signal: abortCtrl.signal,
});
Expand All @@ -113,7 +113,7 @@ describe('Jira API', () => {
});

expect(res).toEqual(fieldsResponse);
expect(http.post).toHaveBeenCalledWith('/api/actions/action/test/_execute', {
expect(http.post).toHaveBeenCalledWith('/api/actions/connector/test/_execute', {
body: '{"params":{"subAction":"fieldsByIssueType","subActionParams":{"id":"10006"}}}',
signal: abortCtrl.signal,
});
Expand All @@ -132,7 +132,7 @@ describe('Jira API', () => {
});

expect(res).toEqual(issuesResponse);
expect(http.post).toHaveBeenCalledWith('/api/actions/action/test/_execute', {
expect(http.post).toHaveBeenCalledWith('/api/actions/connector/test/_execute', {
body: '{"params":{"subAction":"issues","subActionParams":{"title":"test issue"}}}',
signal: abortCtrl.signal,
});
Expand All @@ -151,7 +151,7 @@ describe('Jira API', () => {
});

expect(res).toEqual(issuesResponse);
expect(http.post).toHaveBeenCalledWith('/api/actions/action/test/_execute', {
expect(http.post).toHaveBeenCalledWith('/api/actions/connector/test/_execute', {
body: '{"params":{"subAction":"issue","subActionParams":{"id":"RJ-107"}}}',
signal: abortCtrl.signal,
});
Expand Down
24 changes: 10 additions & 14 deletions x-pack/plugins/cases/public/components/connectors/jira/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,22 @@

import { HttpSetup } from 'kibana/public';
import { ActionTypeExecutorResult } from '../../../../../actions/common';
import { getExecuteConnectorUrl } from '../../../../common/utils/connectors_api';
import { IssueTypes, Fields, Issues, Issue } from './types';

export const BASE_ACTION_API_PATH = '/api/actions';

export interface GetIssueTypesProps {
http: HttpSetup;
signal: AbortSignal;
connectorId: string;
}

export async function getIssueTypes({ http, signal, connectorId }: GetIssueTypesProps) {
return http.post<ActionTypeExecutorResult<IssueTypes>>(
`${BASE_ACTION_API_PATH}/action/${connectorId}/_execute`,
{
body: JSON.stringify({
params: { subAction: 'issueTypes', subActionParams: {} },
}),
signal,
}
);
return http.post<ActionTypeExecutorResult<IssueTypes>>(getExecuteConnectorUrl(connectorId), {
body: JSON.stringify({
params: { subAction: 'issueTypes', subActionParams: {} },
}),
signal,
});
}

export interface GetFieldsByIssueTypeProps {
Expand All @@ -42,7 +38,7 @@ export async function getFieldsByIssueType({
connectorId,
id,
}: GetFieldsByIssueTypeProps): Promise<ActionTypeExecutorResult<Fields>> {
return http.post(`${BASE_ACTION_API_PATH}/action/${connectorId}/_execute`, {
return http.post(getExecuteConnectorUrl(connectorId), {
body: JSON.stringify({
params: { subAction: 'fieldsByIssueType', subActionParams: { id } },
}),
Expand All @@ -63,7 +59,7 @@ export async function getIssues({
connectorId,
title,
}: GetIssuesTypeProps): Promise<ActionTypeExecutorResult<Issues>> {
return http.post(`${BASE_ACTION_API_PATH}/action/${connectorId}/_execute`, {
return http.post(getExecuteConnectorUrl(connectorId), {
body: JSON.stringify({
params: { subAction: 'issues', subActionParams: { title } },
}),
Expand All @@ -84,7 +80,7 @@ export async function getIssue({
connectorId,
id,
}: GetIssueTypeProps): Promise<ActionTypeExecutorResult<Issue>> {
return http.post(`${BASE_ACTION_API_PATH}/action/${connectorId}/_execute`, {
return http.post(getExecuteConnectorUrl(connectorId), {
body: JSON.stringify({
params: { subAction: 'issue', subActionParams: { id } },
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { HttpSetup } from 'kibana/public';
import { ActionTypeExecutorResult } from '../../../../../actions/common';
import { getExecuteConnectorUrl } from '../../../../common/utils/connectors_api';
import { ResilientIncidentTypes, ResilientSeverity } from './types';

export const BASE_ACTION_API_PATH = '/api/actions';
Expand All @@ -19,7 +20,7 @@ export interface Props {

export async function getIncidentTypes({ http, signal, connectorId }: Props) {
return http.post<ActionTypeExecutorResult<ResilientIncidentTypes>>(
`${BASE_ACTION_API_PATH}/action/${connectorId}/_execute`,
getExecuteConnectorUrl(connectorId),
{
body: JSON.stringify({
params: { subAction: 'incidentTypes', subActionParams: {} },
Expand All @@ -31,7 +32,7 @@ export async function getIncidentTypes({ http, signal, connectorId }: Props) {

export async function getSeverity({ http, signal, connectorId }: Props) {
return http.post<ActionTypeExecutorResult<ResilientSeverity>>(
`${BASE_ACTION_API_PATH}/action/${connectorId}/_execute`,
getExecuteConnectorUrl(connectorId),
{
body: JSON.stringify({
params: { subAction: 'severity', subActionParams: {} },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('ServiceNow API', () => {
});

expect(res).toEqual(choicesResponse);
expect(http.post).toHaveBeenCalledWith('/api/actions/action/test/_execute', {
expect(http.post).toHaveBeenCalledWith('/api/actions/connector/test/_execute', {
body: '{"params":{"subAction":"getChoices","subActionParams":{"fields":["priority"]}}}',
signal: abortCtrl.signal,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { HttpSetup } from 'kibana/public';
import { ActionTypeExecutorResult } from '../../../../../actions/common';
import { getExecuteConnectorUrl } from '../../../../common/utils/connectors_api';
import { Choice } from './types';

export const BASE_ACTION_API_PATH = '/api/actions';
Expand All @@ -19,13 +20,10 @@ export interface GetChoicesProps {
}

export async function getChoices({ http, signal, connectorId, fields }: GetChoicesProps) {
return http.post<ActionTypeExecutorResult<Choice[]>>(
`${BASE_ACTION_API_PATH}/action/${connectorId}/_execute`,
{
body: JSON.stringify({
params: { subAction: 'getChoices', subActionParams: { fields } },
}),
signal,
}
);
return http.post<ActionTypeExecutorResult<Choice[]>>(getExecuteConnectorUrl(connectorId), {
body: JSON.stringify({
params: { subAction: 'getChoices', subActionParams: { fields } },
}),
signal,
});
}
2 changes: 1 addition & 1 deletion x-pack/plugins/cases/public/containers/api.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe('Case Configuration API', () => {

test('check url, method, signal', async () => {
await getActionLicense(abortCtrl.signal);
expect(fetchMock).toHaveBeenCalledWith(`/api/actions/list_action_types`, {
expect(fetchMock).toHaveBeenCalledWith(`/api/actions/connector_types`, {
method: 'GET',
signal: abortCtrl.signal,
});
Expand Down
17 changes: 11 additions & 6 deletions x-pack/plugins/cases/public/containers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import { assign, omit } from 'lodash';

import {
ACTION_TYPES_URL,
CASE_REPORTERS_URL,
CASE_STATUS_URL,
CASE_TAGS_URL,
Expand Down Expand Up @@ -38,6 +37,8 @@ import {
User,
} from '../../common';

import { getAllConnectorTypesUrl } from '../../common/utils/connectors_api';

import { KibanaServices } from '../common/lib/kibana';

import {
Expand Down Expand Up @@ -351,9 +352,13 @@ export const pushCase = async (
};

export const getActionLicense = async (signal: AbortSignal): Promise<ActionLicense[]> => {
const response = await KibanaServices.get().http.fetch<ActionLicense[]>(ACTION_TYPES_URL, {
method: 'GET',
signal,
});
return response;
const response = await KibanaServices.get().http.fetch<ActionLicense[]>(
getAllConnectorTypesUrl(),
{
method: 'GET',
signal,
}
);

return convertArrayToCamelCase(response) as ActionLicense[];
};
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ describe('Case Configuration API', () => {

test('check url, method, signal', async () => {
await fetchActionTypes({ signal: abortCtrl.signal });
expect(fetchMock).toHaveBeenCalledWith('/api/actions/list_action_types', {
expect(fetchMock).toHaveBeenCalledWith('/api/actions/connector_types', {
method: 'GET',
signal: abortCtrl.signal,
});
Expand Down
16 changes: 4 additions & 12 deletions x-pack/plugins/cases/public/containers/configure/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/

import { isEmpty } from 'lodash/fp';
import { getAllConnectorTypesUrl } from '../../../common/utils/connectors_api';
import {
ACTION_TYPES_URL,
ActionConnector,
ActionTypeConnector,
CASE_CONFIGURE_CONNECTORS_URL,
Expand All @@ -22,6 +22,7 @@ import { KibanaServices } from '../../common/lib/kibana';

import { ApiProps } from '../types';
import {
convertArrayToCamelCase,
convertToCamelCase,
decodeCaseConfigurationsResponse,
decodeCaseConfigureResponse,
Expand Down Expand Up @@ -60,15 +61,6 @@ export const getCaseConfigure = async ({
return null;
};

export const getConnectorMappings = async ({ signal }: ApiProps): Promise<ActionConnector[]> => {
const response = await KibanaServices.get().http.fetch(`${CASE_CONFIGURE_CONNECTORS_URL}/_find`, {
method: 'GET',
signal,
});

return response;
};

export const postCaseConfigure = async (
caseConfiguration: CasesConfigureRequest,
signal: AbortSignal
Expand Down Expand Up @@ -105,10 +97,10 @@ export const patchCaseConfigure = async (
};

export const fetchActionTypes = async ({ signal }: ApiProps): Promise<ActionTypeConnector[]> => {
const response = await KibanaServices.get().http.fetch(ACTION_TYPES_URL, {
const response = await KibanaServices.get().http.fetch(getAllConnectorTypesUrl(), {
method: 'GET',
signal,
});

return response;
return convertArrayToCamelCase(response) as ActionTypeConnector[];
};
8 changes: 6 additions & 2 deletions x-pack/plugins/cases/server/scripts/sub_cases/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import {
} from '../../../common';
import { ActionResult, ActionTypeExecutorResult } from '../../../../actions/common';
import { ContextTypeGeneratedAlertType, createAlertsString } from '../../connectors';
import {
getCreateConnectorUrl,
getExecuteConnectorUrl,
} from '../../../common/utils/connectors_api';

main();

Expand Down Expand Up @@ -71,7 +75,7 @@ async function handleGenGroupAlerts(argv: any) {

try {
const createdAction = await client.request<ActionResult>({
path: '/api/actions/action',
path: getCreateConnectorUrl(),
method: 'POST',
body: {
name: 'A case connector',
Expand Down Expand Up @@ -121,7 +125,7 @@ async function handleGenGroupAlerts(argv: any) {
};

const executeResp = await client.request<ActionTypeExecutorResult<CaseResponse>>({
path: `/api/actions/action/${createdAction.data.id}/_execute`,
path: getExecuteConnectorUrl(createdAction.data.id),
method: 'POST',
body: {
params: {
Expand Down
Loading

0 comments on commit 82e0ce1

Please sign in to comment.