import { CoreSetup , CoreStart , SavedObjectsClientContract , } from 'kibana/server' ; import { TelemetryUsageCounter } from '../typings' ; import { APMPluginStartDependencies } from '../../types' ; import { getInternalSavedObjectsClient } from '../../lib/helpers/get_internal_saved_objects_client' ; import { Setup } from '../../lib/helpers/setup_request' ; import { listConfigurations } from '../settings/agent_configuration/list_configurations' ; import { getApmPackgePolicies } from './get_apm_package_policies' ; import { getPackagePolicyWithAgentConfigurations } from './register_fleet_policy_callbacks' ; export async function syncAgentConfigsToApmPackagePolicies ( { core , fleetPluginStart , setup , telemetryUsageCounter , } ) { if ( telemetryUsageCounter ) { telemetryUsageCounter . incrementCounter ( { counterName : 'sync_agent_config_to_apm_package_policies' , counterType : 'success' , } ) ; } const coreStart = await core . start ( ) ; const esClient = coreStart . elasticsearch . client . asInternalUser ; const [ savedObjectsClient , agentConfigurations , packagePolicies ] = await Promise . all ( [ getInternalSavedObjectsClient ( core . setup ) , listConfigurations ( { setup } ) , getApmPackgePolicies ( { core , fleetPluginStart , } ) , ] ) ; return Promise . all ( packagePolicies . items . map ( async ( item ) => { const { id , revision , updated_at , updated_by , ... packagePolicy } = item ; const updatedPackagePolicy = getPackagePolicyWithAgentConfigurations ( packagePolicy , agentConfigurations ) ; return fleetPluginStart . packagePolicyService . update ( savedObjectsClient as unknown as SavedObjectsClientContract , esClient , id , updatedPackagePolicy ) ; } ) ) ; }