Skip to content

Commit

Permalink
[Fleet] Fix Fleet server host default conflict creation (#163826)
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet authored Aug 15, 2023
1 parent 3efc0a7 commit 1c0d656
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
2 changes: 1 addition & 1 deletion x-pack/plugins/fleet/server/services/fleet_server_host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/fleet/server/services/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
55 changes: 55 additions & 0 deletions x-pack/test/fleet_api_integration/apis/fleet_server_hosts/crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
}

0 comments on commit 1c0d656

Please sign in to comment.