diff --git a/x-pack/plugins/fleet/server/services/fleet_server_host.ts b/x-pack/plugins/fleet/server/services/fleet_server_host.ts index 62ceb0a87eea7..156d7e478b02b 100644 --- a/x-pack/plugins/fleet/server/services/fleet_server_host.ts +++ b/x-pack/plugins/fleet/server/services/fleet_server_host.ts @@ -48,7 +48,7 @@ export async function createFleetServerHost( ): Promise { if (data.is_default) { const defaultItem = await getDefaultFleetServerHost(soClient); - if (defaultItem) { + if (defaultItem && defaultItem.id !== options?.id) { await updateFleetServerHost( soClient, defaultItem.id, diff --git a/x-pack/plugins/fleet/server/services/output.ts b/x-pack/plugins/fleet/server/services/output.ts index 2cf1764a78f59..f190fc7d2315f 100644 --- a/x-pack/plugins/fleet/server/services/output.ts +++ b/x-pack/plugins/fleet/server/services/output.ts @@ -432,7 +432,7 @@ class OutputService { // ensure only default output exists if (data.is_default) { - if (defaultDataOutputId) { + if (defaultDataOutputId && defaultDataOutputId !== options?.id) { await this._updateDefaultOutput( soClient, defaultDataOutputId, @@ -443,7 +443,7 @@ class OutputService { } if (data.is_default_monitoring) { const defaultMonitoringOutputId = await this.getDefaultMonitoringOutputId(soClient); - if (defaultMonitoringOutputId) { + if (defaultMonitoringOutputId && defaultMonitoringOutputId !== options?.id) { await this._updateDefaultOutput( soClient, defaultMonitoringOutputId, diff --git a/x-pack/test/fleet_api_integration/apis/fleet_server_hosts/crud.ts b/x-pack/test/fleet_api_integration/apis/fleet_server_hosts/crud.ts index 21bc912dbad29..f17a894f859cc 100644 --- a/x-pack/test/fleet_api_integration/apis/fleet_server_hosts/crud.ts +++ b/x-pack/test/fleet_api_integration/apis/fleet_server_hosts/crud.ts @@ -117,5 +117,60 @@ export default function (providerContext: FtrProviderContext) { .expect(404); }); }); + + describe('POST /fleet_server_hosts', () => { + it('should allow to create a default fleet server host with id', async function () { + const id = `test-${Date.now()}`; + + await supertest + .post(`/api/fleet/fleet_server_hosts`) + .set('kbn-xsrf', 'xxxx') + .send({ + name: `Default ${Date.now()}`, + host_urls: ['https://test.fr:8080', 'https://test.fr:8081'], + is_default: true, + id, + }) + .expect(200); + + const { + body: { item: fleetServerHost }, + } = await supertest.get(`/api/fleet/fleet_server_hosts/${id}`).expect(200); + + expect(fleetServerHost.is_default).to.be(true); + }); + + it('should not unset default fleet server host on id conflict', async function () { + const id = `test-${Date.now()}`; + + await supertest + .post(`/api/fleet/fleet_server_hosts`) + .set('kbn-xsrf', 'xxxx') + .send({ + name: `Default ${Date.now()}`, + host_urls: ['https://test.fr:8080', 'https://test.fr:8081'], + is_default: true, + id, + }) + .expect(200); + + await supertest + .post(`/api/fleet/fleet_server_hosts`) + .set('kbn-xsrf', 'xxxx') + .send({ + name: `Default ${Date.now()}`, + host_urls: ['https://test.fr:8080', 'https://test.fr:8081'], + is_default: true, + id, + }) + .expect(409); + + const { + body: { item: fleetServerHost }, + } = await supertest.get(`/api/fleet/fleet_server_hosts/${id}`).expect(200); + + expect(fleetServerHost.is_default).to.be(true); + }); + }); }); }