WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit d40cdc4

Browse files
shahzad31dominiqueclarkejustinkambic
authored
[Synthetics] Add global params sync internal API !! (#239284)
## Summary These two endpoints are added to provide bit more control around the sync task functionality, they are currently not being used in UI. Following two endpoints have been added to be used internally !! `/internal/synthetics/sync_global_params` This will trigger syncing of global params to private locations. ``` `PUT /internal/synthetics/sync_global_params/_settings` { enable: false } ``` This can be used to disable/enable the task which performs auto syncing of global params to private locations. --------- Co-authored-by: Dominique Clarke <[email protected]> Co-authored-by: Justin Kambic <[email protected]>
1 parent f6b1a02 commit d40cdc4

File tree

7 files changed

+159
-48
lines changed

7 files changed

+159
-48
lines changed

x-pack/solutions/observability/plugins/synthetics/common/constants/synthetics/rest_api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export enum SYNTHETICS_API_URLS {
2424
SYNTHETICS_PROJECT_APIKEY = '/internal/synthetics/service/api_key',
2525
SYNTHETICS_HAS_INTEGRATION_MONITORS = '/internal/synthetics/fleet/has_integration_monitors',
2626
PRIVATE_LOCATIONS_CLEANUP = `/internal/synthetics/private_locations/_cleanup`,
27+
SYNC_GLOBAL_PARAMS = `/internal/synthetics/sync_global_params`,
28+
SYNC_GLOBAL_PARAMS_SETTINGS = `/internal/synthetics/sync_global_params/_settings`,
2729

2830
PINGS = '/internal/synthetics/pings',
2931
MONITOR_STATUS_HEATMAP = '/internal/synthetics/ping_heatmap',

x-pack/solutions/observability/plugins/synthetics/server/plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ export class Plugin implements PluginType {
9797

9898
this.syncPrivateLocationMonitorsTask = new SyncPrivateLocationMonitorsTask(
9999
this.server,
100-
plugins.taskManager,
101100
this.syntheticsMonitorClient
102101
);
102+
this.syncPrivateLocationMonitorsTask.registerTaskDefinition(plugins.taskManager);
103103

104104
plugins.embeddable.registerTransforms(SYNTHETICS_STATS_OVERVIEW_EMBEDDABLE, {
105105
transformOutInjectsReferences: true,

x-pack/solutions/observability/plugins/synthetics/server/routes/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* 2.0.
66
*/
77

8+
import { syncParamsSettingsParamsRoute } from './settings/params/sync_global_params_settings';
9+
import { syncParamsSyntheticsParamsRoute } from './settings/params/sync_global_params';
810
import { cleanupPrivateLocationRoute } from './settings/private_locations/cleanup_private_locations';
911
import { getSyntheticsTriggerTaskRun } from './tasks/trigger_task_run';
1012
import { syntheticsInspectStatusRuleRoute } from './rules/inspect_status_rule';
@@ -107,6 +109,8 @@ export const syntheticsAppRestApiRoutes: SyntheticsRestApiRouteFactory[] = [
107109
syntheticsInspectTLSRuleRoute,
108110
getSyntheticsTriggerTaskRun,
109111
cleanupPrivateLocationRoute,
112+
syncParamsSyntheticsParamsRoute,
113+
syncParamsSettingsParamsRoute,
110114
];
111115

112116
export const syntheticsAppPublicRestApiRoutes: SyntheticsRestApiRouteFactory[] = [
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import { SyncPrivateLocationMonitorsTask } from '../../../tasks/sync_private_locations_monitors_task';
9+
import { SYNTHETICS_API_URLS } from '../../../../common/constants';
10+
import { getPrivateLocations } from '../../../synthetics_service/get_private_locations';
11+
import type { SyntheticsRestApiRouteFactory } from '../../types';
12+
13+
export const syncParamsSyntheticsParamsRoute: SyntheticsRestApiRouteFactory = () => ({
14+
method: 'PUT',
15+
path: SYNTHETICS_API_URLS.SYNC_GLOBAL_PARAMS,
16+
validate: {},
17+
options: {
18+
// 5 minutes
19+
timeout: {
20+
payload: 1000 * 60 * 5,
21+
},
22+
},
23+
writeAccess: true,
24+
handler: async ({ syntheticsMonitorClient, server }): Promise<any> => {
25+
const soClient = server.coreStart.savedObjects.createInternalRepository();
26+
27+
const allPrivateLocations = await getPrivateLocations(soClient);
28+
29+
const syncTask = new SyncPrivateLocationMonitorsTask(server, syntheticsMonitorClient);
30+
if (allPrivateLocations.length > 0) {
31+
await syncTask.syncGlobalParams({
32+
allPrivateLocations,
33+
soClient,
34+
});
35+
}
36+
37+
return { success: true };
38+
},
39+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import { schema } from '@kbn/config-schema';
9+
import { disableSyncPrivateLocationTask } from '../../../tasks/sync_private_locations_monitors_task';
10+
import { SYNTHETICS_API_URLS } from '../../../../common/constants';
11+
import type { SyntheticsRestApiRouteFactory } from '../../types';
12+
13+
export const syncParamsSettingsParamsRoute: SyntheticsRestApiRouteFactory = () => ({
14+
method: 'PUT',
15+
path: SYNTHETICS_API_URLS.SYNC_GLOBAL_PARAMS_SETTINGS,
16+
validate: {
17+
body: schema.object({
18+
enable: schema.boolean(),
19+
}),
20+
},
21+
writeAccess: true,
22+
handler: async ({ server, request }): Promise<any> => {
23+
const { enable } = request.body as { enable: boolean };
24+
await disableSyncPrivateLocationTask({ server, disableAutoSync: !enable });
25+
return { success: true };
26+
},
27+
});

x-pack/solutions/observability/plugins/synthetics/server/tasks/sync_private_locations_monitors_task.test.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* 2.0.
66
*/
77

8-
import type { TaskManagerSetupContract } from '@kbn/task-manager-plugin/server/plugin';
98
import type { CustomTaskInstance } from './sync_private_locations_monitors_task';
109
import {
1110
SyncPrivateLocationMonitorsTask,
@@ -84,20 +83,20 @@ describe('SyncPrivateLocationMonitorsTask', () => {
8483
jest.clearAllMocks();
8584
task = new SyncPrivateLocationMonitorsTask(
8685
mockServerSetup as any,
87-
mockTaskManager as unknown as TaskManagerSetupContract,
8886
mockSyntheticsMonitorClient as unknown as SyntheticsMonitorClient
8987
);
9088
mockSoClient.createInternalRepository.mockReturnValue(mockSoClient as any);
9189
});
9290

9391
describe('constructor', () => {
9492
it('should register task definitions correctly', () => {
93+
task.registerTaskDefinition(mockTaskManager as any);
9594
expect(mockTaskManager.registerTaskDefinitions).toHaveBeenCalledWith({
9695
'Synthetics:Sync-Private-Location-Monitors': expect.objectContaining({
9796
title: 'Synthetics Sync Global Params Task',
9897
description:
9998
'This task is executed so that we can sync private location monitors for example when global params are updated',
100-
timeout: '5m',
99+
timeout: '10m',
101100
maxAttempts: 1,
102101
createTaskRunner: expect.any(Function),
103102
}),
@@ -150,6 +149,7 @@ describe('SyncPrivateLocationMonitorsTask', () => {
150149
expect(mockSyntheticsMonitorClient.privateLocationAPI.editMonitors).not.toHaveBeenCalled();
151150
expect(result.error).toBeUndefined();
152151
expect(result.state).toEqual({
152+
disableAutoSync: false,
153153
hasAlreadyDoneCleanup: false,
154154
lastStartedAt: expect.anything(),
155155
lastTotalParams: 1,
@@ -190,6 +190,7 @@ describe('SyncPrivateLocationMonitorsTask', () => {
190190
expect(task.syncGlobalParams).toHaveBeenCalled();
191191
expect(result.error).toBeUndefined();
192192
expect(result.state).toEqual({
193+
disableAutoSync: false,
193194
lastTotalParams: 1,
194195
lastTotalMWs: 1,
195196
maxCleanUpRetries: 2,
@@ -227,6 +228,7 @@ describe('SyncPrivateLocationMonitorsTask', () => {
227228
);
228229
expect(result.error).toBe(error);
229230
expect(result.state).toEqual({
231+
disableAutoSync: false,
230232
lastStartedAt: expect.anything(),
231233
lastTotalParams: 1,
232234
lastTotalMWs: 1,
@@ -414,7 +416,6 @@ describe('SyncPrivateLocationMonitorsTask', () => {
414416

415417
await task.syncGlobalParams({
416418
allPrivateLocations: mockAllPrivateLocations as any,
417-
encryptedSavedObjects: mockEncryptedSoClient as any,
418419
soClient: mockSoClient as any,
419420
});
420421

@@ -445,7 +446,6 @@ describe('SyncPrivateLocationMonitorsTask', () => {
445446
await task.syncGlobalParams({
446447
allPrivateLocations: [],
447448
soClient: mockSoClient as any,
448-
encryptedSavedObjects: mockEncryptedSoClient as any,
449449
});
450450

451451
expect(mockSyntheticsMonitorClient.privateLocationAPI.editMonitors).not.toHaveBeenCalled();
@@ -506,7 +506,6 @@ describe('SyncPrivateLocationMonitorsTask', () => {
506506
mockSoClient.createPointInTimeFinder = jest.fn().mockReturnValue(mockFinder);
507507
task = new SyncPrivateLocationMonitorsTask(
508508
mockServerSetup as any,
509-
mockTaskManager as unknown as TaskManagerSetupContract,
510509
mockSyntheticsMonitorClient as unknown as SyntheticsMonitorClient
511510
);
512511
});
@@ -519,10 +518,10 @@ describe('SyncPrivateLocationMonitorsTask', () => {
519518
);
520519
const result = await task.cleanUpDuplicatedPackagePolicies(mockSoClient as any, {} as any);
521520
expect(mockFleet.packagePolicyService.delete).not.toHaveBeenCalled();
522-
expect(result.performSync).toBe(false);
521+
expect(result.performCleanupSync).toBe(false);
523522
});
524523

525-
it('should delete unexpected policies and set performSync true', async () => {
524+
it('should delete unexpected policies and set performCleanupSync true', async () => {
526525
mockFleet.packagePolicyService.fetchAllItemIds.mockResolvedValue(
527526
(async function* () {
528527
yield ['monitor1-loc1-space1', 'unexpected-policy'];
@@ -535,30 +534,30 @@ describe('SyncPrivateLocationMonitorsTask', () => {
535534
['unexpected-policy'],
536535
{ force: true, spaceIds: ['*'] }
537536
);
538-
expect(result.performSync).toBe(true);
537+
expect(result.performCleanupSync).toBe(true);
539538
});
540539

541-
it('should set performSync true if expected policies are missing', async () => {
540+
it('should set performCleanupSync true if expected policies are missing', async () => {
542541
mockFleet.packagePolicyService.fetchAllItemIds.mockResolvedValue(
543542
(async function* () {
544543
yield [];
545544
})()
546545
);
547546
const result = await task.cleanUpDuplicatedPackagePolicies(mockSoClient as any, {} as any);
548-
expect(result.performSync).toBe(true);
547+
expect(result.performCleanupSync).toBe(true);
549548
});
550549

551-
it('should handle errors gracefully and return performSync', async () => {
550+
it('should handle errors gracefully and return performCleanupSync', async () => {
552551
mockFleet.packagePolicyService.fetchAllItemIds.mockRejectedValue(new Error('fail'));
553552
const result = await task.cleanUpDuplicatedPackagePolicies(mockSoClient as any, {} as any);
554553
expect(mockLogger.error).toHaveBeenCalled();
555-
expect(result).toHaveProperty('performSync');
554+
expect(result).toHaveProperty('performCleanupSync');
556555
});
557556

558557
it('should skip cleanup if hasAlreadyDoneCleanup is true', async () => {
559558
const state = { hasAlreadyDoneCleanup: true, maxCleanUpRetries: 3 };
560559
const result = await task.cleanUpDuplicatedPackagePolicies(mockSoClient as any, state as any);
561-
expect(result.performSync).toBe(false);
560+
expect(result.performCleanupSync).toBe(false);
562561
expect(mockLogger.debug).toHaveBeenCalledWith(
563562
'[SyncPrivateLocationMonitorsTask] Skipping cleanup of duplicated package policies as it has already been done once'
564563
);
@@ -567,7 +566,7 @@ describe('SyncPrivateLocationMonitorsTask', () => {
567566
it('should skip cleanup if maxCleanUpRetries is 0 or less', async () => {
568567
const state = { hasAlreadyDoneCleanup: false, maxCleanUpRetries: 0 };
569568
const result = await task.cleanUpDuplicatedPackagePolicies(mockSoClient as any, state as any);
570-
expect(result.performSync).toBe(false);
569+
expect(result.performCleanupSync).toBe(false);
571570
expect(state.hasAlreadyDoneCleanup).toBe(true);
572571
expect(state.maxCleanUpRetries).toBe(3);
573572
expect(mockLogger.debug).toHaveBeenCalledWith(
@@ -581,7 +580,7 @@ describe('SyncPrivateLocationMonitorsTask', () => {
581580
const state = { hasAlreadyDoneCleanup: false, maxCleanUpRetries: 2 };
582581
const result = await task.cleanUpDuplicatedPackagePolicies(mockSoClient as any, state as any);
583582
expect(state.maxCleanUpRetries).toBe(1);
584-
expect(result).toHaveProperty('performSync');
583+
expect(result).toHaveProperty('performCleanupSync');
585584
// Call again to reach 0
586585
await task.cleanUpDuplicatedPackagePolicies(mockSoClient as any, state as any);
587586
expect(state.hasAlreadyDoneCleanup).toBe(true);

0 commit comments

Comments
 (0)