diff --git a/x-pack/plugins/fleet/server/services/agent_policies/__snapshots__/full_agent_policy.test.ts.snap b/x-pack/plugins/fleet/server/services/agent_policies/__snapshots__/full_agent_policy.test.ts.snap index 21192a1158009..86f8126c1c45a 100644 --- a/x-pack/plugins/fleet/server/services/agent_policies/__snapshots__/full_agent_policy.test.ts.snap +++ b/x-pack/plugins/fleet/server/services/agent_policies/__snapshots__/full_agent_policy.test.ts.snap @@ -12,7 +12,7 @@ Object { "logs": false, "metrics": true, "namespace": "default", - "use_output": "test-id", + "use_output": "default", }, "protection": Object { "enabled": false, @@ -35,7 +35,7 @@ Object { ], }, }, - "test-id": Object { + "default": Object { "_elastic_agent_checks": Object { "cluster": Array [ "monitor", @@ -61,7 +61,7 @@ Object { "preset": "balanced", "type": "elasticsearch", }, - "test-id": Object { + "default": Object { "hosts": Array [ "http://127.0.0.1:9201", ], @@ -106,6 +106,13 @@ Object { "id": "agent-policy", "inputs": Array [], "output_permissions": Object { + "default": Object { + "_elastic_agent_checks": Object { + "cluster": Array [ + "monitor", + ], + }, + }, "monitoring-output-id": Object { "_elastic_agent_checks": Object { "cluster": Array [ @@ -123,25 +130,18 @@ Object { ], }, }, - "test-id": Object { - "_elastic_agent_checks": Object { - "cluster": Array [ - "monitor", - ], - }, - }, }, "outputs": Object { - "monitoring-output-id": Object { + "default": Object { "hosts": Array [ - "http://es-monitoring.co:9201", + "http://127.0.0.1:9201", ], "preset": "balanced", "type": "elasticsearch", }, - "test-id": Object { + "monitoring-output-id": Object { "hosts": Array [ - "http://127.0.0.1:9201", + "http://es-monitoring.co:9201", ], "preset": "balanced", "type": "elasticsearch", diff --git a/x-pack/plugins/fleet/server/services/agent_policies/full_agent_policy.test.ts b/x-pack/plugins/fleet/server/services/agent_policies/full_agent_policy.test.ts index 09bf54773509f..0ecd385c2516c 100644 --- a/x-pack/plugins/fleet/server/services/agent_policies/full_agent_policy.test.ts +++ b/x-pack/plugins/fleet/server/services/agent_policies/full_agent_policy.test.ts @@ -93,6 +93,15 @@ jest.mock('../output', () => { type: 'elasticsearch', hosts: ['http://127.0.0.1:9201'], }, + 'test-remote-id': { + id: 'test-remote-id', + is_default: true, + is_default_monitoring: true, + name: 'default', + // @ts-ignore + type: 'remote_elasticsearch', + hosts: ['http://127.0.0.1:9201'], + }, }; return { outputService: { @@ -197,7 +206,7 @@ describe('getFullAgentPolicy', () => { expect(agentPolicy).toMatchObject({ id: 'agent-policy', outputs: { - 'test-id': { + default: { type: 'elasticsearch', hosts: ['http://127.0.0.1:9201'], }, @@ -228,7 +237,7 @@ describe('getFullAgentPolicy', () => { expect(agentPolicy).toMatchObject({ id: 'agent-policy', outputs: { - 'test-id': { + default: { type: 'elasticsearch', hosts: ['http://127.0.0.1:9201'], }, @@ -244,7 +253,7 @@ describe('getFullAgentPolicy', () => { }, monitoring: { namespace: 'default', - use_output: 'test-id', + use_output: 'default', enabled: true, logs: true, metrics: false, @@ -264,7 +273,7 @@ describe('getFullAgentPolicy', () => { expect(agentPolicy).toMatchObject({ id: 'agent-policy', outputs: { - 'test-id': { + default: { type: 'elasticsearch', hosts: ['http://127.0.0.1:9201'], }, @@ -280,7 +289,7 @@ describe('getFullAgentPolicy', () => { }, monitoring: { namespace: 'default', - use_output: 'test-id', + use_output: 'default', enabled: true, logs: false, metrics: true, @@ -358,7 +367,7 @@ describe('getFullAgentPolicy', () => { expect(agentPolicy).toMatchSnapshot(); }); - it('should use output id from default policy id', async () => { + it('should use "default" as the default policy id', async () => { mockAgentPolicy({ id: 'policy', status: 'active', @@ -372,7 +381,24 @@ describe('getFullAgentPolicy', () => { const agentPolicy = await getFullAgentPolicy(savedObjectsClientMock.create(), 'agent-policy'); - expect(agentPolicy?.outputs['test-id']).toBeDefined(); + expect(agentPolicy?.outputs.default).toBeDefined(); + }); + + it('should use output id as the default policy id when remote elasticsearch', async () => { + mockAgentPolicy({ + id: 'policy', + status: 'active', + package_policies: [], + is_managed: false, + namespace: 'default', + revision: 1, + data_output_id: 'test-remote-id', + monitoring_output_id: 'test-remote-id', + }); + + const agentPolicy = await getFullAgentPolicy(savedObjectsClientMock.create(), 'agent-policy'); + + expect(agentPolicy?.outputs['test-remote-id']).toBeDefined(); }); it('should return the sourceURI from the agent policy', async () => { @@ -387,7 +413,7 @@ describe('getFullAgentPolicy', () => { expect(agentPolicy).toMatchObject({ id: 'agent-policy', outputs: { - 'test-id': { + default: { type: 'elasticsearch', hosts: ['http://127.0.0.1:9201'], }, @@ -403,7 +429,7 @@ describe('getFullAgentPolicy', () => { }, monitoring: { namespace: 'default', - use_output: 'test-id', + use_output: 'default', enabled: true, logs: false, metrics: true, @@ -427,7 +453,7 @@ describe('getFullAgentPolicy', () => { expect(agentPolicy).toMatchObject({ id: 'agent-policy', outputs: { - 'test-id': { + default: { type: 'elasticsearch', hosts: ['http://127.0.0.1:9201'], }, @@ -440,7 +466,7 @@ describe('getFullAgentPolicy', () => { agent: { monitoring: { namespace: 'default', - use_output: 'test-id', + use_output: 'default', enabled: true, logs: false, metrics: true, @@ -626,7 +652,7 @@ describe('getFullAgentPolicy', () => { }, ], type: 'test-logs', - use_output: 'test-id', + use_output: 'default', }, { data_stream: { @@ -652,11 +678,11 @@ describe('getFullAgentPolicy', () => { }, ], type: 'test-logs', - use_output: 'test-id', + use_output: 'default', }, ], output_permissions: { - 'test-id': { + default: { _elastic_agent_checks: { cluster: ['monitor'], }, @@ -679,7 +705,7 @@ describe('getFullAgentPolicy', () => { }, }, outputs: { - 'test-id': { + default: { hosts: ['http://127.0.0.1:9201'], preset: 'balanced', type: 'elasticsearch', diff --git a/x-pack/plugins/fleet/server/services/agent_policies/full_agent_policy.ts b/x-pack/plugins/fleet/server/services/agent_policies/full_agent_policy.ts index 3a35eb9373cb6..ab277a97774d0 100644 --- a/x-pack/plugins/fleet/server/services/agent_policies/full_agent_policy.ts +++ b/x-pack/plugins/fleet/server/services/agent_policies/full_agent_policy.ts @@ -31,7 +31,12 @@ import type { PackageInfo, } from '../../../common/types'; import { agentPolicyService } from '../agent_policy'; -import { dataTypes, kafkaCompressionType, outputType } from '../../../common/constants'; +import { + dataTypes, + DEFAULT_OUTPUT, + kafkaCompressionType, + outputType, +} from '../../../common/constants'; import { getPackageInfo } from '../epm/packages'; import { pkgToPkgKey, splitPkgKey } from '../epm/registry'; @@ -490,8 +495,12 @@ export function transformOutputToFullPolicyOutput( /** * Get id used in full agent policy (sent to the agents) + * we use "default" for the default policy to avoid breaking changes */ function getOutputIdForAgentPolicy(output: Output) { + if (output.is_default && output.type === outputType.Elasticsearch) { + return DEFAULT_OUTPUT.name; + } return output.id; }