[Synthetics] add monitors with defaults (#134355)

* synthetics - add monitors with defaults

* Update x-pack/test/api_integration/apis/uptime/rest/add_monitor.ts

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Dominique Clarke 2022-06-24 10:15:34 -04:00 committed by GitHub
parent 68143891fd
commit 4faa1175d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 76 additions and 4 deletions

View file

@ -14,6 +14,7 @@ import {
} from '../../../common/runtime_types';
import { UMRestApiRouteFactory } from '../../legacy_uptime/routes/types';
import { API_URLS } from '../../../common/constants';
import { DEFAULT_FIELDS } from '../../../common/constants/monitor_defaults';
import { syntheticsMonitorType } from '../../legacy_uptime/lib/saved_objects/synthetics_monitor';
import { validateMonitor } from './monitor_validation';
import { sendTelemetryEvents, formatTelemetryEvent } from '../telemetry/monitor_upgrade_sender';
@ -29,8 +30,13 @@ export const addSyntheticsMonitorRoute: UMRestApiRouteFactory = () => ({
},
handler: async ({ request, response, savedObjectsClient, server }): Promise<any> => {
const monitor: SyntheticsMonitor = request.body as SyntheticsMonitor;
const monitorType = monitor[ConfigKey.MONITOR_TYPE];
const monitorWithDefaults = {
...DEFAULT_FIELDS[monitorType],
...monitor,
};
const validationResult = validateMonitor(monitor as MonitorFields);
const validationResult = validateMonitor(monitorWithDefaults as MonitorFields);
if (!validationResult.valid) {
const { reason: message, details, payload } = validationResult;
@ -43,7 +49,7 @@ export const addSyntheticsMonitorRoute: UMRestApiRouteFactory = () => ({
newMonitor = await savedObjectsClient.create<EncryptedSyntheticsMonitor>(
syntheticsMonitorType,
formatSecrets({
...monitor,
...monitorWithDefaults,
revision: 1,
})
);

View file

@ -7,8 +7,9 @@
import { omit } from 'lodash';
import expect from '@kbn/expect';
import { secretKeys } from '@kbn/synthetics-plugin/common/constants/monitor_management';
import { HTTPFields } from '@kbn/synthetics-plugin/common/runtime_types';
import { DataStream, HTTPFields } from '@kbn/synthetics-plugin/common/runtime_types';
import { API_URLS } from '@kbn/synthetics-plugin/common/constants';
import { DEFAULT_FIELDS } from '@kbn/synthetics-plugin/common/constants/monitor_defaults';
import { ALL_SPACES_ID } from '@kbn/security-plugin/common/constants';
import { format as formatUrl } from 'url';
import supertest from 'supertest';
@ -46,7 +47,7 @@ export default function ({ getService }: FtrProviderContext) {
it('returns bad request if payload is invalid for HTTP monitor', async () => {
// Delete a required property to make payload invalid
const newMonitor = { ...httpMonitorJson, 'check.request.headers': undefined };
const newMonitor = { ...httpMonitorJson, 'check.request.headers': null };
const apiResponse = await supertestAPI
.post(API_URLS.SYNTHETICS_MONITORS)
@ -68,6 +69,71 @@ export default function ({ getService }: FtrProviderContext) {
expect(apiResponse.body.message).eql('Monitor type is invalid');
});
it('can create valid monitors without all defaults', async () => {
// Delete a required property to make payload invalid
const newMonitor = {
name: 'Sample name',
type: 'http',
urls: 'https://elastic.co',
locations: [
{
id: 'eu-west-01',
label: 'Europe West',
geo: {
lat: 33.2343132435,
lon: 73.2342343434,
},
url: 'https://example-url.com',
isServiceManaged: true,
},
],
};
const apiResponse = await supertestAPI
.post(API_URLS.SYNTHETICS_MONITORS)
.set('kbn-xsrf', 'true')
.send(newMonitor);
expect(apiResponse.status).eql(200);
expect(apiResponse.body.attributes).eql(
omit(
{
...DEFAULT_FIELDS[DataStream.HTTP],
...newMonitor,
revision: 1,
},
secretKeys
)
);
});
it('cannot create a valid monitor without a monitor type', async () => {
// Delete a required property to make payload invalid
const newMonitor = {
name: 'Sample name',
url: 'https://elastic.co',
locations: [
{
id: 'eu-west-01',
label: 'Europe West',
geo: {
lat: 33.2343132435,
lon: 73.2342343434,
},
url: 'https://example-url.com',
isServiceManaged: true,
},
],
};
const apiResponse = await supertestAPI
.post(API_URLS.SYNTHETICS_MONITORS)
.set('kbn-xsrf', 'true')
.send(newMonitor);
expect(apiResponse.status).eql(400);
});
it('can create monitor with API key with proper permissions', async () => {
await supertestAPI
.post('/internal/security/api_key')