-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(slo): Introduce createSLO application service (#140523)
- Loading branch information
Showing
14 changed files
with
227 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
x-pack/plugins/observability/server/services/slo/create_slo.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { CreateSLO } from './create_slo'; | ||
import { createAPMTransactionErrorRateIndicator, createSLOParams } from './fixtures/slo'; | ||
import { | ||
createResourceInstallerMock, | ||
createSLORepositoryMock, | ||
createTransformInstallerMock, | ||
} from './mocks'; | ||
import { ResourceInstaller } from './resource_installer'; | ||
import { SLORepository } from './slo_repository'; | ||
import { TransformInstaller } from './transform_installer'; | ||
|
||
const SPACE_ID = 'some-space-id'; | ||
|
||
describe('createSLO', () => { | ||
let mockResourceInstaller: jest.Mocked<ResourceInstaller>; | ||
let mockRepository: jest.Mocked<SLORepository>; | ||
let mockTransformInstaller: jest.Mocked<TransformInstaller>; | ||
let createSLO: CreateSLO; | ||
|
||
beforeEach(() => { | ||
mockResourceInstaller = createResourceInstallerMock(); | ||
mockRepository = createSLORepositoryMock(); | ||
mockTransformInstaller = createTransformInstallerMock(); | ||
createSLO = new CreateSLO( | ||
mockResourceInstaller, | ||
mockRepository, | ||
mockTransformInstaller, | ||
SPACE_ID | ||
); | ||
}); | ||
|
||
describe('happy path', () => { | ||
it('calls the expected services', async () => { | ||
const sloParams = createSLOParams(createAPMTransactionErrorRateIndicator()); | ||
const response = await createSLO.execute(sloParams); | ||
|
||
expect(mockResourceInstaller.ensureCommonResourcesInstalled).toHaveBeenCalledWith(SPACE_ID); | ||
expect(mockRepository.save).toHaveBeenCalledWith( | ||
expect.objectContaining({ ...sloParams, id: expect.any(String) }) | ||
); | ||
expect(mockTransformInstaller.installAndStartTransform).toHaveBeenCalledWith( | ||
expect.objectContaining({ ...sloParams, id: expect.any(String) }), | ||
SPACE_ID | ||
); | ||
expect(response).toEqual(expect.objectContaining({ id: expect.any(String) })); | ||
}); | ||
}); | ||
|
||
describe('unhappy path', () => { | ||
it('deletes the SLO saved objects when transform installation fails', async () => { | ||
mockTransformInstaller.installAndStartTransform.mockRejectedValue( | ||
new Error('Transform Error') | ||
); | ||
const sloParams = createSLOParams(createAPMTransactionErrorRateIndicator()); | ||
|
||
await expect(createSLO.execute(sloParams)).rejects.toThrowError('Transform Error'); | ||
expect(mockRepository.deleteById).toBeCalled(); | ||
}); | ||
}); | ||
}); |
56 changes: 56 additions & 0 deletions
56
x-pack/plugins/observability/server/services/slo/create_slo.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import uuid from 'uuid'; | ||
|
||
import { SLO } from '../../types/models'; | ||
import { ResourceInstaller } from './resource_installer'; | ||
import { SLORepository } from './slo_repository'; | ||
import { TransformInstaller } from './transform_installer'; | ||
|
||
import { CreateSLOParams, CreateSLOResponse } from '../../types/schema'; | ||
|
||
export class CreateSLO { | ||
constructor( | ||
private resourceInstaller: ResourceInstaller, | ||
private repository: SLORepository, | ||
private transformInstaller: TransformInstaller, | ||
private spaceId: string | ||
) {} | ||
|
||
public async execute(sloParams: CreateSLOParams): Promise<CreateSLOResponse> { | ||
const slo = this.toSLO(sloParams); | ||
|
||
await this.resourceInstaller.ensureCommonResourcesInstalled(this.spaceId); | ||
await this.repository.save(slo); | ||
|
||
try { | ||
await this.transformInstaller.installAndStartTransform(slo, this.spaceId); | ||
} catch (err) { | ||
await this.repository.deleteById(slo.id); | ||
throw err; | ||
} | ||
|
||
return this.toResponse(slo); | ||
} | ||
|
||
private toSLO(sloParams: CreateSLOParams): SLO { | ||
return { | ||
...sloParams, | ||
id: uuid.v1(), | ||
settings: { | ||
destination_index: sloParams.settings?.destination_index, | ||
}, | ||
}; | ||
} | ||
|
||
private toResponse(slo: SLO): CreateSLOResponse { | ||
return { | ||
id: slo.id, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
x-pack/plugins/observability/server/services/slo/mocks/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { ResourceInstaller } from '../resource_installer'; | ||
import { SLORepository } from '../slo_repository'; | ||
import { TransformInstaller } from '../transform_installer'; | ||
|
||
const createResourceInstallerMock = (): jest.Mocked<ResourceInstaller> => { | ||
return { | ||
ensureCommonResourcesInstalled: jest.fn(), | ||
}; | ||
}; | ||
|
||
const createTransformInstallerMock = (): jest.Mocked<TransformInstaller> => { | ||
return { | ||
installAndStartTransform: jest.fn(), | ||
}; | ||
}; | ||
|
||
const createSLORepositoryMock = (): jest.Mocked<SLORepository> => { | ||
return { | ||
save: jest.fn(), | ||
findById: jest.fn(), | ||
deleteById: jest.fn(), | ||
}; | ||
}; | ||
|
||
export { createResourceInstallerMock, createTransformInstallerMock, createSLORepositoryMock }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.