import { ElasticsearchClient , SavedObjectsClientContract , Logger , } from 'kibana/server' ; import { PackagePolicy } from '../../../../fleet/common' ; import { APM_SERVER_SCHEMA_SAVED_OBJECT_TYPE , APM_SERVER_SCHEMA_SAVED_OBJECT_ID , } from '../../../common/apm_saved_object_constants' ; import { APMPluginSetupDependencies , APMPluginStartDependencies , } from '../../types' ; import { getApmPackagePolicyDefinition } from './get_apm_package_policy_definition' ; import { Setup } from '../../lib/helpers/setup_request' ; import { mergePackagePolicyWithApm } from './merge_package_policy_with_apm' ; export async function createCloudApmPackgePolicy ( { cloudPluginSetup , fleetPluginStart , savedObjectsClient , esClient , logger , setup , kibanaVersion , } ) { const { attributes } = await savedObjectsClient . get ( APM_SERVER_SCHEMA_SAVED_OBJECT_TYPE , APM_SERVER_SCHEMA_SAVED_OBJECT_ID ) ; const apmServerSchema = JSON . parse ( ( attributes as { schemaJson : string ; } ) . schemaJson ) ; const apmPackagePolicyDefinition = await getApmPackagePolicyDefinition ( { apmServerSchema , cloudPluginSetup , fleetPluginStart , kibanaVersion , } ) ; const mergedAPMPackagePolicy = await mergePackagePolicyWithApm ( { setup , packagePolicy : apmPackagePolicyDefinition , fleetPluginStart , } ) ; logger . info ( `Fleet migration on Cloud - apmPackagePolicy create start` ) ; const apmPackagePolicy = await fleetPluginStart . packagePolicyService . create ( savedObjectsClient , esClient , mergedAPMPackagePolicy , { force : true , bumpRevision : true } ) ; logger . info ( `Fleet migration on Cloud - apmPackagePolicy create end` ) ; return apmPackagePolicy ; }