Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export default function ({ getService }: FtrProviderContext) {
'ProductDocBase:UninstallAll',
'SLO:ORPHAN_SUMMARIES-CLEANUP-TASK',
'Synthetics:Clean-Up-Package-Policies',
'Synthetics:Sync-Global-Params-Private-Locations',
'Synthetics:Sync-Private-Location-Monitors',
'UPTIME:SyntheticsService:Sync-Saved-Monitor-Objects',
'actions:.bedrock',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
import { SavedObjectsClient } from '@kbn/core/server';
import { mappingFromFieldMap } from '@kbn/alerting-plugin/common';
import { Dataset } from '@kbn/rule-registry-plugin/server';
import { SyncGlobalParamsPrivateLocationsTask } from './tasks/sync_global_params_task';
import type {
SyntheticsPluginsSetupDependencies,
SyntheticsPluginsStartDependencies,
Expand All @@ -40,6 +41,7 @@ export class Plugin implements PluginType {
private syntheticsMonitorClient?: SyntheticsMonitorClient;
private readonly telemetryEventsSender: TelemetryEventsSender;
private syncPrivateLocationMonitorsTask?: SyncPrivateLocationMonitorsTask;
private syncGlobalParamsTask?: SyncGlobalParamsPrivateLocationsTask;

constructor(private readonly initContext: PluginInitializerContext<UptimeConfig>) {
this.logger = initContext.logger.get();
Expand Down Expand Up @@ -97,6 +99,14 @@ export class Plugin implements PluginType {
this.syntheticsMonitorClient
);

this.syncGlobalParamsTask = new SyncGlobalParamsPrivateLocationsTask(
this.server,
plugins.taskManager,
this.syntheticsMonitorClient
);

this.syncGlobalParamsTask.registerTaskDefinition(plugins.taskManager);

return {};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { schema } from '@kbn/config-schema';
import { ALL_SPACES_ID } from '@kbn/security-plugin/common/constants';
import { DEFAULT_SPACE_ID } from '@kbn/spaces-plugin/common';
import type { SavedObject, SavedObjectsBulkCreateObject } from '@kbn/core-saved-objects-api-server';
import { runSynPrivateLocationMonitorsTaskSoon } from '../../../tasks/sync_private_locations_monitors_task';
import type { SyntheticsRestApiRouteFactory } from '../../types';
import type {
SyntheticsParamRequest,
Expand All @@ -18,6 +17,7 @@ import type {
} from '../../../../common/runtime_types';
import { syntheticsParamType } from '../../../../common/types/saved_objects';
import { SYNTHETICS_API_URLS } from '../../../../common/constants';
import { asyncGlobalParamsPropagation } from '../../../tasks/sync_global_params_task';

const ParamsObjectSchema = schema.object({
key: schema.string({
Expand Down Expand Up @@ -57,8 +57,16 @@ export const addSyntheticsParamsRoute: SyntheticsRestApiRouteFactory<
savedObjectsData
);

await runSynPrivateLocationMonitorsTaskSoon({
await asyncGlobalParamsPropagation({
server,
paramsSpacesToSync: Array.from(
new Set(
savedObjectsData.reduce(
(spacesToSync, obj) => spacesToSync.concat(obj.initialNamespaces || []),
[] as string[]
)
)
),
});

if (savedObjectsData.length > 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

import { schema } from '@kbn/config-schema';
import { i18n } from '@kbn/i18n';
import { runSynPrivateLocationMonitorsTaskSoon } from '../../../tasks/sync_private_locations_monitors_task';
import type { SavedObjectsClientContract } from '@kbn/core-saved-objects-api-server';
import type { SyntheticsRestApiRouteFactory } from '../../types';
import { syntheticsParamType } from '../../../../common/types/saved_objects';
import { SYNTHETICS_API_URLS } from '../../../../common/constants';
import type { DeleteParamsResponse } from '../../../../common/runtime_types';
import { asyncGlobalParamsPropagation } from '../../../tasks/sync_global_params_task';

export const deleteSyntheticsParamsRoute: SyntheticsRestApiRouteFactory<
DeleteParamsResponse[],
Expand Down Expand Up @@ -58,14 +59,33 @@ export const deleteSyntheticsParamsRoute: SyntheticsRestApiRouteFactory<
});
}

const existingParamsSpaces = await getExistingParamsSpaces(savedObjectsClient, idsToDelete);

const result = await savedObjectsClient.bulkDelete(
idsToDelete.map((id) => ({ type: syntheticsParamType, id })),
{ force: true }
);
await runSynPrivateLocationMonitorsTaskSoon({
await asyncGlobalParamsPropagation({
server,
paramsSpacesToSync: existingParamsSpaces,
});

return result.statuses.map(({ id, success }) => ({ id, deleted: success }));
},
});

export async function getExistingParamsSpaces(
savedObjectsClient: SavedObjectsClientContract,
paramIds: string[]
) {
const existingParam = await savedObjectsClient.bulkGet(
paramIds.map((id) => ({ type: syntheticsParamType, id }))
);
return Array.from(
new Set(
existingParam.saved_objects.reduce((spaces, obj) => {
return spaces.concat(obj.namespaces ?? []);
}, [] as string[])
)
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
*/

import { schema } from '@kbn/config-schema';
import { runSynPrivateLocationMonitorsTaskSoon } from '../../../tasks/sync_private_locations_monitors_task';
import { getExistingParamsSpaces } from './delete_param';
import type { SyntheticsRestApiRouteFactory } from '../../types';
import { syntheticsParamType } from '../../../../common/types/saved_objects';
import { SYNTHETICS_API_URLS } from '../../../../common/constants';
import type { DeleteParamsResponse } from '../../../../common/runtime_types';
import { asyncGlobalParamsPropagation } from '../../../tasks/sync_global_params_task';

export const deleteSyntheticsParamsBulkRoute: SyntheticsRestApiRouteFactory<
DeleteParamsResponse[],
Expand All @@ -31,13 +32,16 @@ export const deleteSyntheticsParamsBulkRoute: SyntheticsRestApiRouteFactory<
handler: async ({ savedObjectsClient, request, server, spaceId }) => {
const { ids } = request.body;

const existingParamsSpaces = await getExistingParamsSpaces(savedObjectsClient, ids);

const result = await savedObjectsClient.bulkDelete(
ids.map((id) => ({ type: syntheticsParamType, id })),
{ force: true }
);

await runSynPrivateLocationMonitorsTaskSoon({
await asyncGlobalParamsPropagation({
server,
paramsSpacesToSync: existingParamsSpaces,
});

return result.statuses.map(({ id, success }) => ({ id, deleted: success }));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { schema } from '@kbn/config-schema';
import type { SavedObject } from '@kbn/core/server';
import { SavedObjectsErrorHelpers } from '@kbn/core/server';
import { isEmpty } from 'lodash';
import { runSynPrivateLocationMonitorsTaskSoon } from '../../../tasks/sync_private_locations_monitors_task';
import { validateRouteSpaceName } from '../../common';
import type { SyntheticsRestApiRouteFactory } from '../../types';
import type { SyntheticsParamRequest, SyntheticsParams } from '../../../../common/runtime_types';
import { syntheticsParamType } from '../../../../common/types/saved_objects';
import { SYNTHETICS_API_URLS } from '../../../../common/constants';
import { asyncGlobalParamsPropagation } from '../../../tasks/sync_global_params_task';

const RequestParamsSchema = schema.object({
id: schema.string(),
Expand Down Expand Up @@ -86,8 +86,9 @@ export const editSyntheticsParamsRoute: SyntheticsRestApiRouteFactory<
newParam
)) as SavedObject<SyntheticsParams>;

await runSynPrivateLocationMonitorsTaskSoon({
await asyncGlobalParamsPropagation({
server,
paramsSpacesToSync: existingParam.namespaces || [spaceId],
});

return { id: responseId, key, tags, description, namespaces, value };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export const editPrivateLocationRoute: SyntheticsRestApiRouteFactory<
const [existingLocation, monitorsInLocation] = await Promise.all([
repo.getPrivateLocation(locationId),
routeContext.monitorConfigRepository.findDecryptedMonitors({
spaceId: ALL_SPACES_ID,
spaceIds: [ALL_SPACES_ID],
filter: filtersStr,
}),
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ describe('MonitorConfigRepository', () => {
pointInTimeFinderMock
);

const result = await repository.findDecryptedMonitors({ spaceId, filter });
const result = await repository.findDecryptedMonitors({ spaceIds: [spaceId], filter });

expect(
encryptedSavedObjectsClient.createPointInTimeFinderDecryptedAsInternalUser
Expand Down Expand Up @@ -709,7 +709,7 @@ describe('MonitorConfigRepository', () => {
pointInTimeFinderMock
);

const result = await repository.findDecryptedMonitors({ spaceId });
const result = await repository.findDecryptedMonitors({ spaceIds: [spaceId] });

expect(pointInTimeFinderMock.close).toHaveBeenCalled();
expect(result).toEqual([...mockDecryptedMonitors, ...mockDecryptedMonitors]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ export class MonitorConfigRepository {
return combineAndSortSavedObjects<T>(results, options, page, perPage);
}

async findDecryptedMonitors({ spaceId, filter }: { spaceId: string; filter?: string }) {
async findDecryptedMonitors({ spaceIds, filter }: { spaceIds: string[]; filter?: string }) {
const getDecrypted = async (soType: string) => {
// Handle legacy filter if the type is legacy
const legacyFilter =
Expand All @@ -304,7 +304,7 @@ export class MonitorConfigRepository {
filter: legacyFilter,
type: soType,
perPage: 500,
namespaces: [spaceId],
namespaces: spaceIds,
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ export class ProjectMonitorFormatter {

return await this.routeContext.monitorConfigRepository.findDecryptedMonitors({
filter: monitorFilter,
spaceId: this.spaceId,
spaceIds: [this.spaceId],
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class SyntheticsMonitorClient {
const privateConfigs: PrivateConfig[] = [];
const publicConfigs: ConfigData[] = [];

const paramsBySpace = await this.syntheticsService.getSyntheticsParams({ spaceId });
const paramsBySpace = await this.syntheticsService.getSyntheticsParams({ spaceIds: [spaceId] });
const maintenanceWindows = await this.syntheticsService.getMaintenanceWindows();

for (const monitorObj of monitors) {
Expand Down Expand Up @@ -99,7 +99,7 @@ export class SyntheticsMonitorClient {
const publicConfigs: ConfigData[] = [];
const deletedPublicConfigs: ConfigData[] = [];

const paramsBySpace = await this.syntheticsService.getSyntheticsParams({ spaceId });
const paramsBySpace = await this.syntheticsService.getSyntheticsParams({ spaceIds: [spaceId] });
const maintenanceWindows = await this.syntheticsService.getMaintenanceWindows();

for (const editedMonitor of monitors) {
Expand Down Expand Up @@ -188,7 +188,7 @@ export class SyntheticsMonitorClient {
let privateConfig: PrivateConfig | undefined;
let publicConfig: ConfigData | undefined;

const paramsBySpace = await this.syntheticsService.getSyntheticsParams({ spaceId });
const paramsBySpace = await this.syntheticsService.getSyntheticsParams({ spaceIds: [spaceId] });

const { formattedConfig, params, config } = await this.formatConfigWithParams(
monitor,
Expand Down Expand Up @@ -303,7 +303,7 @@ export class SyntheticsMonitorClient {
) {
const privateConfigs: PrivateConfig[] = [];
const paramsBySpace = await this.syntheticsService.getSyntheticsParams({
spaceId,
spaceIds: [spaceId],
canSave,
hideParams,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ describe('SyntheticsService', () => {
],
});

const params = await service.getSyntheticsParams({ spaceId: 'default' });
const params = await service.getSyntheticsParams({ spaceIds: ['default'] });

expect(params).toEqual({
'*': {
Expand All @@ -468,7 +468,7 @@ describe('SyntheticsService', () => {
],
});

const params = await service.getSyntheticsParams({ spaceId: 'default' });
const params = await service.getSyntheticsParams({ spaceIds: ['default'] });

expect(params).toEqual({
default: {
Expand Down Expand Up @@ -498,7 +498,7 @@ describe('SyntheticsService', () => {
],
});

const params = await service.getSyntheticsParams({ spaceId: 'default' });
const params = await service.getSyntheticsParams({ spaceIds: ['default'] });

expect(params).toEqual({
'*': {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,10 +595,10 @@ export class SyntheticsService {
}

async getSyntheticsParams({
spaceId,
spaceIds = [ALL_SPACES_ID],
hideParams = false,
canSave = true,
}: { spaceId?: string; canSave?: boolean; hideParams?: boolean } = {}) {
}: { spaceIds?: string[]; canSave?: boolean; hideParams?: boolean } = {}) {
if (!canSave) {
return Object.create(null);
}
Expand All @@ -610,7 +610,7 @@ export class SyntheticsService {
await encryptedClient.createPointInTimeFinderDecryptedAsInternalUser<SyntheticsParams>({
type: syntheticsParamType,
perPage: 1000,
namespaces: spaceId ? [spaceId] : [ALL_SPACES_ID],
namespaces: spaceIds,
});

for await (const response of finder.find()) {
Expand Down Expand Up @@ -638,6 +638,7 @@ export class SyntheticsService {
};
}
});
const spaceId = spaceIds.find((id) => id !== ALL_SPACES_ID);
if (spaceId) {
paramsBySpace[spaceId] = {
...(paramsBySpace?.[spaceId] ?? {}),
Expand Down
Loading