[8.6] [Synthetics] Fixes project monitor edit code path (#146416) (#146853)

# Backport

This will backport the following commits from `main` to `8.6`:
- [[Synthetics] Fixes project monitor edit code path
(#146416)](https://github.com/elastic/kibana/pull/146416)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT
[{"author":{"name":"Shahzad","email":"shahzad.muhammad@elastic.co"},"sourceCommit":{"committedDate":"2022-12-02T08:56:29Z","message":"[Synthetics]
Fixes project monitor edit code path (#146416)\n\n## Summary\n\nFixed
project monitor editor code path, it was called un-necessarily on\neach
request.","sha":"dc2b29add974e8ab086979f11f7ff7fa8967a495","branchLabelMapping":{"^v8.7.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Team:uptime","release_note:skip","v8.6.0","v8.7.0"],"number":146416,"url":"https://github.com/elastic/kibana/pull/146416","mergeCommit":{"message":"[Synthetics]
Fixes project monitor edit code path (#146416)\n\n## Summary\n\nFixed
project monitor editor code path, it was called un-necessarily on\neach
request.","sha":"dc2b29add974e8ab086979f11f7ff7fa8967a495"}},"sourceBranch":"main","suggestedTargetBranches":["8.6"],"targetPullRequestStates":[{"branch":"8.6","label":"v8.6.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.7.0","labelRegex":"^v8.7.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/146416","number":146416,"mergeCommit":{"message":"[Synthetics]
Fixes project monitor edit code path (#146416)\n\n## Summary\n\nFixed
project monitor editor code path, it was called un-necessarily on\neach
request.","sha":"dc2b29add974e8ab086979f11f7ff7fa8967a495"}}]}]
BACKPORT-->

Co-authored-by: Shahzad <shahzad.muhammad@elastic.co>
This commit is contained in:
Kibana Machine 2022-12-02 05:02:22 -05:00 committed by GitHub
parent 8e7f17ac69
commit e4a9eb90fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 35 deletions

View file

@ -10,7 +10,6 @@ import { ProjectMonitor } from '../../../common/runtime_types';
import { SyntheticsRestApiRouteFactory } from '../../legacy_uptime/routes/types';
import { API_URLS } from '../../../common/constants';
import { getAllLocations } from '../../synthetics_service/get_all_locations';
import { ProjectMonitorFormatter } from '../../synthetics_service/project_monitor/project_monitor_formatter';
const MAX_PAYLOAD_SIZE = 1048576 * 20; // 20MiB
@ -52,18 +51,11 @@ export const addSyntheticsProjectMonitorRoute: SyntheticsRestApiRouteFactory = (
}
try {
const { publicLocations, privateLocations } = await getAllLocations(
server,
syntheticsMonitorClient,
savedObjectsClient
);
const encryptedSavedObjectsClient = server.encryptedSavedObjects.getClient();
const pushMonitorFormatter = new ProjectMonitorFormatter({
projectId: decodedProjectName,
spaceId,
locations: publicLocations,
privateLocations,
encryptedSavedObjectsClient,
savedObjectsClient,
monitors,

View file

@ -10,7 +10,13 @@ import {
INSUFFICIENT_FLEET_PERMISSIONS,
ProjectMonitorFormatter,
} from './project_monitor_formatter';
import { ConfigKey, DataStream, LocationStatus } from '../../../common/runtime_types';
import {
ConfigKey,
DataStream,
Locations,
LocationStatus,
PrivateLocation,
} from '../../../common/runtime_types';
import { DEFAULT_FIELDS } from '../../../common/constants/monitor_defaults';
import { times } from 'lodash';
import { SyntheticsService } from '../synthetics_service';
@ -22,6 +28,7 @@ import { formatSecrets } from '../utils';
import * as telemetryHooks from '../../routes/telemetry/monitor_upgrade_sender';
import { formatLocation } from '../../../common/utils/location_formatter';
import * as locationsUtil from '../get_all_locations';
const testMonitors = [
{
@ -86,7 +93,7 @@ const privateLocations = times(1).map((n) => {
agentPolicyId: `loc-${n}`,
concurrentMonitors: 1,
};
});
}) as PrivateLocation[];
describe('ProjectMonitorFormatter', () => {
const mockEsClient = {
@ -124,7 +131,7 @@ describe('ProjectMonitorFormatter', () => {
const encryptedSavedObjectsClient = encryptedSavedObjectsMock.createStart().getClient();
const locations = times(3).map((n) => {
const publicLocations = times(3).map((n) => {
return {
id: `loc-${n}`,
label: `Location ${n}`,
@ -136,16 +143,22 @@ describe('ProjectMonitorFormatter', () => {
isServiceManaged: true,
status: LocationStatus.GA,
};
});
}) as Locations;
const monitorClient = new SyntheticsMonitorClient(syntheticsService, serverMock);
jest.spyOn(locationsUtil, 'getAllLocations').mockImplementation(
async () =>
({
publicLocations,
privateLocations,
} as any)
);
it('should return errors', async () => {
const pushMonitorFormatter = new ProjectMonitorFormatter({
projectId: 'test-project',
spaceId: 'default-space',
locations,
privateLocations,
encryptedSavedObjectsClient,
savedObjectsClient: soClient,
monitors: testMonitors,
@ -194,8 +207,6 @@ describe('ProjectMonitorFormatter', () => {
const pushMonitorFormatter = new ProjectMonitorFormatter({
projectId: 'test-project',
spaceId: 'default-space',
locations,
privateLocations,
encryptedSavedObjectsClient,
savedObjectsClient: soClient,
monitors: testMonitors,
@ -244,8 +255,6 @@ describe('ProjectMonitorFormatter', () => {
const pushMonitorFormatter = new ProjectMonitorFormatter({
projectId: 'test-project',
spaceId: 'default-space',
locations,
privateLocations,
encryptedSavedObjectsClient,
savedObjectsClient: soClient,
monitors: testMonitors,
@ -289,8 +298,6 @@ describe('ProjectMonitorFormatter', () => {
const pushMonitorFormatter = new ProjectMonitorFormatter({
projectId: 'test-project',
spaceId: 'default-space',
locations,
privateLocations,
encryptedSavedObjectsClient,
savedObjectsClient: soClient,
monitors: testMonitors,
@ -335,8 +342,6 @@ describe('ProjectMonitorFormatter', () => {
const pushMonitorFormatter = new ProjectMonitorFormatter({
projectId: 'test-project',
spaceId: 'default-space',
locations,
privateLocations,
encryptedSavedObjectsClient,
savedObjectsClient: soClient,
monitors: testMonitors,
@ -387,8 +392,6 @@ describe('ProjectMonitorFormatter', () => {
const pushMonitorFormatter = new ProjectMonitorFormatter({
projectId: 'test-project',
spaceId: 'default-space',
locations,
privateLocations,
encryptedSavedObjectsClient,
savedObjectsClient: soClient,
monitors: testMonitors,

View file

@ -13,6 +13,7 @@ import {
} from '@kbn/core/server';
import { i18n } from '@kbn/i18n';
import { EncryptedSavedObjectsClient } from '@kbn/encrypted-saved-objects-plugin/server';
import { getAllLocations } from '../get_all_locations';
import { syncNewMonitorBulk } from '../../routes/monitor_cruds/bulk_cruds/add_monitor_bulk';
import { SyntheticsMonitorClient } from '../synthetics_monitor/synthetics_monitor_client';
import { syncEditedMonitorBulk } from '../../routes/monitor_cruds/bulk_cruds/edit_monitor_bulk';
@ -71,7 +72,7 @@ export const FAILED_TO_UPDATE_MONITORS = i18n.translate(
export class ProjectMonitorFormatter {
private projectId: string;
private spaceId: string;
private locations: Locations;
private publicLocations: Locations;
private privateLocations: PrivateLocation[];
private savedObjectsClient: SavedObjectsClientContract;
private encryptedSavedObjectsClient: EncryptedSavedObjectsClient;
@ -87,8 +88,6 @@ export class ProjectMonitorFormatter {
private writeIntegrationPoliciesPermissions?: boolean;
constructor({
locations,
privateLocations,
savedObjectsClient,
encryptedSavedObjectsClient,
projectId,
@ -98,8 +97,6 @@ export class ProjectMonitorFormatter {
syntheticsMonitorClient,
request,
}: {
locations: Locations;
privateLocations: PrivateLocation[];
savedObjectsClient: SavedObjectsClientContract;
encryptedSavedObjectsClient: EncryptedSavedObjectsClient;
projectId: string;
@ -111,8 +108,6 @@ export class ProjectMonitorFormatter {
}) {
this.projectId = projectId;
this.spaceId = spaceId;
this.locations = locations;
this.privateLocations = privateLocations;
this.savedObjectsClient = savedObjectsClient;
this.encryptedSavedObjectsClient = encryptedSavedObjectsClient;
this.syntheticsMonitorClient = syntheticsMonitorClient;
@ -120,10 +115,33 @@ export class ProjectMonitorFormatter {
this.server = server;
this.projectFilter = `${syntheticsMonitorType}.attributes.${ConfigKey.PROJECT_ID}: "${this.projectId}"`;
this.request = request;
this.publicLocations = [];
this.privateLocations = [];
}
init = async () => {
const locationsPromise = getAllLocations(
this.server,
this.syntheticsMonitorClient,
this.savedObjectsClient
);
const existingMonitorsPromise = this.getProjectMonitorsForProject();
const [locations, existingMonitors] = await Promise.all([
locationsPromise,
existingMonitorsPromise,
]);
const { publicLocations, privateLocations } = locations;
this.publicLocations = publicLocations;
this.privateLocations = privateLocations;
return existingMonitors;
};
public configureAllProjectMonitors = async () => {
const existingMonitors = await this.getProjectMonitorsForProject();
const existingMonitors = await this.init();
const normalizedNewMonitors: SyntheticsMonitor[] = [];
const normalizedUpdateMonitors: Array<{
@ -161,7 +179,6 @@ export class ProjectMonitorFormatter {
),
payload: monitor,
});
continue;
} else if (previousMonitor) {
this.updatedMonitors.push(monitor.id);
normalizedUpdateMonitors.push({ monitor: normM as MonitorFields, previousMonitor });
@ -197,7 +214,7 @@ export class ProjectMonitorFormatter {
const { normalizedFields: normalizedMonitor, errors } = normalizeProjectMonitor({
monitor,
locations: this.locations,
locations: this.publicLocations,
privateLocations: this.privateLocations,
projectId: this.projectId,
namespace: this.spaceId,
@ -258,7 +275,8 @@ export class ProjectMonitorFormatter {
);
}
await finder.close();
// no need to wait for it
finder.close();
return hits;
};
@ -336,6 +354,13 @@ export class ProjectMonitorFormatter {
errors: ServiceLocationErrors;
updatedCount: number;
}> => {
if (monitors.length === 0) {
return {
editedMonitors: [],
errors: [],
updatedCount: 0,
};
}
const decryptedPreviousMonitors = await this.getDecryptedMonitors(
monitors.map((m) => m.previousMonitor)
);