Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented actions server API for supporting preconfigured connectors #62382

Merged
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const AlertsConfiguration: React.FC<AlertsConfigurationProps> = (
async function fetchEmailActions() {
const kibanaActions = await kfetch({
method: 'GET',
pathname: `/api/action/_find`,
pathname: `/api/action/_getAll`,
});

const actions = kibanaActions.data.filter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('Step1', () => {
actionTypeId: '1abc',
name: 'Testing',
config: {},
isPreconfigured: false,
},
];
const selectedEmailActionId = emailActions[0].id;
Expand Down Expand Up @@ -83,6 +84,7 @@ describe('Step1', () => {
actionTypeId: '.email',
name: '',
config: {},
isPreconfigured: false,
},
],
selectedEmailActionId: NEW_ACTION_ID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { isEmpty } from 'lodash/fp';
import {
CasesConnectorsFindResult,
Connector,
CasesConfigurePatch,
CasesConfigureResponse,
CasesConfigureRequest,
Expand All @@ -18,7 +18,7 @@ import { ApiProps } from '../types';
import { convertToCamelCase, decodeCaseConfigureResponse } from '../utils';
import { CaseConfigure } from './types';

export const fetchConnectors = async ({ signal }: ApiProps): Promise<CasesConnectorsFindResult> => {
export const fetchConnectors = async ({ signal }: ApiProps): Promise<Connector[]> => {
const response = await KibanaServices.get().http.fetch(
`${CASES_CONFIGURE_URL}/connectors/_find`,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const useConnectors = (): ReturnConnectors => {
const res = await fetchConnectors({ signal: abortCtrl.signal });
if (!didCancel) {
setLoading(false);
setConnectors(res.data);
setConnectors(res);
}
} catch (error) {
if (!didCancel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ export const createActionResult = (): ActionResult => ({
actionTypeId: 'action-id-1',
name: '',
config: {},
isPreconfigured: false,
});

export const nonRuleAlert = () => ({
Expand Down Expand Up @@ -512,6 +513,7 @@ export const updateActionResult = (): ActionResult => ({
actionTypeId: 'action-id-1',
name: '',
config: {},
isPreconfigured: false,
});

export const getMockPrivilegesResult = () => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ set -e
# https:/elastic/kibana/blob/master/x-pack/legacy/plugins/actions/README.md#get-apiaction_find-find-actions
curl -s -k \
-u ${ELASTICSEARCH_USERNAME}:${ELASTICSEARCH_PASSWORD} \
-X GET ${KIBANA_URL}${SPACE_URL}/api/action/_find \
-X GET ${KIBANA_URL}${SPACE_URL}/api/action/_getAll \
| jq .
10 changes: 6 additions & 4 deletions x-pack/plugins/actions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Table of Contents
- [RESTful API](#restful-api)
- [`POST /api/action`: Create action](#post-apiaction-create-action)
- [`DELETE /api/action/{id}`: Delete action](#delete-apiactionid-delete-action)
- [`GET /api/action/_find`: Find actions](#get-apiactionfind-find-actions)
- [`GET /api/action/_getAll`: Get all actions](#get-apiaction-get-all-actions)
- [`GET /api/action/{id}`: Get action](#get-apiactionid-get-action)
- [`GET /api/action/types`: List action types](#get-apiactiontypes-list-action-types)
- [`PUT /api/action/{id}`: Update action](#put-apiactionid-update-action)
Expand Down Expand Up @@ -174,11 +174,13 @@ Params:
| -------- | --------------------------------------------- | ------ |
| id | The id of the action you're trying to delete. | string |

### `GET /api/action/_find`: Find actions
### `GET /api/action/_getAll`: Get all actions

Params:
No parameters.

See the [saved objects API documentation for find](https://www.elastic.co/guide/en/kibana/master/saved-objects-api-find.html). All the properties are the same except that you cannot pass in `type`.
Return all actions from saved objects merged with predefined list.
Use the [saved objects API for find](https://www.elastic.co/guide/en/kibana/master/saved-objects-api-find.html) with the proprties: `type: 'action'` and `perPage: 10000`.
List of predefined actions should be set up in Kibana.yaml.

### `GET /api/action/{id}`: Get action

Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/actions/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ export interface ActionResult {
actionTypeId: string;
name: string;
config: Record<string, any>;
isPreconfigured: boolean;
}
2 changes: 1 addition & 1 deletion x-pack/plugins/actions/server/actions_client.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ const createActionsClientMock = () => {
const mocked: jest.Mocked<ActionsClientContract> = {
create: jest.fn(),
get: jest.fn(),
find: jest.fn(),
delete: jest.fn(),
update: jest.fn(),
getAll: jest.fn(),
};
return mocked;
};
Expand Down
94 changes: 78 additions & 16 deletions x-pack/plugins/actions/server/actions_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ beforeEach(() => {
savedObjectsClient,
scopedClusterClient,
defaultKibanaIndex,
preconfiguredActions: [],
});
});

Expand Down Expand Up @@ -83,6 +84,7 @@ describe('create()', () => {
});
expect(result).toEqual({
id: '1',
isPreconfigured: false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering why isPreconfigured is something we're returning here. Guessing it will be nice, eventually, to somehow indicate this to the user, with some kind of "lock" icon or something. But I don't think it's really needed for anything but that, is it? Just curious, I'm +1 on having it, now that I see it, but guessing I might not have returned it if I had implemented this PR.

name: 'my name',
actionTypeId: 'my-action-type',
config: {},
Expand Down Expand Up @@ -178,6 +180,7 @@ describe('create()', () => {
});
expect(result).toEqual({
id: '1',
isPreconfigured: false,
name: 'my name',
actionTypeId: 'my-action-type',
config: {
Expand Down Expand Up @@ -226,6 +229,7 @@ describe('create()', () => {
savedObjectsClient,
scopedClusterClient,
defaultKibanaIndex,
preconfiguredActions: [],
});

const savedObjectCreateResult = {
Expand Down Expand Up @@ -305,6 +309,7 @@ describe('get()', () => {
const result = await actionsClient.get({ id: '1' });
expect(result).toEqual({
id: '1',
isPreconfigured: false,
});
expect(savedObjectsClient.get).toHaveBeenCalledTimes(1);
expect(savedObjectsClient.get.mock.calls[0]).toMatchInlineSnapshot(`
Expand All @@ -314,9 +319,44 @@ describe('get()', () => {
]
`);
});

test('return predefined action with id', async () => {
actionsClient = new ActionsClient({
actionTypeRegistry,
savedObjectsClient,
scopedClusterClient,
defaultKibanaIndex,
preconfiguredActions: [
{
id: 'testPreconfigured',
actionTypeId: '.slack',
secrets: {
test: 'test1',
},
isPreconfigured: true,
name: 'test',
config: {
foo: 'bar',
},
},
],
});

const result = await actionsClient.get({ id: 'testPreconfigured' });
expect(result).toEqual({
id: 'testPreconfigured',
actionTypeId: '.slack',
isPreconfigured: true,
name: 'test',
config: {
foo: 'bar',
},
});
expect(savedObjectsClient.get).not.toHaveBeenCalled();
});
});

describe('find()', () => {
describe('getAll()', () => {
test('calls savedObjectsClient with parameters', async () => {
const expectedResult = {
total: 1,
Expand All @@ -327,6 +367,7 @@ describe('find()', () => {
id: '1',
type: 'type',
attributes: {
name: 'test',
config: {
foo: 'bar',
},
Expand All @@ -339,31 +380,50 @@ describe('find()', () => {
scopedClusterClient.callAsInternalUser.mockResolvedValueOnce({
aggregations: {
'1': { doc_count: 6 },
testPreconfigured: { doc_count: 2 },
},
});
const result = await actionsClient.find({});
expect(result).toEqual({
total: 1,
perPage: 10,
page: 1,
data: [

actionsClient = new ActionsClient({
actionTypeRegistry,
savedObjectsClient,
scopedClusterClient,
defaultKibanaIndex,
preconfiguredActions: [
{
id: '1',
id: 'testPreconfigured',
actionTypeId: '.slack',
secrets: {},
isPreconfigured: true,
name: 'test',
config: {
foo: 'bar',
},
referencedByCount: 6,
},
],
});
expect(savedObjectsClient.find).toHaveBeenCalledTimes(1);
expect(savedObjectsClient.find.mock.calls[0]).toMatchInlineSnapshot(`
Array [
Object {
"type": "action",
const result = await actionsClient.getAll();
expect(result).toEqual([
{
id: '1',
isPreconfigured: false,
name: 'test',
config: {
foo: 'bar',
},
]
`);
referencedByCount: 6,
},
{
id: 'testPreconfigured',
actionTypeId: '.slack',
isPreconfigured: true,
name: 'test',
config: {
foo: 'bar',
},
referencedByCount: 2,
},
]);
});
});

Expand Down Expand Up @@ -420,6 +480,7 @@ describe('update()', () => {
});
expect(result).toEqual({
id: 'my-action',
isPreconfigured: false,
actionTypeId: 'my-action-type',
name: 'my name',
config: {},
Expand Down Expand Up @@ -524,6 +585,7 @@ describe('update()', () => {
});
expect(result).toEqual({
id: 'my-action',
isPreconfigured: false,
actionTypeId: 'my-action-type',
name: 'my name',
config: {
Expand Down
Loading