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

[Fleet] Enable per policy outputs #126692

Merged
merged 5 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
2 changes: 2 additions & 0 deletions x-pack/plugins/fleet/common/constants/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ export const DEFAULT_OUTPUT: NewOutput = {
type: outputType.Elasticsearch,
hosts: [''],
};

export const LICENCE_FOR_PER_POLICY_OUTPUT = 'platinum';
14 changes: 14 additions & 0 deletions x-pack/plugins/fleet/common/openapi/bundled.json
Original file line number Diff line number Diff line change
Expand Up @@ -3825,6 +3825,14 @@
"logs"
]
}
},
"data_output_id": {
"type": "string",
"nullable": true
},
"monitoring_output_id": {
"type": "string",
"nullable": true
}
},
"required": [
Expand Down Expand Up @@ -3981,6 +3989,12 @@
"updated_by": {
"type": "string"
},
"data_output_id": {
"type": "string"
},
"monitoring_output_id": {
"type": "string"
},
"revision": {
"type": "number"
},
Expand Down
10 changes: 10 additions & 0 deletions x-pack/plugins/fleet/common/openapi/bundled.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2402,6 +2402,12 @@ components:
enum:
- metrics
- logs
data_output_id:
type: string
nullable: true
monitoring_output_id:
type: string
nullable: true
required:
- name
- namespace
Expand Down Expand Up @@ -2501,6 +2507,10 @@ components:
format: date-time
updated_by:
type: string
data_output_id:
type: string
monitoring_output_id:
type: string
revision:
type: number
agents:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ allOf:
format: date-time
updated_by:
type: string
data_output_id:
type: string
monitoring_output_id:
type: string
revision:
type: number
agents:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ properties:
enum:
- metrics
- logs
data_output_id:
type: string
nullable: true
monitoring_output_id:
type: string
nullable: true
required:
- name
- namespace
7 changes: 7 additions & 0 deletions x-pack/plugins/fleet/common/services/license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ export class LicenseService {
this.licenseInformation?.hasAtLeast('enterprise')
);
}
public isPlatinium() {
return (
this.licenseInformation?.isAvailable &&
this.licenseInformation?.isActive &&
this.licenseInformation?.hasAtLeast('platinum')
);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: this could be simpler probably, but then also maybe we don't need these helper methods for each license type?

Suggested change
return (
this.licenseInformation?.isAvailable &&
this.licenseInformation?.isActive &&
this.licenseInformation?.hasAtLeast('platinum')
);
return this.hasAtLeast('platinum');

Copy link
Member Author

Choose a reason for hiding this comment

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

👍 I used directly the hasAtLeast and refactored the existing helper to use hasAtLeast too

}
public hasAtLeast(licenseType: LicenseType) {
return (
this.licenseInformation?.isAvailable &&
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/fleet/common/types/models/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export interface NewAgentPolicy {
monitoring_enabled?: MonitoringType;
unenroll_timeout?: number;
is_preconfigured?: boolean;
data_output_id?: string;
monitoring_output_id?: string;
data_output_id?: string | null;
Copy link
Member Author

Choose a reason for hiding this comment

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

These fields are nullable to allow to clear them to use the default output

Copy link
Contributor

Choose a reason for hiding this comment

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

Good note. Would you mind adding that as a comment directly in the code?

monitoring_output_id?: string | null;
}

export interface AgentPolicy extends Omit<NewAgentPolicy, 'id'> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/*
* 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.
*/

import { createFleetTestRendererMock } from '../../../../../../mock';
import type { MockedFleetStartServices } from '../../../../../../mock';
import { useLicense } from '../../../../../../hooks/use_license';
import type { LicenseService } from '../../../../services';

import { useOutputOptions } from './hooks';

jest.mock('../../../../../../hooks/use_license');

const mockedUseLicence = useLicense as jest.MockedFunction<typeof useLicense>;

function defaultHttpClientGetImplementation(path: any) {
if (typeof path !== 'string') {
throw new Error('Invalid request');
}
const err = new Error(`API [GET ${path}] is not MOCKED!`);
// eslint-disable-next-line no-console
console.log(err);
throw err;
}

const mockApiCallsWithOutputs = (http: MockedFleetStartServices['http']) => {
http.get.mockImplementation(async (path) => {
if (typeof path !== 'string') {
throw new Error('Invalid request');
}
if (path === '/api/fleet/outputs') {
return {
data: {
items: [
{
id: 'output1',
name: 'Output 1',
is_default: true,
is_default_monitoring: true,
},
{
id: 'output2',
name: 'Output 2',
is_default: true,
is_default_monitoring: true,
},
{
id: 'output3',
name: 'Output 3',
is_default: true,
is_default_monitoring: true,
},
],
},
};
}

return defaultHttpClientGetImplementation(path);
});
};

describe('useOutputOptions', () => {
it('should generate enabled options if the licence is platinium', async () => {
const testRenderer = createFleetTestRendererMock();
mockedUseLicence.mockReturnValue({
isPlatinium: () => true,
} as unknown as LicenseService);
mockApiCallsWithOutputs(testRenderer.startServices.http);
const { result, waitForNextUpdate } = testRenderer.renderHook(() => useOutputOptions());
expect(result.current.isLoading).toBeTruthy();

await waitForNextUpdate();
expect(result.current.dataOutputOptions).toMatchInlineSnapshot(`
Array [
Object {
"inputDisplay": "Default (currently Output 1)",
"value": "@@##DEFAULT_OUTPUT_VALUE##@@",
},
Object {
"disabled": false,
"inputDisplay": "Output 1",
"value": "output1",
},
Object {
"disabled": false,
"inputDisplay": "Output 2",
"value": "output2",
},
Object {
"disabled": false,
"inputDisplay": "Output 3",
"value": "output3",
},
]
`);
expect(result.current.monitoringOutputOptions).toMatchInlineSnapshot(`
Array [
Object {
"inputDisplay": "Default (currently Output 1)",
"value": "@@##DEFAULT_OUTPUT_VALUE##@@",
},
Object {
"disabled": false,
"inputDisplay": "Output 1",
"value": "output1",
},
Object {
"disabled": false,
"inputDisplay": "Output 2",
"value": "output2",
},
Object {
"disabled": false,
"inputDisplay": "Output 3",
"value": "output3",
},
]
`);
});

it('should only enable the default options if the licence is not platinium', async () => {
const testRenderer = createFleetTestRendererMock();
mockedUseLicence.mockReturnValue({
isPlatinium: () => false,
} as unknown as LicenseService);
mockApiCallsWithOutputs(testRenderer.startServices.http);
const { result, waitForNextUpdate } = testRenderer.renderHook(() => useOutputOptions());
expect(result.current.isLoading).toBeTruthy();

await waitForNextUpdate();
expect(result.current.dataOutputOptions).toMatchInlineSnapshot(`
Array [
Object {
"inputDisplay": "Default (currently Output 1)",
"value": "@@##DEFAULT_OUTPUT_VALUE##@@",
},
Object {
"disabled": true,
"inputDisplay": "Output 1",
"value": "output1",
},
Object {
"disabled": true,
"inputDisplay": "Output 2",
"value": "output2",
},
Object {
"disabled": true,
"inputDisplay": "Output 3",
"value": "output3",
},
]
`);
expect(result.current.monitoringOutputOptions).toMatchInlineSnapshot(`
Array [
Object {
"inputDisplay": "Default (currently Output 1)",
"value": "@@##DEFAULT_OUTPUT_VALUE##@@",
},
Object {
"disabled": true,
"inputDisplay": "Output 1",
"value": "output1",
},
Object {
"disabled": true,
"inputDisplay": "Output 2",
"value": "output2",
},
Object {
"disabled": true,
"inputDisplay": "Output 3",
"value": "output3",
},
]
`);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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.
*/

import { useMemo } from 'react';
import type { EuiSuperSelectOption } from '@elastic/eui';

import { useGetOutputs, useLicense } from '../../../../hooks';

export const DEFAULT_OUTPUT_VALUE = '@@##DEFAULT_OUTPUT_VALUE##@@';
Copy link
Member Author

Choose a reason for hiding this comment

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

the super select component do not support null or '' as a value so I come with that not super elegant solution


export function useOutputOptions() {
const outputsRequest = useGetOutputs();
const { isPlatinium: isPlatiniumFn } = useLicense();

const isPlatinium = useMemo(() => isPlatiniumFn(), [isPlatiniumFn]);

const outputOptions: Array<EuiSuperSelectOption<string>> = useMemo(() => {
if (outputsRequest.isLoading || !outputsRequest.data) {
return [];
}

return outputsRequest.data.items.map((item) => ({
value: item.id,
inputDisplay: item.name,
disabled: !isPlatinium,
}));
}, [outputsRequest, isPlatinium]);

const dataOutputOptions = useMemo(() => {
if (outputsRequest.isLoading || !outputsRequest.data) {
return [];
}

const defaultOutputName = outputsRequest.data.items.find((item) => item.is_default)?.name;
return [
{ inputDisplay: `Default (currently ${defaultOutputName})`, value: DEFAULT_OUTPUT_VALUE },
...outputOptions,
]; // TODO translations
}, [outputsRequest, outputOptions]);

const monitoringOutputOptions = useMemo(() => {
if (outputsRequest.isLoading || !outputsRequest.data) {
return [];
}

const defaultOutputName = outputsRequest.data.items.find(
(item) => item.is_default_monitoring
)?.name;
return [
{ inputDisplay: `Default (currently ${defaultOutputName})`, value: DEFAULT_OUTPUT_VALUE },
...outputOptions,
]; // TODO translations
}, [outputsRequest, outputOptions]);

return useMemo(
() => ({
dataOutputOptions,
monitoringOutputOptions,
isLoading: outputsRequest.isLoading,
}),
[dataOutputOptions, monitoringOutputOptions, outputsRequest.isLoading]
);
}
Loading