@@ -148,74 +148,72 @@ export class SyntheticsPrivateLocation {
148148 if ( configs . length === 0 ) {
149149 return { created : [ ] , failed : [ ] } ;
150150 }
151- return this . runWithCache ( async ( ) => {
152- const newPolicies : NewPackagePolicyWithId [ ] = [ ] ;
153- const newPolicyTemplate = await this . buildNewPolicy ( ) ;
151+ const newPolicies : NewPackagePolicyWithId [ ] = [ ] ;
152+ const newPolicyTemplate = await this . buildNewPolicy ( ) ;
154153
155- for ( const { config, globalParams } of configs ) {
156- try {
157- const { locations } = config ;
158- const fleetManagedLocations = locations . filter ( ( loc ) => ! loc . isServiceManaged ) ;
159-
160- for ( const privateLocation of fleetManagedLocations ) {
161- const location = privateLocations ?. find ( ( loc ) => loc . id === privateLocation . id ) ! ;
162- if ( ! location ) {
163- throw new Error (
164- `Unable to find Synthetics private location for agentId ${ privateLocation . id } `
165- ) ;
166- }
154+ for ( const { config, globalParams } of configs ) {
155+ try {
156+ const { locations } = config ;
157+ const fleetManagedLocations = locations . filter ( ( loc ) => ! loc . isServiceManaged ) ;
167158
168- const newPolicy = await this . generateNewPolicy (
169- config ,
170- location ,
171- newPolicyTemplate ,
172- spaceId ,
173- globalParams ,
174- maintenanceWindows ,
175- testRunId ,
176- runOnce
159+ for ( const privateLocation of fleetManagedLocations ) {
160+ const location = privateLocations ?. find ( ( loc ) => loc . id === privateLocation . id ) ! ;
161+ if ( ! location ) {
162+ throw new Error (
163+ `Unable to find Synthetics private location for agentId ${ privateLocation . id } `
177164 ) ;
165+ }
178166
179- if ( ! newPolicy ) {
180- throw new Error (
181- `Unable to create Synthetics package policy for monitor ${
182- config [ ConfigKey . NAME ]
183- } with private location ${ location . label } `
184- ) ;
185- }
186- if ( newPolicy ) {
187- if ( testRunId ) {
188- newPolicies . push ( newPolicy as NewPackagePolicyWithId ) ;
189- } else {
190- newPolicies . push ( {
191- ...newPolicy ,
192- id : this . getPolicyId ( config , location . id , spaceId ) ,
193- } ) ;
194- }
167+ const newPolicy = await this . generateNewPolicy (
168+ config ,
169+ location ,
170+ newPolicyTemplate ,
171+ spaceId ,
172+ globalParams ,
173+ maintenanceWindows ,
174+ testRunId ,
175+ runOnce
176+ ) ;
177+
178+ if ( ! newPolicy ) {
179+ throw new Error (
180+ `Unable to create Synthetics package policy for monitor ${
181+ config [ ConfigKey . NAME ]
182+ } with private location ${ location . label } `
183+ ) ;
184+ }
185+ if ( newPolicy ) {
186+ if ( testRunId ) {
187+ newPolicies . push ( newPolicy as NewPackagePolicyWithId ) ;
188+ } else {
189+ newPolicies . push ( {
190+ ...newPolicy ,
191+ id : this . getPolicyId ( config , location . id , spaceId ) ,
192+ } ) ;
195193 }
196194 }
197- } catch ( e ) {
198- this . server . logger . error ( e ) ;
199- throw e ;
200- }
201- }
202-
203- if ( newPolicies . length === 0 ) {
204- throw new Error ( 'Failed to build package policies for all monitors' ) ;
205- }
206-
207- try {
208- const result = await this . createPolicyBulk ( newPolicies ) ;
209- if ( result ?. created && result ?. created ?. length > 0 && testRunId ) {
210- // ignore await here, we don't want to wait for this to finish
211- void scheduleCleanUpTask ( this . server ) ;
212195 }
213- return result ;
214196 } catch ( e ) {
215197 this . server . logger . error ( e ) ;
216198 throw e ;
217199 }
218- } ) ;
200+ }
201+
202+ if ( newPolicies . length === 0 ) {
203+ throw new Error ( 'Failed to build package policies for all monitors' ) ;
204+ }
205+
206+ try {
207+ const result = await this . createPolicyBulk ( newPolicies ) ;
208+ if ( result ?. created && result ?. created ?. length > 0 && testRunId ) {
209+ // ignore await here, we don't want to wait for this to finish
210+ void scheduleCleanUpTask ( this . server ) ;
211+ }
212+ return result ;
213+ } catch ( e ) {
214+ this . server . logger . error ( e ) ;
215+ throw e ;
216+ }
219217 }
220218
221219 async inspectPackagePolicy ( {
@@ -276,90 +274,88 @@ export class SyntheticsPrivateLocation {
276274 } ;
277275 }
278276
279- return this . runWithCache ( async ( ) => {
280- const [ newPolicyTemplate , existingPolicies ] = await Promise . all ( [
281- this . buildNewPolicy ( ) ,
282- this . getExistingPolicies (
283- configs . map ( ( { config } ) => config ) ,
284- allPrivateLocations ,
285- spaceId
286- ) ,
287- ] ) ;
288-
289- const policiesToUpdate : NewPackagePolicyWithId [ ] = [ ] ;
290- const policiesToCreate : NewPackagePolicyWithId [ ] = [ ] ;
291- const policiesToDelete : string [ ] = [ ] ;
292-
293- for ( const { config, globalParams } of configs ) {
294- const { locations } = config ;
277+ const [ newPolicyTemplate , existingPolicies ] = await Promise . all ( [
278+ this . buildNewPolicy ( ) ,
279+ this . getExistingPolicies (
280+ configs . map ( ( { config } ) => config ) ,
281+ allPrivateLocations ,
282+ spaceId
283+ ) ,
284+ ] ) ;
295285
296- const monitorPrivateLocations = locations . filter ( ( loc ) => ! loc . isServiceManaged ) ;
286+ const policiesToUpdate : NewPackagePolicyWithId [ ] = [ ] ;
287+ const policiesToCreate : NewPackagePolicyWithId [ ] = [ ] ;
288+ const policiesToDelete : string [ ] = [ ] ;
297289
298- for ( const privateLocation of allPrivateLocations ) {
299- const hasLocation = monitorPrivateLocations ?. some ( ( loc ) => loc . id === privateLocation . id ) ;
300- const currId = this . getPolicyId ( config , privateLocation . id , spaceId ) ;
301- const hasPolicy = existingPolicies ?. some ( ( policy ) => policy . id === currId ) ;
302- try {
303- if ( hasLocation ) {
304- const newPolicy = await this . generateNewPolicy (
305- config ,
306- privateLocation ,
307- newPolicyTemplate ,
308- spaceId ,
309- globalParams ,
310- maintenanceWindows
311- ) ;
312-
313- if ( ! newPolicy ) {
314- throwAddEditError ( hasPolicy , privateLocation . label ) ;
315- }
290+ for ( const { config, globalParams } of configs ) {
291+ const { locations } = config ;
316292
317- if ( hasPolicy ) {
318- policiesToUpdate . push ( { ...newPolicy , id : currId } as NewPackagePolicyWithId ) ;
319- } else {
320- policiesToCreate . push ( { ...newPolicy , id : currId } as NewPackagePolicyWithId ) ;
321- }
322- } else if ( hasPolicy ) {
323- policiesToDelete . push ( currId ) ;
293+ const monitorPrivateLocations = locations . filter ( ( loc ) => ! loc . isServiceManaged ) ;
294+
295+ for ( const privateLocation of allPrivateLocations ) {
296+ const hasLocation = monitorPrivateLocations ?. some ( ( loc ) => loc . id === privateLocation . id ) ;
297+ const currId = this . getPolicyId ( config , privateLocation . id , spaceId ) ;
298+ const hasPolicy = existingPolicies ?. some ( ( policy ) => policy . id === currId ) ;
299+ try {
300+ if ( hasLocation ) {
301+ const newPolicy = await this . generateNewPolicy (
302+ config ,
303+ privateLocation ,
304+ newPolicyTemplate ,
305+ spaceId ,
306+ globalParams ,
307+ maintenanceWindows
308+ ) ;
309+
310+ if ( ! newPolicy ) {
311+ throwAddEditError ( hasPolicy , privateLocation . label ) ;
312+ }
313+
314+ if ( hasPolicy ) {
315+ policiesToUpdate . push ( { ...newPolicy , id : currId } as NewPackagePolicyWithId ) ;
316+ } else {
317+ policiesToCreate . push ( { ...newPolicy , id : currId } as NewPackagePolicyWithId ) ;
324318 }
325- } catch ( e ) {
326- this . server . logger . error ( e ) ;
327- throwAddEditError ( hasPolicy , privateLocation . label , config [ ConfigKey . NAME ] ) ;
319+ } else if ( hasPolicy ) {
320+ policiesToDelete . push ( currId ) ;
328321 }
322+ } catch ( e ) {
323+ this . server . logger . error ( e ) ;
324+ throwAddEditError ( hasPolicy , privateLocation . label , config [ ConfigKey . NAME ] ) ;
329325 }
330326 }
327+ }
331328
332- this . server . logger . debug (
333- `[editingMonitors] Creating ${ policiesToCreate . length } policies, updating ${ policiesToUpdate . length } policies, and deleting ${ policiesToDelete . length } policies`
334- ) ;
329+ this . server . logger . debug (
330+ `[editingMonitors] Creating ${ policiesToCreate . length } policies, updating ${ policiesToUpdate . length } policies, and deleting ${ policiesToDelete . length } policies`
331+ ) ;
335332
336- const [ _createResponse , failedUpdatesRes , _deleteResponse ] = await Promise . all ( [
337- this . createPolicyBulk ( policiesToCreate ) ,
338- this . updatePolicyBulk ( policiesToUpdate ) ,
339- this . deletePolicyBulk ( policiesToDelete ) ,
340- ] ) ;
333+ const [ _createResponse , failedUpdatesRes , _deleteResponse ] = await Promise . all ( [
334+ this . createPolicyBulk ( policiesToCreate ) ,
335+ this . updatePolicyBulk ( policiesToUpdate ) ,
336+ this . deletePolicyBulk ( policiesToDelete ) ,
337+ ] ) ;
341338
342- const failedUpdates = failedUpdatesRes ?. map ( ( { packagePolicy, error } ) => {
343- const policyConfig = configs . find ( ( { config } ) => {
344- const { locations } = config ;
339+ const failedUpdates = failedUpdatesRes ?. map ( ( { packagePolicy, error } ) => {
340+ const policyConfig = configs . find ( ( { config } ) => {
341+ const { locations } = config ;
345342
346- const monitorPrivateLocations = locations . filter ( ( loc ) => ! loc . isServiceManaged ) ;
347- for ( const privateLocation of monitorPrivateLocations ) {
348- const currId = this . getPolicyId ( config , privateLocation . id , spaceId ) ;
349- return currId === packagePolicy . id ;
350- }
351- } ) ;
352- return {
353- error,
354- packagePolicy,
355- config : policyConfig ?. config ,
356- } ;
343+ const monitorPrivateLocations = locations . filter ( ( loc ) => ! loc . isServiceManaged ) ;
344+ for ( const privateLocation of monitorPrivateLocations ) {
345+ const currId = this . getPolicyId ( config , privateLocation . id , spaceId ) ;
346+ return currId === packagePolicy . id ;
347+ }
357348 } ) ;
358-
359349 return {
360- failedUpdates,
350+ error,
351+ packagePolicy,
352+ config : policyConfig ?. config ,
361353 } ;
362354 } ) ;
355+
356+ return {
357+ failedUpdates,
358+ } ;
363359 }
364360
365361 async getExistingPolicies (
@@ -436,39 +432,37 @@ export class SyntheticsPrivateLocation {
436432 }
437433
438434 async deleteMonitors ( configs : HeartbeatConfig [ ] , spaceId : string ) {
439- return this . runWithCache ( async ( ) => {
440- const soClient = this . server . coreStart . savedObjects . createInternalRepository ( ) ;
441- const esClient = this . server . coreStart . elasticsearch . client . asInternalUser ;
435+ const soClient = this . server . coreStart . savedObjects . createInternalRepository ( ) ;
436+ const esClient = this . server . coreStart . elasticsearch . client . asInternalUser ;
442437
443- const policyIdsToDelete = [ ] ;
444- for ( const config of configs ) {
445- const { locations } = config ;
438+ const policyIdsToDelete = [ ] ;
439+ for ( const config of configs ) {
440+ const { locations } = config ;
446441
447- const monitorPrivateLocations = locations . filter ( ( loc ) => ! loc . isServiceManaged ) ;
442+ const monitorPrivateLocations = locations . filter ( ( loc ) => ! loc . isServiceManaged ) ;
448443
449- for ( const privateLocation of monitorPrivateLocations ) {
450- policyIdsToDelete . push ( this . getPolicyId ( config , privateLocation . id , spaceId ) ) ;
451- }
444+ for ( const privateLocation of monitorPrivateLocations ) {
445+ policyIdsToDelete . push ( this . getPolicyId ( config , privateLocation . id , spaceId ) ) ;
452446 }
453- if ( policyIdsToDelete . length > 0 ) {
454- const result = await this . server . fleet . packagePolicyService . delete (
455- soClient ,
456- esClient ,
457- policyIdsToDelete ,
458- {
459- force : true ,
460- asyncDeploy : true ,
461- }
462- ) ;
463- const failedPolicies = result ?. filter ( ( policy ) => {
464- return ! policy . success && policy ?. statusCode !== 404 ;
465- } ) ;
466- if ( failedPolicies ?. length === policyIdsToDelete . length ) {
467- throw new Error ( deletePolicyError ( configs [ 0 ] [ ConfigKey . NAME ] ) ) ;
447+ }
448+ if ( policyIdsToDelete . length > 0 ) {
449+ const result = await this . server . fleet . packagePolicyService . delete (
450+ soClient ,
451+ esClient ,
452+ policyIdsToDelete ,
453+ {
454+ force : true ,
455+ asyncDeploy : true ,
468456 }
469- return result ;
457+ ) ;
458+ const failedPolicies = result ?. filter ( ( policy ) => {
459+ return ! policy . success && policy ?. statusCode !== 404 ;
460+ } ) ;
461+ if ( failedPolicies ?. length === policyIdsToDelete . length ) {
462+ throw new Error ( deletePolicyError ( configs [ 0 ] [ ConfigKey . NAME ] ) ) ;
470463 }
471- } ) ;
464+ return result ;
465+ }
472466 }
473467
474468 async getAgentPolicies ( ) {
@@ -481,10 +475,6 @@ export class SyntheticsPrivateLocation {
481475 }
482476 return undefined ;
483477 }
484-
485- async runWithCache < T > ( fn : ( ) => Promise < T > ) : Promise < T > {
486- return await this . server . fleet . runWithCache ( fn ) ;
487- }
488478}
489479
490480const throwAddEditError = ( hasPolicy : boolean , location ?: string , name ?: string ) => {
0 commit comments