[Fleet] Fix Fleet server host default conflict creation (#163826)

This commit is contained in:
Nicolas Chaulet 2023-08-15 08:35:44 -04:00 committed by GitHub
parent 3efc0a7c45
commit 1c0d656ae1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 3 deletions

View file

@ -48,7 +48,7 @@ export async function createFleetServerHost(
): Promise<FleetServerHost> {
if (data.is_default) {
const defaultItem = await getDefaultFleetServerHost(soClient);
if (defaultItem) {
if (defaultItem && defaultItem.id !== options?.id) {
await updateFleetServerHost(
soClient,
defaultItem.id,

View file

@ -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,

View file

@ -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);
});
});
});
}