Skip to content

Commit

Permalink
[Ingest][Endpoint] Add Ingest rest api response types for use in Endp…
Browse files Browse the repository at this point in the history
…oint (#63373)

* Added REST Response types to datasouces
* Adjust Types in Endpoint to reference those from Ingest
* Remove un-used endpoint type
  • Loading branch information
paul-tavares authored Apr 14, 2020
1 parent 7cb3ee4 commit a4464c2
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
MiddlewareFactory,
PolicyData,
PolicyDetailsState,
UpdateDatasourceResponse,
UpdatePolicyResponse,
} from '../../types';
import { policyIdFromParams, isOnPolicyDetailsPage, policyDetails } from './selectors';
import { generatePolicy } from '../../models/policy';
Expand Down Expand Up @@ -77,7 +77,7 @@ export const policyDetailsMiddlewareFactory: MiddlewareFactory<PolicyDetailsStat
} else if (action.type === 'userClickedPolicyDetailsSaveButton') {
const { id, revision, ...updatedPolicyItem } = policyDetails(state) as PolicyData;

let apiResponse: UpdateDatasourceResponse;
let apiResponse: UpdatePolicyResponse;
try {
apiResponse = await sendPutDatasource(http, id, updatedPolicyItem);
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { MiddlewareFactory, PolicyListState, GetDatasourcesResponse } from '../../types';
import { GetPolicyListResponse, MiddlewareFactory, PolicyListState } from '../../types';
import { sendGetEndpointSpecificDatasources } from './services/ingest';
import { isOnPolicyListPage, urlSearchParams } from './selectors';

Expand All @@ -18,7 +18,7 @@ export const policyListMiddlewareFactory: MiddlewareFactory<PolicyListState> = c

if (action.type === 'userChangedUrl' && isOnPolicyListPage(state)) {
const { page_index: pageIndex, page_size: pageSize } = urlSearchParams(state);
let response: GetDatasourcesResponse;
let response: GetPolicyListResponse;

try {
response = await sendGetEndpointSpecificDatasources(http, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import {
} from '../../../../../../../ingest_manager/common';
import {
NewPolicyData,
GetDatasourcesResponse,
GetDatasourceResponse,
UpdateDatasourceResponse,
GetPolicyListResponse,
GetPolicyResponse,
UpdatePolicyResponse,
} from '../../../types';

const INGEST_API_ROOT = `/api/ingest_manager`;
Expand All @@ -30,8 +30,8 @@ const INGEST_API_FLEET_AGENT_STATUS = `${INGEST_API_FLEET}/agent-status`;
export const sendGetEndpointSpecificDatasources = (
http: HttpStart,
options: HttpFetchOptions & Partial<GetDatasourcesRequest> = {}
): Promise<GetDatasourcesResponse> => {
return http.get<GetDatasourcesResponse>(INGEST_API_DATASOURCES, {
): Promise<GetPolicyListResponse> => {
return http.get<GetPolicyListResponse>(INGEST_API_DATASOURCES, {
...options,
query: {
...options.query,
Expand All @@ -53,7 +53,7 @@ export const sendGetDatasource = (
datasourceId: string,
options?: HttpFetchOptions
) => {
return http.get<GetDatasourceResponse>(`${INGEST_API_DATASOURCES}/${datasourceId}`, options);
return http.get<GetPolicyResponse>(`${INGEST_API_DATASOURCES}/${datasourceId}`, options);
};

/**
Expand All @@ -69,7 +69,7 @@ export const sendPutDatasource = (
datasourceId: string,
datasource: NewPolicyData,
options: Exclude<HttpFetchOptions, 'body'> = {}
): Promise<UpdateDatasourceResponse> => {
): Promise<UpdatePolicyResponse> => {
return http.put(`${INGEST_API_DATASOURCES}/${datasourceId}`, {
...options,
body: JSON.stringify(datasource),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { HttpStart } from 'kibana/public';
import { Dispatch } from 'redux';
import { INGEST_API_DATASOURCES } from './services/ingest';
import { EndpointDocGenerator } from '../../../../../common/generate_data';
import { AppAction, GetDatasourcesResponse, GlobalState, MiddlewareFactory } from '../../types';
import { AppAction, GetPolicyListResponse, GlobalState, MiddlewareFactory } from '../../types';

const generator = new EndpointDocGenerator('policy-list');

Expand All @@ -19,13 +19,13 @@ const generator = new EndpointDocGenerator('policy-list');
*/
export const setPolicyListApiMockImplementation = (
mockedHttpService: jest.Mocked<HttpStart>,
responseItems: GetDatasourcesResponse['items'] = [generator.generatePolicyDatasource()]
responseItems: GetPolicyListResponse['items'] = [generator.generatePolicyDatasource()]
): void => {
mockedHttpService.get.mockImplementation((...args) => {
const [path] = args;
if (typeof path === 'string') {
if (path === INGEST_API_DATASOURCES) {
return Promise.resolve<GetDatasourcesResponse>({
return Promise.resolve<GetPolicyListResponse>({
items: responseItems,
total: 10,
page: 1,
Expand Down
27 changes: 10 additions & 17 deletions x-pack/plugins/endpoint/public/applications/endpoint/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ import {
import { EndpointPluginStartDependencies } from '../../plugin';
import { AppAction } from './store/action';
import { CoreStart } from '../../../../../../src/core/public';
import { Datasource, NewDatasource } from '../../../../ingest_manager/common/types/models';
import {
Datasource,
NewDatasource,
GetAgentStatusResponse,
CreateDatasourceResponse,
} from '../../../../ingest_manager/common/types/rest_spec';
GetDatasourcesResponse,
GetOneDatasourceResponse,
UpdateDatasourceResponse,
} from '../../../../ingest_manager/common';

export { AppAction };
export type MiddlewareFactory<S = GlobalState> = (
Expand Down Expand Up @@ -335,24 +338,14 @@ export interface AlertingIndexUIQueryParams {
filters?: string;
}

export interface GetDatasourcesResponse {
export interface GetPolicyListResponse extends GetDatasourcesResponse {
items: PolicyData[];
total: number;
page: number;
perPage: number;
success: boolean;
}

export interface GetDatasourceResponse {
export interface GetPolicyResponse extends GetOneDatasourceResponse {
item: PolicyData;
success: boolean;
}

export type UpdateDatasourceResponse = CreateDatasourceResponse & {
export interface UpdatePolicyResponse extends UpdateDatasourceResponse {
item: PolicyData;
};

/**
* The PageId type is used for the payload when firing userNavigatedToPage actions
*/
export type PageId = 'alertsPage' | 'managementPage' | 'policyListPage';
}
17 changes: 12 additions & 5 deletions x-pack/plugins/ingest_manager/common/types/rest_spec/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,22 @@ export interface GetDatasourcesRequest {
};
}

export interface GetDatasourcesResponse {
items: Datasource[];
total: number;
page: number;
perPage: number;
success: boolean;
}

export interface GetOneDatasourceRequest {
params: {
datasourceId: string;
};
}

export interface GetDatasourcesResponse {
items: Datasource[];
total: number;
page: number;
perPage: number;
export interface GetOneDatasourceResponse {
item: Datasource;
success: boolean;
}

Expand All @@ -40,6 +45,8 @@ export type UpdateDatasourceRequest = GetOneDatasourceRequest & {
body: NewDatasource;
};

export type UpdateDatasourceResponse = CreateDatasourceResponse;

export interface DeleteDatasourcesRequest {
body: {
datasourceIds: string[];
Expand Down

0 comments on commit a4464c2

Please sign in to comment.